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

View file

@ -166,24 +166,35 @@ pub const Image = struct {
end: usize, end: usize,
) !void { ) !void {
const total_bytes = end - start; const total_bytes = end - start;
var i: usize = 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]; var view: []f32 = buf[0..buf.len];
if (i + buf.len > end) {
const excess = i + buf.len - end; if (excess > 0) {
view = buf[0..excess]; std.debug.warn(
_ = c.sf_seek( "excess of {} bytes, reading {} instead\n",
self.sndfile, excess,
-@intCast(i64, excess), @intCast(i64, buf.len) - excess,
c.SEEK_CUR,
); );
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)); 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. /// Run a plugin over the image.
@ -265,28 +276,21 @@ pub const Image = struct {
// - plugin // - plugin
// - post-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); var file_copy_buf = try self.allocator.alloc(f32, BufferSize);
defer self.allocator.free(file_copy_buf); defer self.allocator.free(file_copy_buf);
// bmp header copy // pre-plugin copy, merged with 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
try self.copyBytes( try self.copyBytes(
out_file, out_file,
file_copy_buf, file_copy_buf,
BMPHeaderSize, usize(0),
seek_pos.start, seek_pos.start - 1,
); );
var i: usize = seek_pos.start; 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); const read_bytes = c.sf_readf_float(self.sndfile, rctx.in_buf.ptr, 1);
if (read_bytes == 0) { if (read_bytes == 0) {
std.debug.warn("WARN! reached EOF at idx={}\n", i); std.debug.warn("WARN! reached EOF at idx={}\n", i);
@ -301,7 +305,7 @@ pub const Image = struct {
try self.copyBytes( try self.copyBytes(
out_file, out_file,
file_copy_buf, file_copy_buf,
seek_pos.end, seek_pos.end + 1,
file_end, file_end,
); );