diff options
Diffstat (limited to 'source')
-rw-r--r-- | source/fitz/output-tga.c | 42 | ||||
-rw-r--r-- | source/fitz/output.c | 45 |
2 files changed, 65 insertions, 22 deletions
diff --git a/source/fitz/output-tga.c b/source/fitz/output-tga.c index b5e594b6..8a595265 100644 --- a/source/fitz/output-tga.c +++ b/source/fitz/output-tga.c @@ -6,21 +6,45 @@ static inline void tga_put_pixel(fz_context *ctx, fz_output *out, unsigned char *data, int n, int is_bgr) { - if (n >= 3 && !is_bgr) + switch(n) { - fz_putc(ctx, out, data[2]); + case 4: /* RGBA or BGRA */ + if (!is_bgr) { + fz_putc(ctx, out, data[2]); + fz_putc(ctx, out, data[1]); + fz_putc(ctx, out, data[0]); + } else { + fz_putc(ctx, out, data[0]); + fz_putc(ctx, out, data[1]); + fz_putc(ctx, out, data[2]); + } + fz_putc(ctx, out, data[3]); + break; + case 3: /* RGB or BGR */ + if (!is_bgr) { + fz_putc(ctx, out, data[2]); + fz_putc(ctx, out, data[1]); + fz_putc(ctx, out, data[0]); + } else { + fz_putc(ctx, out, data[0]); + fz_putc(ctx, out, data[1]); + fz_putc(ctx, out, data[2]); + } + fz_putc(ctx, out, 255); + break; + case 2: /* GA */ + fz_putc(ctx, out, data[0]); + fz_putc(ctx, out, data[0]); + fz_putc(ctx, out, data[0]); fz_putc(ctx, out, data[1]); + break; + case 1: /* GA */ fz_putc(ctx, out, data[0]); - if (n == 4) - fz_putc(ctx, out, data[3]); - return; - } - if (n == 2) - { fz_putc(ctx, out, data[0]); fz_putc(ctx, out, data[0]); + fz_putc(ctx, out, 255); + break; } - fz_write(ctx, out, data, n); } void diff --git a/source/fitz/output.c b/source/fitz/output.c index a93ecc97..27495b34 100644 --- a/source/fitz/output.c +++ b/source/fitz/output.c @@ -140,14 +140,32 @@ file_close(fz_context *ctx, void *opaque) } fz_output * +fz_new_output(fz_context *ctx, void *state, fz_output_write_fn *write, fz_output_close_fn *close) +{ + fz_output *out; + + fz_try(ctx) + { + out = fz_malloc_struct(ctx, fz_output); + out->state = state; + out->write = write; + out->close = close; + } + fz_catch(ctx) + { + if (close) + close(ctx, state); + fz_rethrow(ctx); + } + return out; +} + +fz_output * fz_new_output_with_file_ptr(fz_context *ctx, FILE *file, int close) { - fz_output *out = fz_malloc_struct(ctx, fz_output); - out->opaque = file; - out->write = file_write; + fz_output *out = fz_new_output(ctx, file, file_write, close ? file_close : NULL); out->seek = file_seek; out->tell = file_tell; - out->close = close ? file_close : NULL; return out; } @@ -214,12 +232,9 @@ buffer_close(fz_context *ctx, void *opaque) fz_output * fz_new_output_with_buffer(fz_context *ctx, fz_buffer *buf) { - fz_output *out = fz_malloc_struct(ctx, fz_output); - out->opaque = fz_keep_buffer(ctx, buf); - out->write = buffer_write; + fz_output *out = fz_new_output(ctx, fz_keep_buffer(ctx, buf), buffer_write, buffer_close); out->seek = buffer_seek; out->tell = buffer_tell; - out->close = buffer_close; return out; } @@ -228,8 +243,8 @@ fz_drop_output(fz_context *ctx, fz_output *out) { if (!out) return; if (out->close) - out->close(ctx, out->opaque); - if (out->opaque != &fz_stdout_global && out->opaque != &fz_stderr_global) + out->close(ctx, out->state); + if (out->state != &fz_stdout_global && out->state != &fz_stderr_global) fz_free(ctx, out); } @@ -237,14 +252,18 @@ void fz_seek_output(fz_context *ctx, fz_output *out, fz_off_t off, int whence) { if (!out) return; - out->seek(ctx, out->opaque, off, whence); + if (out->seek == NULL) + fz_throw(ctx, FZ_ERROR_GENERIC, "Cannot seek in unseekable output stream\n"); + out->seek(ctx, out->state, off, whence); } fz_off_t fz_tell_output(fz_context *ctx, fz_output *out) { if (!out) return 0; - return out->tell(ctx, out->opaque); + if (out->tell == NULL) + fz_throw(ctx, FZ_ERROR_GENERIC, "Cannot tell in untellable output stream\n"); + return out->tell(ctx, out->state); } void @@ -271,7 +290,7 @@ fz_vprintf(fz_context *ctx, fz_output *out, const char *fmt, va_list old_args) } fz_try(ctx) - out->write(ctx, out->opaque, p, len); + out->write(ctx, out->state, p, len); fz_always(ctx) if (p != buffer) fz_free(ctx, p); |