1#include "fitz-imp.h"
2
3/* This code needs to be kept out of stm_buffer.c to avoid it being
4 * pulled into cmapdump.c */
5
6void
7fz_drop_compressed_buffer(fz_context *ctx, fz_compressed_buffer *buf)
8{
9 if (buf)
10 {
11 if (buf->params.type == FZ_IMAGE_JBIG2)
12 fz_drop_jbig2_globals(ctx, buf->params.u.jbig2.globals);
13 fz_drop_buffer(ctx, buf->buffer);
14 fz_free(ctx, buf);
15 }
16}
17
18fz_stream *
19fz_open_image_decomp_stream_from_buffer(fz_context *ctx, fz_compressed_buffer *buffer, int *l2factor)
20{
21 fz_stream *head, *tail;
22
23 tail = fz_open_buffer(ctx, buffer->buffer);
24 fz_try(ctx)
25 head = fz_open_image_decomp_stream(ctx, tail, &buffer->params, l2factor);
26 fz_always(ctx)
27 fz_drop_stream(ctx, tail);
28 fz_catch(ctx)
29 fz_rethrow(ctx);
30 return head;
31}
32
33fz_stream *
34fz_open_image_decomp_stream(fz_context *ctx, fz_stream *tail, fz_compression_params *params, int *l2factor)
35{
36 fz_stream *head = NULL, *body = NULL;
37 int our_l2factor = 0;
38
39 fz_var(body);
40
41 fz_try(ctx)
42 {
43 switch (params->type)
44 {
45 default:
46 head = fz_keep_stream(ctx, tail);
47 break;
48
49 case FZ_IMAGE_FAX:
50 head = fz_open_faxd(ctx, tail,
51 params->u.fax.k,
52 params->u.fax.end_of_line,
53 params->u.fax.encoded_byte_align,
54 params->u.fax.columns,
55 params->u.fax.rows,
56 params->u.fax.end_of_block,
57 params->u.fax.black_is_1);
58 break;
59
60 case FZ_IMAGE_JPEG:
61 if (l2factor)
62 {
63 our_l2factor = *l2factor;
64 if (our_l2factor > 3)
65 our_l2factor = 3;
66 *l2factor -= our_l2factor;
67 }
68 head = fz_open_dctd(ctx, tail, params->u.jpeg.color_transform, our_l2factor, NULL);
69 break;
70
71 case FZ_IMAGE_JBIG2:
72 head = fz_open_jbig2d(ctx, tail, params->u.jbig2.globals);
73 break;
74
75 case FZ_IMAGE_RLD:
76 head = fz_open_rld(ctx, tail);
77 break;
78
79 case FZ_IMAGE_FLATE:
80 head = fz_open_flated(ctx, tail, 15);
81 if (params->u.flate.predictor > 1)
82 {
83 body = head;
84 head = fz_open_predict(ctx, body,
85 params->u.flate.predictor,
86 params->u.flate.columns,
87 params->u.flate.colors,
88 params->u.flate.bpc);
89 }
90 break;
91
92 case FZ_IMAGE_LZW:
93 head = fz_open_lzwd(ctx, tail, params->u.lzw.early_change, 9, 0, 0);
94 if (params->u.flate.predictor > 1)
95 {
96 body = head;
97 head = fz_open_predict(ctx, body,
98 params->u.lzw.predictor,
99 params->u.lzw.columns,
100 params->u.lzw.colors,
101 params->u.lzw.bpc);
102 }
103 break;
104 }
105 }
106 fz_always(ctx)
107 fz_drop_stream(ctx, body);
108 fz_catch(ctx)
109 fz_rethrow(ctx);
110
111 return head;
112}
113
114fz_stream *
115fz_open_compressed_buffer(fz_context *ctx, fz_compressed_buffer *buffer)
116{
117 return fz_open_image_decomp_stream_from_buffer(ctx, buffer, NULL);
118}
119
120size_t
121fz_compressed_buffer_size(fz_compressed_buffer *buffer)
122{
123 if (buffer && buffer->buffer)
124 return (size_t)buffer->buffer->cap;
125 return 0;
126}
127