hpdf 2.0.8

Sign up to get free protection for your applications and to get access to all the features.
Files changed (101) hide show
  1. data/History +57 -0
  2. data/License +25 -0
  3. data/README +165 -0
  4. data/examples/arc_demo.rb +82 -0
  5. data/examples/demo.rb +92 -0
  6. data/examples/encryption.rb +41 -0
  7. data/examples/ext_gstater_demo.rb +171 -0
  8. data/examples/font_demo.rb +67 -0
  9. data/examples/jpfont_demo.rb +122 -0
  10. data/examples/line_demo.rb +301 -0
  11. data/examples/slide_show_demo.rb +139 -0
  12. data/examples/text_demo2.rb +189 -0
  13. data/examples/ttfont_demo.rb +106 -0
  14. data/ext/extconf.rb +6 -0
  15. data/ext/hpdf.c +3779 -0
  16. data/ext/hpdf_annotation.c +415 -0
  17. data/ext/hpdf_array.c +344 -0
  18. data/ext/hpdf_binary.c +117 -0
  19. data/ext/hpdf_boolean.c +47 -0
  20. data/ext/hpdf_catalog.c +354 -0
  21. data/ext/hpdf_destination.c +339 -0
  22. data/ext/hpdf_dict.c +488 -0
  23. data/ext/hpdf_doc.c +2020 -0
  24. data/ext/hpdf_doc_png.c +137 -0
  25. data/ext/hpdf_encoder.c +2991 -0
  26. data/ext/hpdf_encoder_cns.c +36359 -0
  27. data/ext/hpdf_encoder_cnt.c +15307 -0
  28. data/ext/hpdf_encoder_jp.c +16125 -0
  29. data/ext/hpdf_encoder_kr.c +27978 -0
  30. data/ext/hpdf_encrypt.c +632 -0
  31. data/ext/hpdf_encryptdict.c +240 -0
  32. data/ext/hpdf_error.c +114 -0
  33. data/ext/hpdf_ext_gstate.c +150 -0
  34. data/ext/hpdf_font.c +229 -0
  35. data/ext/hpdf_font_cid.c +1030 -0
  36. data/ext/hpdf_font_tt.c +406 -0
  37. data/ext/hpdf_font_type1.c +391 -0
  38. data/ext/hpdf_fontdef.c +56 -0
  39. data/ext/hpdf_fontdef_base14.c +4506 -0
  40. data/ext/hpdf_fontdef_cid.c +194 -0
  41. data/ext/hpdf_fontdef_cns.c +471 -0
  42. data/ext/hpdf_fontdef_cnt.c +250 -0
  43. data/ext/hpdf_fontdef_jp.c +1904 -0
  44. data/ext/hpdf_fontdef_kr.c +1572 -0
  45. data/ext/hpdf_fontdef_tt.c +2230 -0
  46. data/ext/hpdf_fontdef_type1.c +524 -0
  47. data/ext/hpdf_gstate.c +119 -0
  48. data/ext/hpdf_image.c +568 -0
  49. data/ext/hpdf_image_png.c +424 -0
  50. data/ext/hpdf_info.c +164 -0
  51. data/ext/hpdf_list.c +333 -0
  52. data/ext/hpdf_mmgr.c +243 -0
  53. data/ext/hpdf_name.c +71 -0
  54. data/ext/hpdf_null.c +31 -0
  55. data/ext/hpdf_number.c +49 -0
  56. data/ext/hpdf_objects.c +172 -0
  57. data/ext/hpdf_outline.c +329 -0
  58. data/ext/hpdf_page_label.c +74 -0
  59. data/ext/hpdf_page_operator.c +2764 -0
  60. data/ext/hpdf_pages.c +1508 -0
  61. data/ext/hpdf_real.c +61 -0
  62. data/ext/hpdf_streams.c +1435 -0
  63. data/ext/hpdf_string.c +189 -0
  64. data/ext/hpdf_utils.c +438 -0
  65. data/ext/hpdf_xref.c +348 -0
  66. data/ext/include/hpdf.h +1227 -0
  67. data/ext/include/hpdf_annotation.h +74 -0
  68. data/ext/include/hpdf_catalog.h +81 -0
  69. data/ext/include/hpdf_conf.h +76 -0
  70. data/ext/include/hpdf_consts.h +549 -0
  71. data/ext/include/hpdf_destination.h +41 -0
  72. data/ext/include/hpdf_doc.h +159 -0
  73. data/ext/include/hpdf_encoder.h +311 -0
  74. data/ext/include/hpdf_encrypt.h +156 -0
  75. data/ext/include/hpdf_encryptdict.h +66 -0
  76. data/ext/include/hpdf_error.h +201 -0
  77. data/ext/include/hpdf_ext_gstate.h +38 -0
  78. data/ext/include/hpdf_font.h +112 -0
  79. data/ext/include/hpdf_fontdef.h +403 -0
  80. data/ext/include/hpdf_gstate.h +80 -0
  81. data/ext/include/hpdf_image.h +72 -0
  82. data/ext/include/hpdf_info.h +48 -0
  83. data/ext/include/hpdf_list.h +85 -0
  84. data/ext/include/hpdf_mmgr.h +82 -0
  85. data/ext/include/hpdf_objects.h +587 -0
  86. data/ext/include/hpdf_outline.h +74 -0
  87. data/ext/include/hpdf_page_label.h +35 -0
  88. data/ext/include/hpdf_pages.h +128 -0
  89. data/ext/include/hpdf_streams.h +276 -0
  90. data/ext/include/hpdf_types.h +488 -0
  91. data/ext/include/hpdf_utils.h +161 -0
  92. data/tests/arc_demo.rb +82 -0
  93. data/tests/demo.rb +91 -0
  94. data/tests/encryption.rb +41 -0
  95. data/tests/ext_gstater_demo.rb +171 -0
  96. data/tests/font_demo.rb +67 -0
  97. data/tests/line_demo.rb +301 -0
  98. data/tests/slide_show_demo.rb +139 -0
  99. data/tests/test_all_examples.rb +31 -0
  100. data/tests/text_demo2.rb +189 -0
  101. metadata +147 -0
