summaryrefslogtreecommitdiff
path: root/source/fitz/bitmap.c
diff options
context:
space:
mode:
authorTor Andersson <tor.andersson@artifex.com>2013-06-19 15:29:44 +0200
committerTor Andersson <tor.andersson@artifex.com>2013-06-20 16:45:35 +0200
commit0a927854a10e1e6b9770a81e2e1d9f3093631757 (patch)
tree3d65d820d9fdba2d0d394d99c36290c851b78ca0 /source/fitz/bitmap.c
parent1ae8f19179c5f0f8c6352b3c7855465325d5449a (diff)
downloadmupdf-0a927854a10e1e6b9770a81e2e1d9f3093631757.tar.xz
Rearrange source files.
Diffstat (limited to 'source/fitz/bitmap.c')
-rw-r--r--source/fitz/bitmap.c123
1 files changed, 123 insertions, 0 deletions
diff --git a/source/fitz/bitmap.c b/source/fitz/bitmap.c
new file mode 100644
index 00000000..6357f7d7
--- /dev/null
+++ b/source/fitz/bitmap.c
@@ -0,0 +1,123 @@
+#include "mupdf/fitz.h"
+
+fz_bitmap *
+fz_new_bitmap(fz_context *ctx, int w, int h, int n, int xres, int yres)
+{
+ fz_bitmap *bit;
+
+ bit = fz_malloc_struct(ctx, fz_bitmap);
+ bit->refs = 1;
+ bit->w = w;
+ bit->h = h;
+ bit->n = n;
+ bit->xres = xres;
+ bit->yres = yres;
+ /* Span is 32 bit aligned. We may want to make this 64 bit if we
+ * use SSE2 etc. */
+ bit->stride = ((n * w + 31) & ~31) >> 3;
+
+ bit->samples = fz_malloc_array(ctx, h, bit->stride);
+
+ return bit;
+}
+
+fz_bitmap *
+fz_keep_bitmap(fz_context *ctx, fz_bitmap *bit)
+{
+ if (bit)
+ bit->refs++;
+ return bit;
+}
+
+void
+fz_drop_bitmap(fz_context *ctx, fz_bitmap *bit)
+{
+ if (bit && --bit->refs == 0)
+ {
+ fz_free(ctx, bit->samples);
+ fz_free(ctx, bit);
+ }
+}
+
+void
+fz_clear_bitmap(fz_context *ctx, fz_bitmap *bit)
+{
+ memset(bit->samples, 0, bit->stride * bit->h);
+}
+
+/*
+ * Write bitmap to PBM file
+ */
+
+void
+fz_write_pbm(fz_context *ctx, fz_bitmap *bitmap, char *filename)
+{
+ FILE *fp;
+ unsigned char *p;
+ int h, bytestride;
+
+ fp = fopen(filename, "wb");
+ if (!fp)
+ fz_throw(ctx, FZ_ERROR_GENERIC, "cannot open file '%s': %s", filename, strerror(errno));
+
+ assert(bitmap->n == 1);
+
+ fprintf(fp, "P4\n%d %d\n", bitmap->w, bitmap->h);
+
+ p = bitmap->samples;
+
+ h = bitmap->h;
+ bytestride = (bitmap->w + 7) >> 3;
+ while (h--)
+ {
+ fwrite(p, 1, bytestride, fp);
+ p += bitmap->stride;
+ }
+
+ fclose(fp);
+}
+
+fz_colorspace *fz_pixmap_colorspace(fz_context *ctx, fz_pixmap *pix)
+{
+ if (!pix)
+ return NULL;
+ return pix->colorspace;
+}
+
+int fz_pixmap_components(fz_context *ctx, fz_pixmap *pix)
+{
+ if (!pix)
+ return 0;
+ return pix->n;
+}
+
+unsigned char *fz_pixmap_samples(fz_context *ctx, fz_pixmap *pix)
+{
+ if (!pix)
+ return NULL;
+ return pix->samples;
+}
+
+void fz_bitmap_details(fz_bitmap *bit, int *w, int *h, int *n, int *stride)
+{
+ if (!bit)
+ {
+ if (w)
+ *w = 0;
+ if (h)
+ *h = 0;
+ if (n)
+ *n = 0;
+ if (stride)
+ *stride = 0;
+ return;
+ }
+ if (w)
+ *w = bit->w;
+ if (h)
+ *h = bit->h;
+ if (n)
+ *n = bit->n;
+ if (stride)
+ *stride = bit->stride;
+}