summaryrefslogtreecommitdiff
path: root/fitz
diff options
context:
space:
mode:
authorRobin Watts <robin.watts@artifex.com>2013-04-23 17:54:16 +0100
committerRobin Watts <robin.watts@artifex.com>2013-04-25 17:12:19 +0100
commitbbf2da4a518d02d83fd939fd716f09f4daa2546a (patch)
tree142aaa0ea6e03c5b8771a3e446c73ff2422d0f51 /fitz
parent8f5376d30e1004a35a62ddbb8d9d8fcc0673fe8e (diff)
downloadmupdf-bbf2da4a518d02d83fd939fd716f09f4daa2546a.tar.xz
Generalise fz_write_png to fz_output_pixmap_to_png
Extract the core of fz_write_png so that it can work to an fz_output * rather than a FILE *. fz_write_png continues to work as before, but now we can output to buffer to.
Diffstat (limited to 'fitz')
-rw-r--r--fitz/fitz.h5
-rw-r--r--fitz/res_pixmap.c59
2 files changed, 40 insertions, 24 deletions
diff --git a/fitz/fitz.h b/fitz/fitz.h
index b160b3cc..d3a554f4 100644
--- a/fitz/fitz.h
+++ b/fitz/fitz.h
@@ -2102,6 +2102,11 @@ int fz_printf(fz_output *, const char *, ...);
int fz_write(fz_output *out, const char *data, int len);
/*
+ Output a pixmap to an output stream as a png.
+*/
+void fz_output_pixmap_to_png(fz_context *ctx, fz_pixmap *pixmap, fz_output *out, int savealpha);
+
+/*
fz_close_output: Close a previously opened fz_output stream.
Note: whether or not this closes the underlying output method is
diff --git a/fitz/res_pixmap.c b/fitz/res_pixmap.c
index d8d94252..fa58c56a 100644
--- a/fitz/res_pixmap.c
+++ b/fitz/res_pixmap.c
@@ -545,31 +545,51 @@ static inline void big32(unsigned char *buf, unsigned int v)
buf[3] = (v) & 0xff;
}
-static inline void put32(unsigned int v, FILE *fp)
+static inline void put32(unsigned int v, fz_output *out)
{
- putc(v >> 24, fp);
- putc(v >> 16, fp);
- putc(v >> 8, fp);
- putc(v, fp);
+ fz_printf(out, "%c%c%c%c", v>>24, v>>16, v>>8, v);
}
-static void putchunk(char *tag, unsigned char *data, int size, FILE *fp)
+static void putchunk(char *tag, unsigned char *data, int size, fz_output *out)
{
unsigned int sum;
- put32(size, fp);
- fwrite(tag, 1, 4, fp);
- fwrite(data, 1, size, fp);
+ put32(size, out);
+ fz_write(out, tag, 4);
+ fz_write(out, data, size);
sum = crc32(0, NULL, 0);
sum = crc32(sum, (unsigned char*)tag, 4);
sum = crc32(sum, data, size);
- put32(sum, fp);
+ put32(sum, out);
}
void
fz_write_png(fz_context *ctx, fz_pixmap *pixmap, char *filename, int savealpha)
{
+ FILE *fp = fopen(filename, "wb");
+
+ if (!fp)
+ {
+ fz_throw(ctx, "cannot open file '%s': %s", filename, strerror(errno));
+ }
+
+ fz_try(ctx)
+ {
+ fz_output_pixmap_to_png(ctx, pixmap, fz_new_output_file(ctx, fp), savealpha);
+ }
+ fz_always(ctx)
+ {
+ fclose(fp);
+ }
+ fz_catch(ctx)
+ {
+ fz_rethrow(ctx);
+ }
+}
+
+void
+fz_output_pixmap_to_png(fz_context *ctx, fz_pixmap *pixmap, fz_output *out, int savealpha)
+{
static const unsigned char pngsig[8] = { 137, 80, 78, 71, 13, 10, 26, 10 };
- FILE *fp;
unsigned char head[13];
unsigned char *udata = NULL;
unsigned char *cdata = NULL;
@@ -639,14 +659,6 @@ fz_write_png(fz_context *ctx, fz_pixmap *pixmap, char *filename, int savealpha)
fz_throw(ctx, "cannot compress image data");
}
- fp = fopen(filename, "wb");
- if (!fp)
- {
- fz_free(ctx, udata);
- fz_free(ctx, cdata);
- fz_throw(ctx, "cannot open file '%s': %s", filename, strerror(errno));
- }
-
big32(head+0, pixmap->w);
big32(head+4, pixmap->h);
head[8] = 8; /* depth */
@@ -655,11 +667,10 @@ fz_write_png(fz_context *ctx, fz_pixmap *pixmap, char *filename, int savealpha)
head[11] = 0; /* filter */
head[12] = 0; /* interlace */
- fwrite(pngsig, 1, 8, fp);
- putchunk("IHDR", head, 13, fp);
- putchunk("IDAT", cdata, csize, fp);
- putchunk("IEND", head, 0, fp);
- fclose(fp);
+ fz_write(out, pngsig, 8);
+ putchunk("IHDR", head, 13, out);
+ putchunk("IDAT", cdata, csize, out);
+ putchunk("IEND", head, 0, out);
fz_free(ctx, udata);
fz_free(ctx, cdata);