Debian Codegraph @

converter/other/jpeg2000/jpeg2ktopam.c

raw
1 /*****************************************************************************
2 jpeg2kopam
3 ******************************************************************************
4
5 Convert a JPEG-2000 code stream image to a PNM or PAM
6
7 By Bryan Henderson, San Jose CA 2002.10.26
8
9 *****************************************************************************/
10
11 #define _DEFAULT_SOURCE 1 /* New name for SVID & BSD source defines */
12 #define _BSD_SOURCE 1 /* Make sure strdup() is in string.h */
13 #define _XOPEN_SOURCE 500 /* Make sure strdup() is in string.h */
14 /* In 2014.09, this was _XOPEN_SOURCE 600, with a comment saying it was
15 necessary to make <inttypes.h> define int_fast32_t, etc. on AIX.
16 <jasper/jasper.h> does use int_fast32_t and does include <inttypes.h>,
17 but plenty of source files of libjasper do to0, and they did not have
18 _XOPEN_SOURCE 600, so it would seem to be superfluous here too.
19 */
20 #include <string.h>
21
22 #include <jasper/jasper.h>
23
24 #include "pm_c_util.h"
25 #include "pam.h"
26 #include "shhopt.h"
27 #include "nstring.h"
28 #include "mallocvar.h"
29
30 #include "libjasper_compat.h"
31
32
33
34 struct CmdlineInfo {
35 /* All the information the user supplied in the command line,
36 in a form easy for the program to use.
37 */
38 char * inputFilename;
39 unsigned int debuglevel; /* Jasper library debug level */
40 unsigned int verbose;
41 };
42
43
44 static void
45 parseCommandLine(int argc, const char ** argv,
46 struct CmdlineInfo * const cmdlineP) {
47 /*----------------------------------------------------------------------------
48 Note that many of the strings that this function returns in the
49 *cmdlineP structure are actually in the supplied argv array. And
50 sometimes, one of these strings is actually just a suffix of an entry
51 in argv!
52 -----------------------------------------------------------------------------*/
53 optEntry * option_def;
54 optStruct3 opt;
55
56 unsigned int debuglevelSpec;
57
58 unsigned int option_def_index;
59
60 MALLOCARRAY_NOFAIL(option_def, 100);
61
62 option_def_index = 0; /* incremented by OPTENTRY */
63 OPTENT3(0, "verbose", OPT_FLAG, NULL,
64 &cmdlineP->verbose, 0);
65 OPTENT3(0, "debuglevel", OPT_UINT, &cmdlineP->debuglevel,
66 &debuglevelSpec, 0);
67
68 opt.opt_table = option_def;
69 opt.short_allowed = FALSE; /* We have no short (old-fashioned) options */
70 opt.allowNegNum = FALSE; /* We have no parms that are negative numbers */
71
72 pm_optParseOptions3(&argc, (char**)argv, opt, sizeof(opt), 0);
73
74 if (!debuglevelSpec)
75 cmdlineP->debuglevel = 0;
76
77 if (argc - 1 == 0)
78 cmdlineP->inputFilename = strdup("-"); /* he wants stdin */
79 else if (argc - 1 == 1)
80 cmdlineP->inputFilename = strdup(argv[1]);
81 else
82 pm_error("Too many arguments. The only argument accepted\n"
83 "is the input file specification");
84
85 free(option_def);
86 }
87
88
89
90 static void
91 validateJ2k(jas_stream_t * const instreamP) {
92 /*----------------------------------------------------------------------------
93 Abort program with error message if *instreamP is not a JPEG-2000 code
94 stream (JPC) or image file (JP2).
95 -----------------------------------------------------------------------------*/
96 assert(jas_image_lookupfmtbyname("jpc"));
97 assert(jas_image_lookupfmtbyname("jp2"));
98
99 if (jas_image_lookupfmtbyname("jpc")->ops.validate(instreamP) != 0 &&
100 jas_image_lookupfmtbyname("jp2")->ops.validate(instreamP) != 0) {
101
102 pm_error("Input is not JPEG-2000 image file (JP2) "
103 "or code stream (JPC). "
104 "(the first few bytes of the file are not the required "
105 "signature)");
106 }
107 }
108
109
110
111
112 static void
113 readJ2k(const char * const inputFilename,
114 jas_image_t ** const jasperPP) {
115
116 const char * const options = "";
117
118 jas_image_t * jasperP;
119 jas_stream_t * instreamP;
120 const char * error;
121
122 if (streq(inputFilename, "-")) {
123 /* The input image is to be read from standard input. */
124 instreamP = jas_stream_fdopen(fileno(stdin), "rb");
125 if (instreamP == NULL)
126 pm_error("error: cannot reopen standard input");
127 } else {
128 instreamP = jas_stream_fopen(inputFilename, "rb");
129 if (instreamP == NULL )
130 pm_error("cannot open input image file '%s'", inputFilename);
131 }
132
133 validateJ2k(instreamP);
134
135 pmjas_image_decode(instreamP, jas_image_getfmt(instreamP), options,
136 &jasperP, &error);
137 if (error)
138 pm_error("Unable to interpret JPEG-2000 input. %s", error);
139
140 jas_stream_close(instreamP);
141
142 *jasperPP = jasperP;
143 }
144
145
146
147 static void
148 getRgbComponents(int jasperCmpnt[], jas_image_t * const jasperP) {
149
150 {
151 int const rc =
152 jas_image_getcmptbytype(jasperP,
153 JAS_IMAGE_CT_COLOR(JAS_IMAGE_CT_RGB_R));
154 if (rc < 0)
155 pm_error("Input says it has RGB color space, but contains "
156 "no red component");
157 else
158 jasperCmpnt[PAM_RED_PLANE] = rc;
159
160 if (jas_image_cmptsgnd(jasperP, rc))
161 pm_error("Input image says it is RGB, but has signed values "
162 "for what should be the red intensities.");
163 }
164 {
165 int const rc =
166 jas_image_getcmptbytype(jasperP,
167 JAS_IMAGE_CT_COLOR(JAS_IMAGE_CT_RGB_G));
168 if (rc < 0)
169 pm_error("Input says it has RGB color space, but contains "
170 "no green component");
171 else
172 jasperCmpnt[PAM_GRN_PLANE] = rc;
173
174 if (jas_image_cmptsgnd(jasperP, rc))
175 pm_error("Input image says it is RGB, but has signed values "
176 "for what should be the green intensities.");
177 }
178 {
179 int const rc =
180 jas_image_getcmptbytype(jasperP,
181 JAS_IMAGE_CT_COLOR(JAS_IMAGE_CT_RGB_B));
182 if (rc < 0)
183 pm_error("Input says it has RGB color space, but contains "
184 "no blue component");
185 else
186 jasperCmpnt[PAM_BLU_PLANE] = rc;
187
188 if (jas_image_cmptsgnd(jasperP, rc))
189 pm_error("Input image says it is RGB, but has signed values "
190 "for what should be the blue intensities.");
191 }
192 }
193
194
195
196 static void
197 getGrayComponent(int * jasperCmptP, jas_image_t * const jasperP) {
198
199 int const rc =
200 jas_image_getcmptbytype(jasperP,
201 JAS_IMAGE_CT_COLOR(JAS_IMAGE_CT_GRAY_Y));
202 if (rc < 0)
203 pm_error("Input says it has Grayscale color space, but contains "
204 "no gray intensity component");
205 else
206 *jasperCmptP = rc;
207 if (jas_image_cmptsgnd(jasperP, 0))
208 pm_error("Input image says it is grayscale, but has signed values "
209 "for what should be the gray levels.");
210 }
211
212
213
214 static void
215 validateComponentsAlike(jas_image_t * const jasperP) {
216 /*----------------------------------------------------------------------------
217 JPC allows each component to have its own width and height. But
218 PAM requires all planes to have the same shape. So we validate now that
219 all the channels are the same, and abort the program if not.
220 -----------------------------------------------------------------------------*/
221 int cmptNo;
222
223 for (cmptNo = 0; cmptNo < jas_image_numcmpts(jasperP); ++cmptNo) {
224 if (jas_image_cmptwidth(jasperP, cmptNo) !=
225 jas_image_cmptwidth(jasperP, 0))
226 pm_message("Input image does not have components all the same "
227 "width.");
228 if (jas_image_cmptheight(jasperP, cmptNo) !=
229 jas_image_cmptheight(jasperP, 0))
230 pm_message("Input image does not have components all the same "
231 "height.");
232 }
233 }
234
235
236
237 static unsigned int
238 maxJasperComponentPrecision(jas_image_t * const jasperP) {
239
240 int cmptNo;
241 unsigned int max;
242
243 max = 1;
244
245 for (cmptNo = 0; cmptNo < jas_image_numcmpts(jasperP); ++cmptNo)
246 max = MAX(max, jas_image_cmptprec(jasperP, cmptNo));
247
248 return max;
249 }
250
251
252
253 static void
254 computeOutputParm(jas_image_t * const jasperP,
255 struct pam * const outpamP,
256 int ** const jasperCmptNoP) {
257
258 int * jasperCmptNo;
259 /* Malloc'ed array. jaspercmptNo[P] is the component number for use
260 with the Jasper library that corresponds to Plane P of the PAM.
261 */
262
263 switch (jas_clrspc_fam(jas_image_clrspc(jasperP))) {
264 case JAS_CLRSPC_FAM_GRAY:
265 outpamP->depth = 1;
266 MALLOCARRAY_NOFAIL(jasperCmptNo, 1);
267 getGrayComponent(&jasperCmptNo[0], jasperP);
268 if (jas_image_cmptprec(jasperP, jasperCmptNo[0]) == 1) {
269 outpamP->format = RPBM_FORMAT;
270 strcpy(outpamP->tuple_type, PAM_PBM_TUPLETYPE);
271 } else {
272 outpamP->format = RPGM_FORMAT;
273 strcpy(outpamP->tuple_type, PAM_PGM_TUPLETYPE);
274 }
275 break;
276 case JAS_CLRSPC_FAM_RGB:
277 outpamP->depth = 3;
278 MALLOCARRAY_NOFAIL(jasperCmptNo, 3);
279 getRgbComponents(jasperCmptNo, jasperP);
280 outpamP->format = RPPM_FORMAT;
281 strcpy(outpamP->tuple_type, PAM_PPM_TUPLETYPE);
282 break;
283 default:
284 outpamP->format = PAM_FORMAT;
285 outpamP->depth = jas_image_numcmpts(jasperP);
286 {
287 unsigned int plane;
288
289 MALLOCARRAY_NOFAIL(jasperCmptNo, outpamP->depth);
290 for (plane = 0; plane < outpamP->depth; ++plane)
291 jasperCmptNo[plane] = plane;
292 }
293 strcpy(outpamP->tuple_type, "");
294 if (jas_image_cmptsgnd(jasperP, 0))
295 pm_message("Warning: Input image has signed sample values. "
296 "They will be represented in the PAM output in "
297 "two's complement.");
298 }
299 outpamP->plainformat = FALSE;
300
301 outpamP->width = jas_image_cmptwidth(jasperP, 0);
302 outpamP->height = jas_image_cmptheight(jasperP, 0);
303
304 validateComponentsAlike(jasperP);
305
306 {
307 unsigned int const maxPrecision = maxJasperComponentPrecision(jasperP);
308
309 outpamP->maxval = pm_bitstomaxval(maxPrecision);
310
311 outpamP->bytes_per_sample = (maxPrecision + 7)/8;
312 }
313 *jasperCmptNoP = jasperCmptNo;
314 }
315
316
317
318 static void
319 createMatrices(struct pam * const outpamP, jas_matrix_t *** matrixP) {
320
321 jas_matrix_t ** matrix;
322 unsigned int plane;
323
324 MALLOCARRAY_NOFAIL(matrix, outpamP->depth);
325
326 for (plane = 0; plane < outpamP->depth; ++plane) {
327 matrix[plane] = jas_matrix_create(1, outpamP->width);
328
329 if (matrix[plane] == NULL)
330 pm_error("Unable to create matrix for plane %u. "
331 "jas_matrix_create() failed.", plane);
332 }
333 *matrixP = matrix;
334 }
335
336
337
338 static void
339 destroyMatrices(struct pam * const outpamP,
340 jas_matrix_t ** const matrix ) {
341
342 unsigned int plane;
343
344 for (plane = 0; plane < outpamP->depth; ++plane)
345 jas_matrix_destroy(matrix[plane]);
346 free(matrix);
347 }
348
349
350
351 static void
352 computeComponentMaxval(struct pam * const outpamP,
353 jas_image_t * const jasperP,
354 int const jasperCmpt[],
355 sample ** const jasperMaxvalP,
356 bool * const singleMaxvalP) {
357
358 sample * jasperMaxval;
359 unsigned int plane;
360
361 MALLOCARRAY(jasperMaxval, outpamP->depth);
362
363 *singleMaxvalP = TRUE; /* initial assumption */
364 for (plane = 0; plane < outpamP->depth; ++plane) {
365 jasperMaxval[plane] =
366 pm_bitstomaxval(jas_image_cmptprec(jasperP, jasperCmpt[plane]));
367 if (jasperMaxval[plane] != jasperMaxval[0])
368 *singleMaxvalP = FALSE;
369 }
370 *jasperMaxvalP = jasperMaxval;
371 }
372
373
374
375 static void
376 copyRowSingleMaxval(jas_seqent_t ** const jasperRow,
377 tuple * const tuplerow,
378 struct pam * const outpamP) {
379 /*----------------------------------------------------------------------------
380 Copy row from Jasper library representation to Netpbm library
381 representation, assuming all Jasper components have the same precision,
382 which corresponds to the maxval of the output PAM, which means we don't
383 have to do any maxval scaling.
384
385 This is significantly faster than copyRowAnyMaxval().
386 -----------------------------------------------------------------------------*/
387 unsigned int col;
388
389 for (col = 0; col < outpamP->width; ++col) {
390 unsigned int plane;
391 for (plane = 0; plane < outpamP->depth; ++plane)
392 tuplerow[col][plane] = jasperRow[plane][col];
393 }
394 }
395
396
397
398 static void
399 copyRowAnyMaxval(jas_seqent_t ** const jasperRow,
400 tuple * const tuplerow,
401 struct pam * const outpamP,
402 sample const jasperMaxval[]) {
403 /*----------------------------------------------------------------------------
404 Copy row from Jasper library representation to Netpbm library
405 representation, allowing for each Jasper library component to have a
406 different precision (number of bits) and for those precisions to be
407 unrelated to the PAM maxval.
408
409 This is significantly slower than copyRowSingleMaxval().
410 -----------------------------------------------------------------------------*/
411 unsigned int col;
412
413 for (col = 0; col < outpamP->width; ++col) {
414 unsigned int plane;
415 for (plane = 0; plane < outpamP->depth; ++plane)
416 tuplerow[col][plane] =
417 jasperRow[plane][col] *
418 outpamP->maxval / jasperMaxval[plane];
419 }
420 }
421
422
423
424 static void
425 convertToPamPnm(struct pam * const outpamP,
426 jas_image_t * const jasperP,
427 int const jasperCmptNo[]) {
428
429 jas_matrix_t ** matrix; /* malloc'ed */
430 /* matrix[X] is the data for Plane X of the current row */
431 sample * jasperMaxval;
432 unsigned int row;
433 tuple * tuplerow;
434 jas_seqent_t ** jasperRow; /* malloc'ed */
435 /* A row of the raster from the Jasper library This is an array of
436 pointers into the 'matrix' data structures, one for each plane in
437 the row.
438 */
439 bool singleMaxval;
440
441 createMatrices(outpamP, &matrix);
442
443 computeComponentMaxval(outpamP, jasperP, jasperCmptNo,
444 &jasperMaxval, &singleMaxval);
445
446 MALLOCARRAY(jasperRow, outpamP->depth);
447 if (jasperRow == NULL)
448 pm_error("Out of memory");
449
450 tuplerow = pnm_allocpamrow(outpamP);
451
452 for (row = 0; row < outpamP->height; ++row) {
453 unsigned int plane;
454
455 for (plane = 0; plane < outpamP->depth; ++plane) {
456 int rc;
457 rc = jas_image_readcmpt(jasperP, jasperCmptNo[plane], 0, row,
458 outpamP->width, 1,
459 matrix[plane]);
460 if (rc != 0)
461 pm_error("jas_image_readcmpt() of row %u plane %u "
462 "failed.",
463 row, plane);
464 jasperRow[plane] = jas_matrix_getref(matrix[plane], 0, 0);
465 }
466 if (singleMaxval)
467 copyRowSingleMaxval(jasperRow, tuplerow, outpamP);
468 else
469 copyRowAnyMaxval(jasperRow, tuplerow, outpamP, jasperMaxval);
470
471 pnm_writepamrow(outpamP, tuplerow);
472 }
473 pnm_freepamrow(tuplerow);
474
475 destroyMatrices(outpamP, matrix);
476
477 free(jasperRow);
478 free(jasperMaxval);
479 }
480
481
482
483 int
484 main(int argc, const char **argv)
485 {
486 struct CmdlineInfo cmdline;
487 struct pam outpam;
488 int * jasperCmpt; /* malloc'ed */
489 /* jaspercmpt[P] is the component number for use with the
490 Jasper library that corresponds to Plane P of the PAM.
491 */
492 jas_image_t * jasperP;
493
494 pm_proginit(&argc, argv);
495
496 parseCommandLine(argc, argv, &cmdline);
497
498 {
499 int rc;
500
501 rc = jas_init();
502 if ( rc != 0 )
503 pm_error("Failed to initialize Jasper library. "
504 "jas_init() returns rc %d", rc );
505 }
506
507 jas_setdbglevel(cmdline.debuglevel);
508
509 readJ2k(cmdline.inputFilename, &jasperP);
510
511 outpam.file = stdout;
512 outpam.size = sizeof(outpam);
513 outpam.len = PAM_STRUCT_SIZE(tuple_type);
514
515 computeOutputParm(jasperP, &outpam, &jasperCmpt);
516
517 pnm_writepaminit(&outpam);
518
519 convertToPamPnm(&outpam, jasperP, jasperCmpt);
520
521 free(jasperCmpt);
522 jas_image_destroy(jasperP);
523
524 pm_close(stdout);
525
526 return 0;
527 }
528
529
530