From 80d6490e6d54f822de6d36219ce08e6a8ad33137 Mon Sep 17 00:00:00 2001 From: Robin Watts Date: Wed, 28 Sep 2016 22:25:16 -0400 Subject: Hide internals of fz_colorspace The implementation does not need to be in the public API. --- source/fitz/colorspace-impl.h | 16 ++++++++++++ source/fitz/colorspace.c | 59 ++++++++++++++++++++++++++----------------- source/fitz/draw-device.c | 12 ++++----- source/fitz/draw-mesh.c | 10 +++++--- source/fitz/image.c | 2 +- source/fitz/list-device.c | 28 ++++++++++---------- source/fitz/load-jpx.c | 2 +- source/fitz/load-pnm.c | 15 ++++++----- source/fitz/pixmap.c | 10 ++++---- source/fitz/shade.c | 9 ++++--- source/fitz/stext-device.c | 2 +- source/fitz/trace-device.c | 7 ++--- source/pdf/pdf-colorspace.c | 11 ++++---- source/pdf/pdf-device.c | 15 ++++++----- source/pdf/pdf-image.c | 10 +++++--- source/pdf/pdf-interpret.c | 2 +- source/pdf/pdf-op-run.c | 13 ++++++---- source/pdf/pdf-shade.c | 29 +++++++++++---------- source/tools/murun.c | 14 ++++++---- source/xps/xps-common.c | 3 ++- 20 files changed, 161 insertions(+), 108 deletions(-) create mode 100644 source/fitz/colorspace-impl.h (limited to 'source') diff --git a/source/fitz/colorspace-impl.h b/source/fitz/colorspace-impl.h new file mode 100644 index 00000000..1ce2e167 --- /dev/null +++ b/source/fitz/colorspace-impl.h @@ -0,0 +1,16 @@ +#ifndef MUPDF_FITZ_COLORSPACE_IMPL_H +#define MUPDF_FITZ_COLORSPACE_IMPL_H + +struct fz_colorspace_s +{ + fz_storable storable; + size_t size; + char name[16]; + int n; + fz_colorspace_convert_fn *to_rgb; + fz_colorspace_convert_fn *from_rgb; + fz_colorspace_destruct_fn *free_data; + void *data; +}; + +#endif diff --git a/source/fitz/colorspace.c b/source/fitz/colorspace.c index 90539860..0e8ca594 100644 --- a/source/fitz/colorspace.c +++ b/source/fitz/colorspace.c @@ -1,5 +1,7 @@ #include "mupdf/fitz.h" +#include "colorspace-impl.h" + #define SLOWCMYK void @@ -13,17 +15,17 @@ fz_drop_colorspace_imp(fz_context *ctx, fz_storable *cs_) } fz_colorspace * -fz_new_colorspace(fz_context *ctx, char *name, int n) +fz_new_colorspace(fz_context *ctx, char *name, int n, fz_colorspace_convert_fn *to_rgb, fz_colorspace_convert_fn *from_rgb, fz_colorspace_destruct_fn *destruct, void *data, size_t size) { fz_colorspace *cs = fz_malloc_struct(ctx, fz_colorspace); FZ_INIT_STORABLE(cs, 1, fz_drop_colorspace_imp); - cs->size = sizeof(fz_colorspace); + cs->size = sizeof(fz_colorspace) + size; fz_strlcpy(cs->name, name, sizeof cs->name); cs->n = n; - cs->to_rgb = NULL; - cs->from_rgb = NULL; - cs->free_data = NULL; - cs->data = NULL; + cs->to_rgb = to_rgb; + cs->from_rgb = from_rgb; + cs->free_data = destruct; + cs->data = data; return cs; } @@ -207,6 +209,12 @@ rgb_to_lab(fz_context *ctx, fz_colorspace *cs, const float *rgb, float *lab) lab[2] = rgb[2]; } +int +fz_colorspace_is_lab(fz_context *ctx, fz_colorspace *cs) +{ + return (cs && cs->to_rgb == lab_to_rgb); +} + static fz_colorspace k_default_gray = { {-1, fz_drop_colorspace_imp}, 0, "DeviceGray", 1, gray_to_rgb, rgb_to_gray }; static fz_colorspace k_default_rgb = { {-1, fz_drop_colorspace_imp}, 0, "DeviceRGB", 3, rgb_to_rgb, rgb_to_rgb }; static fz_colorspace k_default_bgr = { {-1, fz_drop_colorspace_imp}, 0, "DeviceBGR", 3, bgr_to_rgb, rgb_to_bgr }; @@ -327,18 +335,6 @@ fz_set_device_lab(fz_context *ctx, fz_colorspace *cs) fz_unlock(ctx, FZ_LOCK_ALLOC); } -int -fz_colorspace_is_indexed(fz_context *ctx, fz_colorspace *cs) -{ - return (cs && !strcmp(cs->name, "Indexed")); -} - -int -fz_colorspace_is_lab(fz_context *ctx, fz_colorspace *cs) -{ - return (cs && !strcmp(cs->name, "Lab")); -} - /* Fast pixmap color conversions */ static void fast_gray_to_rgb(fz_pixmap *dst, fz_pixmap *src) @@ -2003,6 +1999,12 @@ free_indexed(fz_context *ctx, fz_colorspace *cs) fz_free(ctx, idx); } +int +fz_colorspace_is_indexed(fz_context *ctx, fz_colorspace *cs) +{ + return (cs && cs->to_rgb == indexed_to_rgb); +} + fz_colorspace * fz_new_indexed_colorspace(fz_context *ctx, fz_colorspace *base, int high, unsigned char *lookup) { @@ -2016,11 +2018,7 @@ fz_new_indexed_colorspace(fz_context *ctx, fz_colorspace *base, int high, unsign fz_try(ctx) { - cs = fz_new_colorspace(ctx, "Indexed", 1); - cs->to_rgb = indexed_to_rgb; - cs->free_data = free_indexed; - cs->data = idx; - cs->size += sizeof(*idx) + (base->n * (idx->high + 1)) + base->size; + cs = fz_new_colorspace(ctx, "Indexed", 1, indexed_to_rgb, NULL, free_indexed, idx, sizeof(*idx) + (base->n * (idx->high + 1)) + base->size); } fz_catch(ctx) { @@ -2172,3 +2170,18 @@ void fz_fin_cached_color_converter(fz_context *ctx, fz_color_converter *cc_) fz_drop_hash(ctx, cc->hash); fz_free(ctx, cc); } + +int fz_colorspace_is(fz_context *ctx, const fz_colorspace *cs, fz_colorspace_convert_fn *to_rgb) +{ + return cs && cs->to_rgb == to_rgb; +} + +int fz_colorspace_n(fz_context *ctx, const fz_colorspace *cs) +{ + return cs ? cs->n : 0; +} + +const char *fz_colorspace_name(fz_context *ctx, const fz_colorspace *cs) +{ + return cs ? cs->name : ""; +} diff --git a/source/fitz/draw-device.c b/source/fitz/draw-device.c index e337c482..e40b61db 100644 --- a/source/fitz/draw-device.c +++ b/source/fitz/draw-device.c @@ -439,7 +439,7 @@ fz_draw_fill_path(fz_context *ctx, fz_device *devp, const fz_path *path, int eve if (state->blendmode & FZ_BLEND_KNOCKOUT) state = fz_knockout_begin(ctx, dev); - n = model ? model->n : 0; + n = fz_colorspace_n(ctx, model); if (n > 0) { fz_convert_color(ctx, model, colorfv, colorspace, color); @@ -507,7 +507,7 @@ fz_draw_stroke_path(fz_context *ctx, fz_device *devp, const fz_path *path, const if (state->blendmode & FZ_BLEND_KNOCKOUT) state = fz_knockout_begin(ctx, dev); - n = model ? model->n : 0; + n = fz_colorspace_n(ctx, model); if (n > 0) { fz_convert_color(ctx, model, colorfv, colorspace, color); @@ -789,7 +789,7 @@ fz_draw_fill_text(fz_context *ctx, fz_device *devp, const fz_text *text, const f if (state->blendmode & FZ_BLEND_KNOCKOUT) state = fz_knockout_begin(ctx, dev); - n = model ? model->n : 0; + n = fz_colorspace_n(ctx, model); if (n > 0) { fz_convert_color(ctx, model, colorfv, colorspace, color); @@ -880,7 +880,7 @@ fz_draw_stroke_text(fz_context *ctx, fz_device *devp, const fz_text *text, const if (state->blendmode & FZ_BLEND_KNOCKOUT) state = fz_knockout_begin(ctx, dev); - n = model ? model->n : 0; + n = fz_colorspace_n(ctx, model); if (n > 0) { fz_convert_color(ctx, model, colorfv, colorspace, color); @@ -1238,7 +1238,7 @@ fz_draw_fill_shade(fz_context *ctx, fz_device *devp, fz_shade *shade, const fz_m { unsigned char *s; int x, y, n, i; - n = model ? model->n : 0; + n = fz_colorspace_n(ctx, model); if (n > 0) { fz_convert_color(ctx, model, colorfv, shade->colorspace, shade->background); @@ -1586,7 +1586,7 @@ fz_draw_fill_image_mask(fz_context *ctx, fz_device *devp, fz_image *image, const pixmap = scaled; } - n = model ? model->n : 0; + n = fz_colorspace_n(ctx, model); if (n > 0) { fz_convert_color(ctx, model, colorfv, colorspace, color); diff --git a/source/fitz/draw-mesh.c b/source/fitz/draw-mesh.c index d7606f1b..4f90c461 100644 --- a/source/fitz/draw-mesh.c +++ b/source/fitz/draw-mesh.c @@ -179,8 +179,9 @@ prepare_mesh_vertex(fz_context *ctx, void *arg, fz_vertex *v, const float *input output[0] = input[0] * 255; else { + int n = fz_colorspace_n(ctx, dest->colorspace); ptd->cc.convert(ctx, &ptd->cc, output, input); - for (i = 0; i < dest->colorspace->n; i++) + for (i = 0; i < n; i++) output[i] *= 255; } } @@ -197,7 +198,7 @@ do_paint_tri(fz_context *ctx, void *arg, fz_vertex *av, fz_vertex *bv, fz_vertex vertices[2] = (float *)cv; dest = ptd->dest; - fz_paint_triangle(dest, vertices, 2 + dest->colorspace->n, ptd->bbox); + fz_paint_triangle(dest, vertices, 2 + fz_colorspace_n(ctx, dest->colorspace), ptd->bbox); } void @@ -221,14 +222,15 @@ fz_paint_shade(fz_context *ctx, fz_shade *shade, const fz_matrix *ctm, fz_pixmap if (shade->use_function) { fz_color_converter cc; - n = dest->colorspace ? dest->colorspace->n : 0; + int cn = fz_colorspace_n(ctx, shade->colorspace); + n = fz_colorspace_n(ctx, dest->colorspace); fz_lookup_color_converter(ctx, &cc, dest->colorspace, shade->colorspace); for (i = 0; i < 256; i++) { cc.convert(ctx, &cc, color, shade->function[i]); for (k = 0; k < n; k++) clut[i][k] = color[k] * 255; - clut[i][k] = shade->function[i][shade->colorspace->n] * 255; + clut[i][k] = shade->function[i][cn] * 255; } /* We need to use alpha = 1 here, because the shade might not fill * the bbox. */ diff --git a/source/fitz/image.c b/source/fitz/image.c index 8d692fbd..77344a16 100644 --- a/source/fitz/image.c +++ b/source/fitz/image.c @@ -784,7 +784,7 @@ fz_new_image(fz_context *ctx, int w, int h, int bpc, fz_colorspace *colorspace, image->xres = xres; image->yres = yres; image->bpc = bpc; - image->n = (colorspace ? colorspace->n : 1); + image->n = (colorspace ? fz_colorspace_n(ctx, colorspace) : 1); image->colorspace = fz_keep_colorspace(ctx, colorspace); image->invert_cmyk_jpeg = 1; image->interpolate = interpolate; diff --git a/source/fitz/list-device.c b/source/fitz/list-device.c index 3d17ee7d..bcc11230 100644 --- a/source/fitz/list-device.c +++ b/source/fitz/list-device.c @@ -302,7 +302,7 @@ fz_append_display_node( else { int i; - int n = colorspace ? colorspace->n : 0; + int n = fz_colorspace_n(ctx, colorspace); colorspace_off = size; size += SIZE_IN_NODES(sizeof(fz_colorspace *)); @@ -373,7 +373,7 @@ fz_append_display_node( else { int i; - int n = colorspace ? colorspace->n : 0; + int n = fz_colorspace_n(ctx, colorspace); for (i=0; i < n; i++) if (color[i] != 0.0f) break; @@ -393,7 +393,7 @@ fz_append_display_node( const float *wc = &writer->color[0]; assert(colorspace != NULL); - n = colorspace->n; + n = fz_colorspace_n(ctx, colorspace); i = 0; /* Only check colors if the colorspace is unchanged. If the * colorspace *has* changed and the colors are implicit then @@ -564,7 +564,7 @@ fz_append_display_node( { fz_colorspace **out_colorspace = (fz_colorspace **)(void *)(&node_ptr[colorspace_off]); int i, n; - n = colorspace ? colorspace->n : 0; + n = fz_colorspace_n(ctx, colorspace); *out_colorspace = fz_keep_colorspace(ctx, colorspace); writer->colorspace = fz_keep_colorspace(ctx, colorspace); @@ -576,9 +576,10 @@ fz_append_display_node( } if (color_off) { + int n = fz_colorspace_n(ctx, colorspace); float *out_color = (float *)(void *)(&node_ptr[color_off]); - memcpy(writer->color, color, colorspace->n * sizeof(float)); - memcpy(out_color, color, colorspace->n * sizeof(float)); + memcpy(writer->color, color, n * sizeof(float)); + memcpy(out_color, color, n * sizeof(float)); } if (node.alpha) { @@ -1260,6 +1261,7 @@ fz_drop_display_list_imp(fz_context *ctx, fz_storable *list_) fz_display_node *node = list->list; fz_display_node *node_end = list->list + list->len; int cs_n = 1; + fz_colorspace *cs; if (list == NULL) return; @@ -1291,10 +1293,9 @@ fz_drop_display_list_imp(fz_context *ctx, fz_storable *list_) cs_n = 4; break; case CS_OTHER_0: - cs_n = 0; - if (*(fz_colorspace **)node) - cs_n = (*(fz_colorspace **)node)->n; - fz_drop_colorspace(ctx, *(fz_colorspace **)node); + cs = *(fz_colorspace **)node; + cs_n = fz_colorspace_n(ctx, cs); + fz_drop_colorspace(ctx, cs); node += SIZE_IN_NODES(sizeof(fz_colorspace *)); break; } @@ -1484,7 +1485,7 @@ fz_run_display_list(fz_context *ctx, fz_display_list *list, fz_device *dev, cons case CS_OTHER_0: colorspace = fz_keep_colorspace(ctx, *(fz_colorspace **)(node)); node += SIZE_IN_NODES(sizeof(fz_colorspace *)); - en = colorspace ? colorspace->n : 0; + en = fz_colorspace_n(ctx, colorspace); for (i = 0; i < en; i++) color[i] = 0.0f; break; @@ -1492,8 +1493,9 @@ fz_run_display_list(fz_context *ctx, fz_display_list *list, fz_device *dev, cons } if (n.color) { - memcpy(color, (float *)node, colorspace->n * sizeof(float)); - node += SIZE_IN_NODES(colorspace->n * sizeof(float)); + int nc = fz_colorspace_n(ctx, colorspace); + memcpy(color, (float *)node, nc * sizeof(float)); + node += SIZE_IN_NODES(nc * sizeof(float)); } if (n.alpha) { diff --git a/source/fitz/load-jpx.c b/source/fitz/load-jpx.c index a7775fc7..48b65217 100644 --- a/source/fitz/load-jpx.c +++ b/source/fitz/load-jpx.c @@ -777,7 +777,7 @@ jpx_read_image(fz_context *ctx, unsigned char *data, size_t size, fz_colorspace if (defcs) { - if (defcs->n == n) + if (fz_colorspace_n(ctx, defcs) == n) { colorspace = defcs; } diff --git a/source/fitz/load-pnm.c b/source/fitz/load-pnm.c index 6a330f98..c140938c 100644 --- a/source/fitz/load-pnm.c +++ b/source/fitz/load-pnm.c @@ -192,9 +192,10 @@ pnm_ascii_read_image(fz_context *ctx, struct info *pnm, unsigned char *p, unsign } else { + int n = fz_colorspace_n(ctx, img->colorspace); for (y = 0; y < pnm->height; y++) for (x = 0; x < pnm->width; x++) - for (k = 0; k < img->colorspace->n; k++) + for (k = 0; k < n; k++) { int v = 0; p = pnm_read_number(ctx, p, e, &v); @@ -262,25 +263,26 @@ pnm_binary_read_image(fz_context *ctx, struct info *pnm, unsigned char *p, unsig } else { + int n = fz_colorspace_n(ctx, img->colorspace); if (pnm->maxval == 255) { for (y = 0; y < pnm->height; y++) for (x = 0; x < pnm->width; x++) - for (k = 0; k < img->colorspace->n; k++) + for (k = 0; k < n; k++) *dp++ = *p++; } else if (pnm->maxval < 256) { for (y = 0; y < pnm->height; y++) for (x = 0; x < pnm->width; x++) - for (k = 0; k < img->colorspace->n; k++) + for (k = 0; k < n; k++) *dp++ = map_color(ctx, *p++, pnm->maxval, 255); } else { for (y = 0; y < pnm->height; y++) for (x = 0; x < pnm->width; x++) - for (k = 0; k < img->colorspace->n; k++) + for (k = 0; k < n; k++) { *dp++ = map_color(ctx, (p[0] << 8) | p[1], pnm->maxval, 255); p += 2; @@ -427,10 +429,11 @@ pam_binary_read_image(fz_context *ctx, struct info *pnm, unsigned char *p, unsig if (!onlymeta) { unsigned char *dp; - int x, y, k; + int x, y, k, n; img = fz_new_pixmap(ctx, pnm->cs, pnm->width, pnm->height, pnm->alpha); dp = img->samples; + n = fz_colorspace_n(ctx, img->colorspace); if (bitmap) { @@ -439,7 +442,7 @@ pam_binary_read_image(fz_context *ctx, struct info *pnm, unsigned char *p, unsig for (y = 0; y < pnm->height; y++) for (x = 0; x < pnm->width; x++) - for (k = 0; k < img->colorspace->n; k++) + for (k = 0; k < n; k++) { if (*p) *dp = 0x00; diff --git a/source/fitz/pixmap.c b/source/fitz/pixmap.c index f888792d..1ced3b53 100644 --- a/source/fitz/pixmap.c +++ b/source/fitz/pixmap.c @@ -33,7 +33,7 @@ fz_new_pixmap_with_data(fz_context *ctx, fz_colorspace *colorspace, int w, int h if (w < 0 || h < 0) fz_throw(ctx, FZ_ERROR_GENERIC, "Illegal dimensions for pixmap %d %d", w, h); - n = alpha + (colorspace ? colorspace->n : 0); + n = alpha + fz_colorspace_n(ctx, colorspace); if (stride < n*w && stride > -n*w) fz_throw(ctx, FZ_ERROR_GENERIC, "Illegal stride for pixmap (n=%d w=%d, stride=%d)", n, w, stride); if (samples == NULL && stride < n*w) @@ -91,7 +91,7 @@ fz_new_pixmap_with_data(fz_context *ctx, fz_colorspace *colorspace, int w, int h fz_pixmap * fz_new_pixmap(fz_context *ctx, fz_colorspace *colorspace, int w, int h, int alpha) { - int stride = ((colorspace ? colorspace->n : 0) + alpha) * w; + int stride = (fz_colorspace_n(ctx, colorspace) + alpha) * w; return fz_new_pixmap_with_data(ctx, colorspace, w, h, alpha, stride, NULL); } @@ -109,7 +109,7 @@ fz_pixmap * fz_new_pixmap_with_bbox_and_data(fz_context *ctx, fz_colorspace *colorspace, const fz_irect *r, int alpha, unsigned char *samples) { int w = r->x1 - r->x0; - int stride = ((colorspace ? colorspace->n : 0) + alpha) * w; + int stride = (fz_colorspace_n(ctx, colorspace) + alpha) * w; fz_pixmap *pixmap = fz_new_pixmap_with_data(ctx, colorspace, w, r->y1 - r->y0, alpha, stride, samples); pixmap->x = r->x0; pixmap->y = r->y0; @@ -441,7 +441,7 @@ fz_clear_pixmap_with_value(fz_context *ctx, fz_pixmap *pix, int value) return; /* CMYK needs special handling (and potentially any other subtractive colorspaces) */ - if (pix->colorspace && pix->colorspace->n == 4) + if (fz_colorspace_n(ctx, pix->colorspace) == 4) { clear_cmyk_bitmap(pix->samples, w, h, pix->stride, 255-value, pix->alpha); return; @@ -611,7 +611,7 @@ fz_clear_pixmap_rect_with_value(fz_context *ctx, fz_pixmap *dest, int value, con destp = dest->samples + (unsigned int)(destspan * (local_b.y0 - dest->y) + dest->n * (local_b.x0 - dest->x)); /* CMYK needs special handling (and potentially any other subtractive colorspaces) */ - if (dest->colorspace && dest->colorspace->n == 4) + if (fz_colorspace_n(ctx, dest->colorspace) == 4) { value = 255 - value; do diff --git a/source/fitz/shade.c b/source/fitz/shade.c index 1b1f3371..8f82eb72 100644 --- a/source/fitz/shade.c +++ b/source/fitz/shade.c @@ -84,7 +84,7 @@ fz_process_mesh_type1(fz_context *ctx, fz_shade *shade, const fz_matrix *ctm, fz fz_vertex vs[2][2]; fz_vertex *v = vs[0]; fz_vertex *vn = vs[1]; - int n = shade->colorspace->n; + int n = fz_colorspace_n(ctx, shade->colorspace); fz_matrix local_ctm; fz_concat(&local_ctm, &shade->u.f.matrix, ctm); @@ -931,7 +931,7 @@ fz_process_mesh(fz_context *ctx, fz_shade *shade, const fz_matrix *ctm, painter.prepare = prepare; painter.process = process; painter.process_arg = process_arg; - painter.ncomp = (shade->use_function > 0 ? 1 : shade->colorspace->n); + painter.ncomp = (shade->use_function > 0 ? 1 : fz_colorspace_n(ctx, shade->colorspace)); if (shade->type == FZ_FUNCTION_BASED) fz_process_mesh_type1(ctx, shade, ctm, &painter); @@ -1105,7 +1105,7 @@ fz_print_shade(fz_context *ctx, fz_output *out, fz_shade *shade) shade->bbox.x0, shade->bbox.y0, shade->bbox.x1, shade->bbox.y1); - fz_printf(ctx, out, "\tcolorspace %s\n", shade->colorspace->name); + fz_printf(ctx, out, "\tcolorspace %s\n", fz_colorspace_name(ctx, shade->colorspace)); fz_printf(ctx, out, "\tmatrix [%g %g %g %g %g %g]\n", shade->matrix.a, shade->matrix.b, shade->matrix.c, @@ -1113,8 +1113,9 @@ fz_print_shade(fz_context *ctx, fz_output *out, fz_shade *shade) if (shade->use_background) { + int n = fz_colorspace_n(ctx, shade->colorspace); fz_printf(ctx, out, "\tbackground ["); - for (i = 0; i < shade->colorspace->n; i++) + for (i = 0; i < n; i++) fz_printf(ctx, out, "%s%g", i == 0 ? "" : " ", shade->background[i]); fz_printf(ctx, out, "]\n"); } diff --git a/source/fitz/stext-device.c b/source/fitz/stext-device.c index 88a9f669..007a6a2a 100644 --- a/source/fitz/stext-device.c +++ b/source/fitz/stext-device.c @@ -942,7 +942,7 @@ fz_stext_fill_image_mask(fz_context *ctx, fz_device *dev, fz_image *img, const f block->image = fz_keep_image(ctx, img); block->cspace = fz_keep_colorspace(ctx, cspace); if (cspace) - memcpy(block->colors, color, sizeof(block->colors[0])*cspace->n); + memcpy(block->colors, color, sizeof(block->colors[0])*fz_colorspace_n(ctx, cspace)); block->mat = *ctm; block->bbox.x0 = 0; block->bbox.y0 = 0; diff --git a/source/fitz/trace-device.c b/source/fitz/trace-device.c index 86e059b5..bcb68bf8 100644 --- a/source/fitz/trace-device.c +++ b/source/fitz/trace-device.c @@ -16,11 +16,12 @@ fz_trace_matrix(fz_context *ctx, fz_output *out, const fz_matrix *ctm) static void fz_trace_color(fz_context *ctx, fz_output *out, fz_colorspace *colorspace, const float *color, float alpha) { - int i; + int i, n; if (colorspace) { - fz_printf(ctx, out, " colorspace=\"%s\" color=\"", colorspace->name); - for (i = 0; i < colorspace->n; i++) + n = fz_colorspace_n(ctx, colorspace); + fz_printf(ctx, out, " colorspace=\"%s\" color=\"", fz_colorspace_name(ctx, colorspace)); + for (i = 0; i < n; i++) fz_printf(ctx, out, "%s%g", i == 0 ? "" : " ", color[i]); fz_printf(ctx, out, "\""); } diff --git a/source/pdf/pdf-colorspace.c b/source/pdf/pdf-colorspace.c index b9cb26e9..05c54b31 100644 --- a/source/pdf/pdf-colorspace.c +++ b/source/pdf/pdf-colorspace.c @@ -1,5 +1,7 @@ #include "mupdf/pdf.h" +#include "../fitz/colorspace-impl.h" + /* ICCBased */ static fz_colorspace * @@ -106,11 +108,8 @@ load_separation(fz_context *ctx, pdf_document *doc, pdf_obj *array) sep->base = base; sep->tint = tint; - cs = fz_new_colorspace(ctx, n == 1 ? "Separation" : "DeviceN", n); - cs->to_rgb = separation_to_rgb; - cs->free_data = free_separation; - cs->data = sep; - cs->size += sizeof(struct separation) + (base ? base->size : 0) + fz_function_size(ctx, tint); + cs = fz_new_colorspace(ctx, n == 1 ? "Separation" : "DeviceN", n, separation_to_rgb, NULL, free_separation, sep, + sizeof(struct separation) + (base ? base->size : 0) + fz_function_size(ctx, tint)); } fz_catch(ctx) { @@ -126,7 +125,7 @@ load_separation(fz_context *ctx, pdf_document *doc, pdf_obj *array) int pdf_is_tint_colorspace(fz_context *ctx, fz_colorspace *cs) { - return cs && cs->to_rgb == separation_to_rgb; + return fz_colorspace_is(ctx, cs, separation_to_rgb); } /* Indexed */ diff --git a/source/pdf/pdf-device.c b/source/pdf/pdf-device.c index 30ae76b5..ccf63e42 100644 --- a/source/pdf/pdf-device.c +++ b/source/pdf/pdf-device.c @@ -241,6 +241,7 @@ pdf_dev_color(fz_context *ctx, pdf_device *pdev, fz_colorspace *colorspace, cons fz_convert_color(ctx, fz_device_rgb(ctx), rgb, colorspace, color); color = rgb; colorspace = fz_device_rgb(ctx); + cspace = 3; } if (gs->colorspace[stroke] != colorspace) @@ -249,7 +250,7 @@ pdf_dev_color(fz_context *ctx, pdf_device *pdev, fz_colorspace *colorspace, cons diff = 1; } - for (i=0; i < colorspace->n; i++) + for (i=0; i < cspace; i++) if (gs->color[stroke][i] != color[i]) { gs->color[stroke][i] = color[i]; @@ -564,15 +565,16 @@ pdf_dev_new_form(fz_context *ctx, pdf_obj **form_ref, pdf_device *pdev, const fz group = pdf_new_dict(ctx, doc, 5); fz_try(ctx) { + int n = fz_colorspace_n(ctx, colorspace); pdf_dict_put_drop(ctx, group, PDF_NAME_Type, PDF_NAME_Group); pdf_dict_put_drop(ctx, group, PDF_NAME_S, PDF_NAME_Transparency); pdf_dict_put_drop(ctx, group, PDF_NAME_K, pdf_new_bool(ctx, doc, knockout)); pdf_dict_put_drop(ctx, group, PDF_NAME_I, pdf_new_bool(ctx, doc, isolated)); - if (!colorspace) + if (n == 0) {} - else if (colorspace->n == 1) + if (n == 1) pdf_dict_put_drop(ctx, group, PDF_NAME_CS, PDF_NAME_DeviceGray); - else if (colorspace->n == 4) + else if (n == 4) pdf_dict_put_drop(ctx, group, PDF_NAME_CS, PDF_NAME_DeviceCMYK); else pdf_dict_put_drop(ctx, group, PDF_NAME_CS, PDF_NAME_DeviceRGB); @@ -907,12 +909,13 @@ pdf_dev_begin_mask(fz_context *ctx, fz_device *dev, const fz_rect *bbox, int lum fz_try(ctx) { + int n = fz_colorspace_n(ctx, colorspace); smask = pdf_new_dict(ctx, doc, 4); pdf_dict_put_drop(ctx, smask, PDF_NAME_Type, PDF_NAME_Mask); pdf_dict_put_drop(ctx, smask, PDF_NAME_S, (luminosity ? PDF_NAME_Luminosity : PDF_NAME_Alpha)); pdf_dict_put(ctx, smask, PDF_NAME_G, form_ref); - color_obj = pdf_new_array(ctx, doc, colorspace->n); - for (i = 0; i < colorspace->n; i++) + color_obj = pdf_new_array(ctx, doc, n); + for (i = 0; i < n; i++) pdf_array_push_drop(ctx, color_obj, pdf_new_real(ctx, doc, color[i])); pdf_dict_put_drop(ctx, smask, PDF_NAME_BC, color_obj); color_obj = NULL; diff --git a/source/pdf/pdf-image.c b/source/pdf/pdf-image.c index d54de34c..fa1d7675 100644 --- a/source/pdf/pdf-image.c +++ b/source/pdf/pdf-image.c @@ -99,7 +99,7 @@ pdf_load_image_imp(fz_context *ctx, pdf_document *doc, pdf_obj *rdb, pdf_obj *di colorspace = pdf_load_colorspace(ctx, doc, obj); indexed = fz_colorspace_is_indexed(ctx, colorspace); - n = colorspace->n; + n = fz_colorspace_n(ctx, colorspace); } else { @@ -296,6 +296,7 @@ pdf_add_image(fz_context *ctx, pdf_document *doc, fz_image *image, int mask) fz_buffer *buffer = NULL; fz_colorspace *colorspace = image->colorspace; unsigned char digest[16]; + int n; /* If we can maintain compression, do so */ cbuffer = fz_compressed_image_buffer(ctx, image); @@ -370,14 +371,15 @@ pdf_add_image(fz_context *ctx, pdf_document *doc, fz_image *image, int mask) pdf_dict_put_drop(ctx, imobj, PDF_NAME_Subtype, PDF_NAME_Image); pdf_dict_put_drop(ctx, imobj, PDF_NAME_Width, pdf_new_int(ctx, doc, image->w)); pdf_dict_put_drop(ctx, imobj, PDF_NAME_Height, pdf_new_int(ctx, doc, image->h)); + n = fz_colorspace_n(ctx, colorspace); if (mask) { } - else if (!colorspace || colorspace->n == 1) + else if (n <= 1) pdf_dict_put_drop(ctx, imobj, PDF_NAME_ColorSpace, PDF_NAME_DeviceGray); - else if (colorspace->n == 3) + else if (n == 3) pdf_dict_put_drop(ctx, imobj, PDF_NAME_ColorSpace, PDF_NAME_DeviceRGB); - else if (colorspace->n == 4) + else if (n == 4) pdf_dict_put_drop(ctx, imobj, PDF_NAME_ColorSpace, PDF_NAME_DeviceCMYK); if (!mask) pdf_dict_put_drop(ctx, imobj, PDF_NAME_BitsPerComponent, pdf_new_int(ctx, doc, image->bpc)); diff --git a/source/pdf/pdf-interpret.c b/source/pdf/pdf-interpret.c index 0ae4844a..28a02f13 100644 --- a/source/pdf/pdf-interpret.c +++ b/source/pdf/pdf-interpret.c @@ -461,7 +461,7 @@ pdf_process_extgstate(fz_context *ctx, pdf_processor *proc, pdf_csi *csi, pdf_ob colorspace = pdf_xobject_colorspace(ctx, xobj); if (colorspace) { - colorspace_n = colorspace->n; + colorspace_n = fz_colorspace_n(ctx, colorspace); fz_drop_colorspace(ctx, colorspace); } diff --git a/source/pdf/pdf-op-run.c b/source/pdf/pdf-op-run.c index dba3e086..ff3034fe 100644 --- a/source/pdf/pdf-op-run.c +++ b/source/pdf/pdf-op-run.c @@ -1108,6 +1108,7 @@ pdf_set_colorspace(fz_context *ctx, pdf_run_processor *pr, int what, fz_colorspa { pdf_gstate *gstate = pr->gstate + pr->gtop; pdf_material *mat; + int n = fz_colorspace_n(ctx, colorspace); gstate = pdf_flush_text(ctx, pr); @@ -1126,7 +1127,7 @@ pdf_set_colorspace(fz_context *ctx, pdf_run_processor *pr, int what, fz_colorspa if (pdf_is_tint_colorspace(ctx, colorspace)) { int i; - for (i = 0; i < colorspace->n; i++) + for (i = 0; i < n; i++) mat->v[i] = 1.0f; } } @@ -1136,7 +1137,7 @@ pdf_set_color(fz_context *ctx, pdf_run_processor *pr, int what, float *v) { pdf_gstate *gstate = pr->gstate + pr->gtop; pdf_material *mat; - int i; + int i, n; gstate = pdf_flush_text(ctx, pr); @@ -1154,13 +1155,15 @@ pdf_set_color(fz_context *ctx, pdf_run_processor *pr, int what, float *v) } else if (fz_colorspace_is_lab(ctx, mat->colorspace)) { + n = fz_colorspace_n(ctx, mat->colorspace); /* input is in range (0..100, -128..127, -128..127) not (0..1, 0..1, 0..1) */ - for (i = 0; i < mat->colorspace->n; i++) + for (i = 0; i < n; i++) mat->v[i] = fz_clamp(v[i], i ? -128 : 0, i ? 127 : 100); } else { - for (i = 0; i < mat->colorspace->n; i++) + n = fz_colorspace_n(ctx, mat->colorspace); + for (i = 0; i < n; i++) mat->v[i] = fz_clamp(v[i], 0, 1); } break; @@ -1486,7 +1489,7 @@ static void pdf_run_gs_SMask(fz_context *ctx, pdf_processor *proc, pdf_xobject * int cs_n = 1; if (cs) { - cs_n = cs->n; + cs_n = fz_colorspace_n(ctx, cs); fz_drop_colorspace(ctx, cs); } gstate->softmask_ctm = gstate->ctm; diff --git a/source/pdf/pdf-shade.c b/source/pdf/pdf-shade.c index 6e25efa8..daa2363a 100644 --- a/source/pdf/pdf-shade.c +++ b/source/pdf/pdf-shade.c @@ -8,14 +8,15 @@ static void pdf_sample_composite_shade_function(fz_context *ctx, fz_shade *shade, fz_function *func, float t0, float t1) { - int i; + int i, n; float t; + n = fz_colorspace_n(ctx, shade->colorspace); for (i = 0; i < 256; i++) { t = t0 + (i / 255.0f) * (t1 - t0); - fz_eval_function(ctx, func, &t, 1, shade->function[i], shade->colorspace->n); - shade->function[i][shade->colorspace->n] = 1; + fz_eval_function(ctx, func, &t, 1, shade->function[i], n); + shade->function[i][n] = 1; } } @@ -55,6 +56,7 @@ pdf_load_function_based_shading(fz_context *ctx, pdf_document *doc, fz_shade *sh fz_matrix matrix; int xx, yy; float *p; + int n = fz_colorspace_n(ctx, shade->colorspace); x0 = y0 = 0; x1 = y1 = 1; @@ -75,7 +77,7 @@ pdf_load_function_based_shading(fz_context *ctx, pdf_document *doc, fz_shade *sh shade->u.f.matrix = matrix; shade->u.f.xdivs = FUNSEGS; shade->u.f.ydivs = FUNSEGS; - shade->u.f.fn_vals = fz_malloc(ctx, (FUNSEGS+1)*(FUNSEGS+1)*shade->colorspace->n*sizeof(float)); + shade->u.f.fn_vals = fz_malloc(ctx, (FUNSEGS+1)*(FUNSEGS+1)*n*sizeof(float)); shade->u.f.domain[0][0] = x0; shade->u.f.domain[0][1] = y0; shade->u.f.domain[1][0] = x1; @@ -90,8 +92,8 @@ pdf_load_function_based_shading(fz_context *ctx, pdf_document *doc, fz_shade *sh { fv[0] = x0 + (x1 - x0) * xx / FUNSEGS; - fz_eval_function(ctx, func, fv, 2, p, shade->colorspace->n); - p += shade->colorspace->n; + fz_eval_function(ctx, func, fv, 2, p, n); + p += n; } } } @@ -307,7 +309,7 @@ pdf_load_shading_dict(fz_context *ctx, pdf_document *doc, pdf_obj *dict, const f pdf_obj *obj; int funcs = 0; int type = 0; - int i, in, out; + int i, in, out, n; fz_var(shade); fz_var(func); @@ -335,12 +337,13 @@ pdf_load_shading_dict(fz_context *ctx, pdf_document *doc, pdf_obj *dict, const f if (!obj) fz_throw(ctx, FZ_ERROR_GENERIC, "shading colorspace is missing"); shade->colorspace = pdf_load_colorspace(ctx, doc, obj); + n = fz_colorspace_n(ctx, shade->colorspace); obj = pdf_dict_get(ctx, dict, PDF_NAME_Background); if (obj) { shade->use_background = 1; - for (i = 0; i < shade->colorspace->n; i++) + for (i = 0; i < n; i++) shade->background[i] = pdf_to_real(ctx, pdf_array_get(ctx, obj, i)); } @@ -357,7 +360,7 @@ pdf_load_shading_dict(fz_context *ctx, pdf_document *doc, pdf_obj *dict, const f in = 2; else in = 1; - out = shade->colorspace->n; + out = n; func[0] = pdf_load_function(ctx, doc, obj, in, out); if (!func[0]) @@ -366,7 +369,7 @@ pdf_load_shading_dict(fz_context *ctx, pdf_document *doc, pdf_obj *dict, const f else if (pdf_is_array(ctx, obj)) { funcs = pdf_array_len(ctx, obj); - if (funcs != 1 && funcs != shade->colorspace->n) + if (funcs != 1 && funcs != n) { funcs = 0; fz_throw(ctx, FZ_ERROR_GENERIC, "incorrect number of shading functions"); @@ -425,12 +428,12 @@ pdf_load_shading_dict(fz_context *ctx, pdf_document *doc, pdf_obj *dict, const f } static size_t -fz_shade_size(fz_shade *s) +fz_shade_size(fz_context *ctx, fz_shade *s) { if (s == NULL) return 0; if (s->type == FZ_FUNCTION_BASED) - return sizeof(*s) + sizeof(float) * s->u.f.xdivs * s->u.f.ydivs * s->colorspace->n; + return sizeof(*s) + sizeof(float) * s->u.f.xdivs * s->u.f.ydivs * fz_colorspace_n(ctx, s->colorspace); return sizeof(*s) + fz_compressed_buffer_size(s->buffer); } @@ -477,7 +480,7 @@ pdf_load_shading(fz_context *ctx, pdf_document *doc, pdf_obj *dict) shade = pdf_load_shading_dict(ctx, doc, dict, &fz_identity); } - pdf_store_item(ctx, dict, shade, fz_shade_size(shade)); + pdf_store_item(ctx, dict, shade, fz_shade_size(ctx, shade)); return shade; } diff --git a/source/tools/murun.c b/source/tools/murun.c index 9c1b07c1..3bb290fc 100644 --- a/source/tools/murun.c +++ b/source/tools/murun.c @@ -381,9 +381,10 @@ static void ffi_pushcolorspace(js_State *J, fz_colorspace *colorspace) static void ffi_pushcolor(js_State *J, fz_colorspace *colorspace, const float *color, float alpha) { + fz_context *ctx = js_getcontext(J); if (colorspace) { ffi_pushcolorspace(J, colorspace); - ffi_pusharray(J, color, colorspace->n); + ffi_pusharray(J, color, fz_colorspace_n(ctx, colorspace)); } else { js_pushnull(J); js_pushnull(J); @@ -395,9 +396,10 @@ static struct color ffi_tocolor(js_State *J, int idx) { struct color c; int n, i; + fz_context *ctx = js_getcontext(J); c.colorspace = js_touserdata(J, idx, "fz_colorspace"); if (c.colorspace) { - n = c.colorspace->n; + n = fz_colorspace_n(ctx, c.colorspace); for (i=0; i < n; ++i) { js_getindex(J, idx + 1, i); c.color[i] = js_tonumber(J, -1); @@ -862,7 +864,7 @@ js_dev_begin_mask(fz_context *ctx, fz_device *dev, const fz_rect *bbox, int lumi js_pushboolean(J, luminosity); if (colorspace) { ffi_pushcolorspace(J, colorspace); - ffi_pusharray(J, color, colorspace->n); + ffi_pusharray(J, color, fz_colorspace_n(ctx, colorspace)); } else { js_pushnull(J); js_pushnull(J); @@ -1807,13 +1809,15 @@ static void ffi_Annotation_toPixmap(js_State *J) static void ffi_ColorSpace_getNumberOfComponents(js_State *J) { fz_colorspace *colorspace = js_touserdata(J, 0, "fz_colorspace"); - js_pushnumber(J, colorspace->n); + fz_context *ctx = js_getcontext(J); + js_pushnumber(J, fz_colorspace_n(ctx, colorspace)); } static void ffi_ColorSpace_toString(js_State *J) { fz_colorspace *colorspace = js_touserdata(J, 0, "fz_colorspace"); - js_pushstring(J, colorspace->name); + fz_context *ctx = js_getcontext(J); + js_pushstring(J, fz_colorspace_name(ctx, colorspace)); } static void ffi_new_Pixmap(js_State *J) diff --git a/source/xps/xps-common.c b/source/xps/xps-common.c index 104b6c1b..61e0932f 100644 --- a/source/xps/xps-common.c +++ b/source/xps/xps-common.c @@ -323,8 +323,9 @@ void xps_set_color(fz_context *ctx, xps_document *doc, fz_colorspace *colorspace, float *samples) { int i; + int n = fz_colorspace_n(ctx, colorspace); doc->colorspace = colorspace; - for (i = 0; i < colorspace->n; i++) + for (i = 0; i < n; i++) doc->color[i] = samples[i + 1]; doc->alpha = samples[0] * doc->opacity[doc->opacity_top]; } -- cgit v1.2.3