@@ -0,0 +1,119 @@
1
+ /*
2
+ * << Haru Free PDF Library 2.0.0 >> -- hpdf_gstate.c
3
+ *
4
+ * Copyright (c) 1999-2006 Takeshi Kanno <takeshi_kanno@est.hi-ho.ne.jp>
5
+ *
6
+ * Permission to use, copy, modify, distribute and sell this software
7
+ * and its documentation for any purpose is hereby granted without fee,
8
+ * provided that the above copyright notice appear in all copies and
9
+ * that both that copyright notice and this permission notice appear
10
+ * in supporting documentation.
11
+ * It is provided "as is" without express or implied warranty.
12
+ *
13
+ */
14
+
15
+ #include "hpdf_conf.h"
16
+ #include "hpdf_utils.h"
17
+ #include "hpdf_gstate.h"
18
+
19
+ HPDF_GState
20
+ HPDF_GState_New (HPDF_MMgr mmgr,
21
+ HPDF_GState current)
22
+ {
23
+ HPDF_GState gstate;
24
+
25
+ if (current && current->depth >= HPDF_LIMIT_MAX_GSTATE) {
26
+ HPDF_SetError (mmgr->error, HPDF_EXCEED_GSTATE_LIMIT, 0);
27
+
28
+ return NULL;
29
+ }
30
+
31
+ gstate = HPDF_GetMem (mmgr, sizeof(HPDF_GState_Rec));
32
+ if (!gstate)
33
+ return NULL;
34
+
35
+ if (current) {
36
+ gstate->trans_matrix = current->trans_matrix;
37
+ gstate->line_width = current->line_width;
38
+ gstate->line_cap = current->line_cap;
39
+ gstate->line_join = current->line_join;
40
+ gstate->miter_limit = current->miter_limit;
41
+ gstate->dash_mode = current->dash_mode;
42
+ gstate->flatness = current->flatness;
43
+
44
+ gstate->char_space = current->char_space;
45
+ gstate->word_space = current->word_space;
46
+ gstate->h_scalling = current->h_scalling;
47
+ gstate->text_leading = current->text_leading;
48
+ gstate->rendering_mode = current->rendering_mode;
49
+ gstate->text_rise = current->text_rise;
50
+
51
+ gstate->cs_stroke = current->cs_stroke;
52
+ gstate->cs_fill = current->cs_fill;
53
+ gstate->rgb_fill = current->rgb_fill;
54
+ gstate->rgb_stroke = current->rgb_stroke;
55
+ gstate->cmyk_fill = current->cmyk_fill;
56
+ gstate->cmyk_stroke = current->cmyk_stroke;
57
+ gstate->gray_fill = current->gray_fill;
58
+ gstate->gray_stroke = current->gray_stroke;
59
+
60
+ gstate->font = current->font;
61
+ gstate->font_size = current->font_size;
62
+
63
+ gstate->prev = current;
64
+ gstate->depth = current->depth + 1;
65
+ } else {
66
+ HPDF_TransMatrix DEF_MATRIX = {1, 0, 0, 1, 0, 0};
67
+ HPDF_RGBColor DEF_RGB_COLOR = {0, 0, 0};
68
+ HPDF_CMYKColor DEF_CMYK_COLOR = {0, 0, 0, 0};
69
+ HPDF_DashMode DEF_DASH_MODE = {{0, 0, 0, 0, 0, 0, 0, 0}, 0, 0};
70
+
71
+ gstate->trans_matrix = DEF_MATRIX;
72
+ gstate->line_width = HPDF_DEF_LINEWIDTH;
73
+ gstate->line_cap = HPDF_DEF_LINECAP;
74
+ gstate->line_join = HPDF_DEF_LINEJOIN;
75
+ gstate->miter_limit = HPDF_DEF_MITERLIMIT;
76
+ gstate->dash_mode = DEF_DASH_MODE;
77
+ gstate->flatness = HPDF_DEF_FLATNESS;
78
+
79
+ gstate->char_space = HPDF_DEF_CHARSPACE;
80
+ gstate->word_space = HPDF_DEF_WORDSPACE;
81
+ gstate->h_scalling = HPDF_DEF_HSCALING;
82
+ gstate->text_leading = HPDF_DEF_LEADING;
83
+ gstate->rendering_mode = HPDF_DEF_RENDERING_MODE;
84
+ gstate->text_rise = HPDF_DEF_RISE;
85
+
86
+ gstate->cs_stroke = HPDF_CS_DEVICE_GRAY;
87
+ gstate->cs_fill = HPDF_CS_DEVICE_GRAY;
88
+ gstate->rgb_fill = DEF_RGB_COLOR;
89
+ gstate->rgb_stroke = DEF_RGB_COLOR;
90
+ gstate->cmyk_fill = DEF_CMYK_COLOR;
91
+ gstate->cmyk_stroke = DEF_CMYK_COLOR;
92
+ gstate->gray_fill = 0;
93
+ gstate->gray_stroke = 0;
94
+
95
+ gstate->font = NULL;
96
+ gstate->font_size = 0;
97
+ gstate->writing_mode = HPDF_WMODE_HORIZONTAL;
98
+
99
+ gstate->prev = NULL;
100
+ gstate->depth = 1;
101
+ }
102
+
103
+ return gstate;
104
+ }
105
+
106
+ HPDF_GState
107
+ HPDF_GState_Free (HPDF_MMgr mmgr,
108
+ HPDF_GState gstate)
109
+ {
110
+ HPDF_GState current = NULL;
111
+
112
+ if (gstate) {
113
+ current = gstate->prev;
114
+ HPDF_FreeMem (mmgr, gstate);
115
+ }
116
+
117
+ return current;
118
+ }
119
+
@@ -0,0 +1,568 @@
1
+ /*
2
+ * << Haru Free PDF Library 2.0.3 >> -- hpdf_image.c
3
+ *
4
+ * Copyright (c) 1999-2006 Takeshi Kanno <takeshi_kanno@est.hi-ho.ne.jp>
5
+ *
6
+ * Permission to use, copy, modify, distribute and sell this software
7
+ * and its documentation for any purpose is hereby granted without fee,
8
+ * provided that the above copyright notice appear in all copies and
9
+ * that both that copyright notice and this permission notice appear
10
+ * in supporting documentation.
11
+ * It is provided "as is" without express or implied warranty.
12
+ *
13
+ * 2006.08.12 modified.
14
+ */
15
+
16
+ #include "hpdf_conf.h"
17
+ #include "hpdf_utils.h"
18
+ #include "hpdf.h"
19
+
20
+ static const char *COL_CMYK = "DeviceCMYK";
21
+ static const char *COL_RGB = "DeviceRGB";
22
+ static const char *COL_GRAY = "DeviceGray";
23
+
24
+ static HPDF_STATUS
25
+ LoadJpegHeader (HPDF_Image image,
26
+ HPDF_Stream stream);
27
+
28
+
29
+ /*---------------------------------------------------------------------------*/
30
+
31
+ static HPDF_STATUS
32
+ LoadJpegHeader (HPDF_Image image,
33
+ HPDF_Stream stream)
34
+ {
35
+ HPDF_UINT16 tag;
36
+ HPDF_UINT16 height;
37
+ HPDF_UINT16 width;
38
+ HPDF_BYTE precision;
39
+ HPDF_BYTE num_components;
40
+ const char *color_space_name;
41
+ HPDF_UINT len;
42
+ HPDF_STATUS ret;
43
+ HPDF_Array array;
44
+
45
+ HPDF_PTRACE ((" HPDF_Image_LoadJpegHeader\n"));
46
+
47
+ len = 2;
48
+ if (HPDF_Stream_Read (stream, (HPDF_BYTE *)&tag, &len) != HPDF_OK)
49
+ return HPDF_Error_GetCode (stream->error);
50
+
51
+ HPDF_UInt16Swap (&tag);
52
+ if (tag != 0xFFD8)
53
+ return HPDF_INVALID_JPEG_DATA;
54
+
55
+ /* find SOF record */
56
+ for (;;) {
57
+ HPDF_UINT16 size;
58
+
59
+ len = 2;
60
+ if (HPDF_Stream_Read (stream, (HPDF_BYTE *)&tag, &len) != HPDF_OK)
61
+ return HPDF_Error_GetCode (stream->error);
62
+
63
+ HPDF_UInt16Swap (&tag);
64
+
65
+ len = 2;
66
+ if (HPDF_Stream_Read (stream, (HPDF_BYTE *)&size, &len) != HPDF_OK)
67
+ return HPDF_Error_GetCode (stream->error);
68
+
69
+ HPDF_UInt16Swap (&size);
70
+
71
+ HPDF_PTRACE (("tag=%04X size=%u\n", tag, size));
72
+
73
+ if (tag == 0xFFC0 || tag == 0xFFC1 ||
74
+ tag == 0xFFC2 || tag == 0xFFC9) {
75
+
76
+ len = 1;
77
+ if (HPDF_Stream_Read (stream, (HPDF_BYTE *)&precision, &len) !=
78
+ HPDF_OK)
79
+ return HPDF_Error_GetCode (stream->error);
80
+
81
+ len = 2;
82
+ if (HPDF_Stream_Read (stream, (HPDF_BYTE *)&height, &len) !=
83
+ HPDF_OK)
84
+ return HPDF_Error_GetCode (stream->error);
85
+
86
+ HPDF_UInt16Swap (&height);
87
+
88
+ len = 2;
89
+ if (HPDF_Stream_Read (stream, (HPDF_BYTE *)&width, &len) != HPDF_OK)
90
+ return HPDF_Error_GetCode (stream->error);
91
+
92
+ HPDF_UInt16Swap (&width);
93
+
94
+ len = 1;
95
+ if (HPDF_Stream_Read (stream, (HPDF_BYTE *)&num_components, &len) !=
96
+ HPDF_OK)
97
+ return HPDF_Error_GetCode (stream->error);
98
+
99
+ break;
100
+ } else if ((tag | 0x00FF) != 0xFFFF)
101
+ /* lost marker */
102
+ return HPDF_SetError (image->error, HPDF_UNSUPPORTED_JPEG_FORMAT,
103
+ 0);
104
+
105
+ if (HPDF_Stream_Seek (stream, size - 2, HPDF_SEEK_CUR) != HPDF_OK)
106
+ return HPDF_Error_GetCode (stream->error);
107
+ }
108
+
109
+ if (HPDF_Dict_AddNumber (image, "Height", height) != HPDF_OK)
110
+ return HPDF_Error_GetCode (stream->error);
111
+
112
+ if (HPDF_Dict_AddNumber (image, "Width", width) != HPDF_OK)
113
+ return HPDF_Error_GetCode (stream->error);
114
+
115
+ /* classification of RGB and CMYK is less than perfect
116
+ * YCbCr and YCCK are classified into RGB or CMYK.
117
+ *
118
+ * It is necessary to read APP14 data to distinguish colorspace perfectly.
119
+
120
+ */
121
+ switch (num_components) {
122
+ case 1:
123
+ color_space_name = COL_GRAY;
124
+ break;
125
+ case 3:
126
+ color_space_name = COL_RGB;
127
+ break;
128
+ case 4:
129
+ array = HPDF_Array_New (image->mmgr);
130
+ if (!array)
131
+ return HPDF_Error_GetCode (stream->error);
132
+
133
+ ret = HPDF_Dict_Add (image, "Decode", array);
134
+ if (ret != HPDF_OK)
135
+ return HPDF_Error_GetCode (stream->error);
136
+
137
+ ret += HPDF_Array_Add (array, HPDF_Number_New (image->mmgr, 1));
138
+ ret += HPDF_Array_Add (array, HPDF_Number_New (image->mmgr, 0));
139
+ ret += HPDF_Array_Add (array, HPDF_Number_New (image->mmgr, 1));
140
+ ret += HPDF_Array_Add (array, HPDF_Number_New (image->mmgr, 0));
141
+ ret += HPDF_Array_Add (array, HPDF_Number_New (image->mmgr, 1));
142
+ ret += HPDF_Array_Add (array, HPDF_Number_New (image->mmgr, 0));
143
+ ret += HPDF_Array_Add (array, HPDF_Number_New (image->mmgr, 1));
144
+ ret += HPDF_Array_Add (array, HPDF_Number_New (image->mmgr, 0));
145
+
146
+ if (ret != HPDF_OK)
147
+ return HPDF_Error_GetCode (stream->error);
148
+
149
+ color_space_name = COL_CMYK;
150
+
151
+ break;
152
+ default:
153
+ return HPDF_SetError (image->error, HPDF_UNSUPPORTED_JPEG_FORMAT,
154
+ 0);
155
+ }
156
+
157
+ if (HPDF_Dict_Add (image, "ColorSpace",
158
+ HPDF_Name_New (image->mmgr, color_space_name)) != HPDF_OK)
159
+ return HPDF_Error_GetCode (stream->error);
160
+
161
+ if (HPDF_Dict_Add (image, "BitsPerComponent",
162
+ HPDF_Number_New (image->mmgr, precision)) != HPDF_OK)
163
+ return HPDF_Error_GetCode (stream->error);
164
+
165
+ return HPDF_OK;
166
+ }
167
+
168
+ HPDF_Image
169
+ HPDF_Image_LoadJpegImage (HPDF_MMgr mmgr,
170
+ HPDF_Stream jpeg_data,
171
+ HPDF_Xref xref)
172
+ {
173
+ HPDF_Dict image;
174
+ HPDF_STATUS ret = HPDF_OK;
175
+
176
+ HPDF_PTRACE ((" HPDF_Image_LoadJpegImage\n"));
177
+
178
+ image = HPDF_DictStream_New (mmgr, xref);
179
+ if (!image)
180
+ return NULL;
181
+
182
+ image->header.obj_class |= HPDF_OSUBCLASS_XOBJECT;
183
+
184
+ /* add requiered elements */
185
+ image->filter = HPDF_STREAM_FILTER_DCT_DECODE;
186
+ ret += HPDF_Dict_AddName (image, "Type", "XObject");
187
+ ret += HPDF_Dict_AddName (image, "Subtype", "Image");
188
+ if (ret != HPDF_OK)
189
+ return NULL;
190
+
191
+ if (LoadJpegHeader (image, jpeg_data) != HPDF_OK)
192
+ return NULL;
193
+
194
+ if (HPDF_Stream_Seek (jpeg_data, 0, HPDF_SEEK_SET) != HPDF_OK)
195
+ return NULL;
196
+
197
+ for (;;) {
198
+ HPDF_BYTE buf[HPDF_STREAM_BUF_SIZ];
199
+ HPDF_UINT len = HPDF_STREAM_BUF_SIZ;
200
+ HPDF_STATUS ret = HPDF_Stream_Read (jpeg_data, buf,
201
+ &len);
202
+
203
+ if (ret != HPDF_OK) {
204
+ if (ret == HPDF_STREAM_EOF) {
205
+ if (len > 0) {
206
+ ret = HPDF_Stream_Write (image->stream, buf, len);
207
+ if (ret != HPDF_OK)
208
+ return NULL;
209
+ }
210
+ break;
211
+ } else
212
+ return NULL;
213
+ }
214
+
215
+ if (HPDF_Stream_Write (image->stream, buf, len) != HPDF_OK)
216
+ return NULL;
217
+ }
218
+
219
+ return image;
220
+ }
221
+
222
+
223
+ HPDF_Image
224
+ HPDF_Image_LoadRawImage (HPDF_MMgr mmgr,
225
+ HPDF_Stream raw_data,
226
+ HPDF_Xref xref,
227
+ HPDF_UINT width,
228
+ HPDF_UINT height,
229
+ HPDF_ColorSpace color_space)
230
+ {
231
+ HPDF_Dict image;
232
+ HPDF_STATUS ret = HPDF_OK;
233
+ HPDF_UINT size;
234
+
235
+ HPDF_PTRACE ((" HPDF_Image_LoadRawImage\n"));
236
+
237
+ if (color_space != HPDF_CS_DEVICE_GRAY &&
238
+ color_space != HPDF_CS_DEVICE_RGB &&
239
+ color_space != HPDF_CS_DEVICE_CMYK) {
240
+ HPDF_SetError (mmgr->error, HPDF_INVALID_COLOR_SPACE, 0);
241
+ return NULL;
242
+ }
243
+
244
+ image = HPDF_DictStream_New (mmgr, xref);
245
+ if (!image)
246
+ return NULL;
247
+
248
+ image->header.obj_class |= HPDF_OSUBCLASS_XOBJECT;
249
+ ret += HPDF_Dict_AddName (image, "Type", "XObject");
250
+ ret += HPDF_Dict_AddName (image, "Subtype", "Image");
251
+ if (ret != HPDF_OK)
252
+ return NULL;
253
+
254
+ if (color_space == HPDF_CS_DEVICE_GRAY) {
255
+ size = width * height;
256
+ ret = HPDF_Dict_AddName (image, "ColorSpace", COL_GRAY);
257
+ } else if (color_space == HPDF_CS_DEVICE_CMYK) {
258
+ size = width * height * 4;
259
+ ret = HPDF_Dict_AddName (image, "ColorSpace", COL_CMYK);
260
+ } else {
261
+ size = width * height * 3;
262
+ ret = HPDF_Dict_AddName (image, "ColorSpace", COL_RGB);
263
+ }
264
+
265
+ if (ret != HPDF_OK)
266
+ return NULL;
267
+
268
+ if (HPDF_Dict_AddNumber (image, "Width", width) != HPDF_OK)
269
+ return NULL;
270
+
271
+ if (HPDF_Dict_AddNumber (image, "Height", height) != HPDF_OK)
272
+ return NULL;
273
+
274
+ if (HPDF_Dict_AddNumber (image, "BitsPerComponent", 8) != HPDF_OK)
275
+ return NULL;
276
+
277
+ if (HPDF_Stream_WriteToStream (raw_data, image->stream, 0, NULL) != HPDF_OK)
278
+ return NULL;
279
+
280
+ if (image->stream->size != size) {
281
+ HPDF_SetError (image->error, HPDF_INVALID_IMAGE, 0);
282
+ return NULL;
283
+ }
284
+
285
+ return image;
286
+ }
287
+
288
+
289
+ HPDF_Image
290
+ HPDF_Image_LoadRawImageFromMem (HPDF_MMgr mmgr,
291
+ const HPDF_BYTE *buf,
292
+ HPDF_Xref xref,
293
+ HPDF_UINT width,
294
+ HPDF_UINT height,
295
+ HPDF_ColorSpace color_space,
296
+ HPDF_UINT bits_per_component)
297
+ {
298
+ HPDF_Dict image;
299
+ HPDF_STATUS ret = HPDF_OK;
300
+ HPDF_UINT size;
301
+
302
+ HPDF_PTRACE ((" HPDF_Image_LoadRawImageFromMem\n"));
303
+
304
+ if (color_space != HPDF_CS_DEVICE_GRAY &&
305
+ color_space != HPDF_CS_DEVICE_RGB) {
306
+ HPDF_SetError (mmgr->error, HPDF_INVALID_COLOR_SPACE, 0);
307
+ return NULL;
308
+ }
309
+
310
+ if (bits_per_component != 1 && bits_per_component != 2 &&
311
+ bits_per_component != 4 && bits_per_component != 8) {
312
+ HPDF_SetError (mmgr->error, HPDF_INVALID_IMAGE, 0);
313
+ return NULL;
314
+ }
315
+
316
+ image = HPDF_DictStream_New (mmgr, xref);
317
+ if (!image)
318
+ return NULL;
319
+
320
+ image->header.obj_class |= HPDF_OSUBCLASS_XOBJECT;
321
+ ret += HPDF_Dict_AddName (image, "Type", "XObject");
322
+ ret += HPDF_Dict_AddName (image, "Subtype", "Image");
323
+ if (ret != HPDF_OK)
324
+ return NULL;
325
+
326
+ if (color_space == HPDF_CS_DEVICE_GRAY) {
327
+ size = (HPDF_DOUBLE)width * height / (8 / bits_per_component) + 0.876;
328
+ ret = HPDF_Dict_AddName (image, "ColorSpace", COL_GRAY);
329
+ } else {
330
+ size = (HPDF_DOUBLE)width * height / (8 / bits_per_component) + 0.876;
331
+ size *= 3;
332
+ ret = HPDF_Dict_AddName (image, "ColorSpace", COL_RGB);
333
+ }
334
+
335
+ if (ret != HPDF_OK)
336
+ return NULL;
337
+
338
+ if (HPDF_Dict_AddNumber (image, "Width", width) != HPDF_OK)
339
+ return NULL;
340
+
341
+ if (HPDF_Dict_AddNumber (image, "Height", height) != HPDF_OK)
342
+ return NULL;
343
+
344
+ if (HPDF_Dict_AddNumber (image, "BitsPerComponent", bits_per_component)
345
+ != HPDF_OK)
346
+ return NULL;
347
+
348
+ if (HPDF_Stream_Write (image->stream, buf, size) != HPDF_OK)
349
+ return NULL;
350
+
351
+ return image;
352
+ }
353
+
354
+
355
+ HPDF_BOOL
356
+ HPDF_Image_Validate (HPDF_Image image)
357
+ {
358
+ HPDF_Name subtype;
359
+
360
+ HPDF_PTRACE ((" HPDF_Image_Validate\n"));
361
+
362
+ if (!image)
363
+ return HPDF_FALSE;
364
+
365
+ if (image->header.obj_class != (HPDF_OSUBCLASS_XOBJECT |
366
+ HPDF_OCLASS_DICT)) {
367
+ HPDF_RaiseError (image->error, HPDF_INVALID_IMAGE, 0);
368
+ return HPDF_FALSE;
369
+ }
370
+
371
+ subtype = HPDF_Dict_GetItem (image, "Subtype", HPDF_OCLASS_NAME);
372
+ if (!subtype || HPDF_StrCmp (subtype->value, "Image") != 0) {
373
+ HPDF_RaiseError (image->error, HPDF_INVALID_IMAGE, 0);
374
+ return HPDF_FALSE;
375
+ }
376
+
377
+ return HPDF_TRUE;
378
+ }
379
+
380
+
381
+ HPDF_EXPORT(HPDF_Point)
382
+ HPDF_Image_GetSize (HPDF_Image image)
383
+ {
384
+ HPDF_Number width;
385
+ HPDF_Number height;
386
+ HPDF_Point ret = {0, 0};
387
+
388
+ HPDF_PTRACE ((" HPDF_Image_GetSize\n"));
389
+
390
+ if (!HPDF_Image_Validate (image))
391
+ return ret;
392
+
393
+ width = HPDF_Dict_GetItem (image, "Width", HPDF_OCLASS_NUMBER);
394
+ height = HPDF_Dict_GetItem (image, "Height", HPDF_OCLASS_NUMBER);
395
+
396
+ if (width && height) {
397
+ ret.x = width->value;
398
+ ret.y = height->value;
399
+ }
400
+
401
+ return ret;
402
+ }
403
+
404
+ HPDF_EXPORT(HPDF_STATUS)
405
+ HPDF_Image_GetSize2 (HPDF_Image image, HPDF_Point *size)
406
+ {
407
+ HPDF_Number width;
408
+ HPDF_Number height;
409
+ size->x = 0;
410
+ size->y = 0;
411
+
412
+ HPDF_PTRACE ((" HPDF_Image_GetSize\n"));
413
+
414
+ if (!HPDF_Image_Validate (image))
415
+ return HPDF_INVALID_IMAGE;
416
+
417
+ width = HPDF_Dict_GetItem (image, "Width", HPDF_OCLASS_NUMBER);
418
+ height = HPDF_Dict_GetItem (image, "Height", HPDF_OCLASS_NUMBER);
419
+
420
+ if (width && height) {
421
+ size->x = width->value;
422
+ size->y = height->value;
423
+ }
424
+
425
+ return HPDF_OK;
426
+ }
427
+
428
+ HPDF_EXPORT(HPDF_UINT)
429
+ HPDF_Image_GetBitsPerComponent (HPDF_Image image)
430
+ {
431
+ HPDF_Number n;
432
+
433
+ HPDF_PTRACE ((" HPDF_Image_GetBitsPerComponent\n"));
434
+
435
+ if (!HPDF_Image_Validate (image))
436
+ return 0;
437
+
438
+ n = HPDF_Dict_GetItem (image, "BitsPerComponent", HPDF_OCLASS_NUMBER);
439
+
440
+ if (!n)
441
+ return 0;
442
+
443
+ return n->value;
444
+ }
445
+
446
+ HPDF_EXPORT(const char*)
447
+ HPDF_Image_GetColorSpace (HPDF_Image image)
448
+ {
449
+ HPDF_Name n;
450
+
451
+ HPDF_PTRACE ((" HPDF_Image_GetColorSpace\n"));
452
+
453
+ n = HPDF_Dict_GetItem (image, "ColorSpace", HPDF_OCLASS_NAME);
454
+
455
+ if (!n) {
456
+ HPDF_CheckError (image->error);
457
+ return NULL;
458
+ }
459
+
460
+ return n->value;
461
+ }
462
+
463
+ HPDF_EXPORT(HPDF_UINT)
464
+ HPDF_Image_GetWidth (HPDF_Image image)
465
+ {
466
+ return HPDF_Image_GetSize (image).x;
467
+ }
468
+
469
+ HPDF_EXPORT(HPDF_UINT)
470
+ HPDF_Image_GetHeight (HPDF_Image image)
471
+ {
472
+ return HPDF_Image_GetSize (image).y;
473
+ }
474
+
475
+ HPDF_STATUS
476
+ HPDF_Image_SetMask (HPDF_Image image,
477
+ HPDF_BOOL mask)
478
+ {
479
+ HPDF_Boolean image_mask;
480
+
481
+ if (!HPDF_Image_Validate (image))
482
+ return HPDF_INVALID_IMAGE;
483
+
484
+ if (mask && HPDF_Image_GetBitsPerComponent (image) != 1)
485
+ return HPDF_SetError (image->error, HPDF_INVALID_BIT_PER_COMPONENT,
486
+ 0);
487
+
488
+ image_mask = HPDF_Dict_GetItem (image, "ImageMask", HPDF_OCLASS_BOOLEAN);
489
+ if (!image_mask) {
490
+ HPDF_STATUS ret;
491
+ image_mask = HPDF_Boolean_New (image->mmgr, HPDF_FALSE);
492
+
493
+ if ((ret = HPDF_Dict_Add (image, "ImageMask", image_mask)) != HPDF_OK)
494
+ return ret;
495
+ }
496
+
497
+ image_mask->value = mask;
498
+ return HPDF_OK;
499
+ }
500
+
501
+
502
+ HPDF_EXPORT(HPDF_STATUS)
503
+ HPDF_Image_SetMaskImage (HPDF_Image image,
504
+ HPDF_Image mask_image)
505
+ {
506
+ if (!HPDF_Image_Validate (image))
507
+ return HPDF_INVALID_IMAGE;
508
+
509
+ if (!HPDF_Image_Validate (mask_image))
510
+ return HPDF_INVALID_IMAGE;
511
+
512
+ if (HPDF_Image_SetMask (mask_image, HPDF_TRUE) != HPDF_OK)
513
+ return HPDF_CheckError (image->error);
514
+
515
+ return HPDF_Dict_Add (image, "Mask", mask_image);
516
+ }
517
+
518
+
519
+ HPDF_EXPORT(HPDF_STATUS)
520
+ HPDF_Image_SetColorMask (HPDF_Image image,
521
+ HPDF_UINT rmin,
522
+ HPDF_UINT rmax,
523
+ HPDF_UINT gmin,
524
+ HPDF_UINT gmax,
525
+ HPDF_UINT bmin,
526
+ HPDF_UINT bmax)
527
+ {
528
+ HPDF_Array array;
529
+ const char *name;
530
+ HPDF_STATUS ret = HPDF_OK;
531
+
532
+ if (!HPDF_Image_Validate (image))
533
+ return HPDF_INVALID_IMAGE;
534
+
535
+ if (HPDF_Dict_GetItem (image, "ImageMask", HPDF_OCLASS_BOOLEAN))
536
+ return HPDF_RaiseError (image->error, HPDF_INVALID_OPERATION, 0);
537
+
538
+ if (HPDF_Image_GetBitsPerComponent (image) != 8)
539
+ return HPDF_RaiseError (image->error, HPDF_INVALID_BIT_PER_COMPONENT,
540
+ 0);
541
+
542
+ name = HPDF_Image_GetColorSpace (image);
543
+ if (!name || HPDF_StrCmp (COL_RGB, name) != 0)
544
+ return HPDF_RaiseError (image->error, HPDF_INVALID_COLOR_SPACE, 0);
545
+
546
+ /* Each integer must be in the range 0 to 2^BitsPerComponent - 1 */
547
+ if (rmax > 255 || gmax > 255 || bmax > 255)
548
+ return HPDF_RaiseError (image->error, HPDF_INVALID_PARAMETER, 0);
549
+
550
+ array = HPDF_Array_New (image->mmgr);
551
+ if (!array)
552
+ return HPDF_CheckError (image->error);
553
+
554
+ ret += HPDF_Dict_Add (image, "Mask", array);
555
+ ret += HPDF_Array_AddNumber (array, rmin);
556
+ ret += HPDF_Array_AddNumber (array, rmax);
557
+ ret += HPDF_Array_AddNumber (array, gmin);
558
+ ret += HPDF_Array_AddNumber (array, gmax);
559
+ ret += HPDF_Array_AddNumber (array, bmin);
560
+ ret += HPDF_Array_AddNumber (array, bmax);
561
+
562
+ if (ret != HPDF_OK)
563
+ return HPDF_CheckError (image->error);
564
+
565
+ return HPDF_OK;
566
+ }
567
+
568
+