fix color mayhem caused by Image.copyBytes()

This commit is contained in:
Luna 2019-07-11 09:10:21 -03:00
parent 542c239e0c
commit 79dc56daae
1 changed files with 27 additions and 23 deletions

View File

@ -166,24 +166,35 @@ pub const Image = struct {
end: usize,
) !void {
const total_bytes = end - start;
var i: usize = start;
while (i < end) : (i += buf.len) {
const read_bytes = c.sf_readf_float(self.sndfile, buf.ptr, @intCast(i64, buf.len));
// we do sf_seek() calls to make sure we are actually on the start
// and actually end at the end position for the file.
_ = c.sf_seek(self.sndfile, @intCast(i64, start), c.SEEK_SET);
while (i <= end) : (i += buf.len) {
std.debug.warn("i={}, buf.len={}, end={}\n", i, buf.len, end);
const excess = @intCast(i64, i + buf.len) - @intCast(i64, end);
var read_bytes: i64 = undefined;
var view: []f32 = buf[0..buf.len];
if (i + buf.len > end) {
const excess = i + buf.len - end;
view = buf[0..excess];
_ = c.sf_seek(
self.sndfile,
-@intCast(i64, excess),
c.SEEK_CUR,
if (excess > 0) {
std.debug.warn(
"excess of {} bytes, reading {} instead\n",
excess,
@intCast(i64, buf.len) - excess,
);
read_bytes = c.sf_readf_float(self.sndfile, buf.ptr, @intCast(i64, buf.len) - excess);
view = buf[0..@intCast(usize, excess)];
} else {
read_bytes = c.sf_readf_float(self.sndfile, buf.ptr, @intCast(i64, buf.len));
}
try swrite(out_file, view.ptr, @intCast(i64, view.len));
}
_ = c.sf_seek(self.sndfile, @intCast(i64, end), c.SEEK_SET);
}
/// Run a plugin over the image.
@ -265,28 +276,21 @@ pub const Image = struct {
// - plugin
// - post-plugin
var header_buf = try self.allocator.alloc(f32, BMPHeaderSize);
defer self.allocator.free(header_buf);
var file_copy_buf = try self.allocator.alloc(f32, BufferSize);
defer self.allocator.free(file_copy_buf);
// bmp header copy
const header_len = @intCast(i64, header_buf.len);
_ = c.sf_readf_float(self.sndfile, header_buf.ptr, header_len);
try swrite(out_file, header_buf.ptr, header_len);
// pre-plugin copy
// pre-plugin copy, merged with bmp header copy
try self.copyBytes(
out_file,
file_copy_buf,
BMPHeaderSize,
seek_pos.start,
usize(0),
seek_pos.start - 1,
);
var i: usize = seek_pos.start;
std.debug.warn("\tseek pos start: {} end: {}\n", seek_pos.start, seek_pos.end);
while (i < seek_pos.end) : (i += 1) {
while (i <= seek_pos.end) : (i += 1) {
const read_bytes = c.sf_readf_float(self.sndfile, rctx.in_buf.ptr, 1);
if (read_bytes == 0) {
std.debug.warn("WARN! reached EOF at idx={}\n", i);
@ -301,7 +305,7 @@ pub const Image = struct {
try self.copyBytes(
out_file,
file_copy_buf,
seek_pos.end,
seek_pos.end + 1,
file_end,
);