Debian Codegraph @

converter/other/jpeg2000/pamtojpeg2k.c

raw
1 /*****************************************************************************
2 pamtojpeg2k
3 ******************************************************************************
4
5 Convert a PNM image to JPEG-2000 code stream image
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 too, and they did not have
18 _XOPEN_SOURCE 600, so it would seem to be superfluous here too.
19 */
20
21 #include <string.h>
22
23 #include <jasper/jasper.h>
24
25 #include "pm_c_util.h"
26 #include "pam.h"
27 #include "shhopt.h"
28 #include "nstring.h"
29 #include "mallocvar.h"
30
31 #include "libjasper_compat.h"
32
33
34 enum compmode {COMPMODE_INTEGER, COMPMODE_REAL};
35
36 enum progression {PROG_LRCP, PROG_RLCP, PROG_RPCL, PROG_PCRL, PROG_CPRL};
37
38 struct CmdlineInfo {
39 /* All the information the user supplied in the command line,
40 in a form easy for the program to use.
41 */
42 char * inputFilename;
43 unsigned int imgareatlx;
44 unsigned int imgareatly;
45 unsigned int tilegrdtlx;
46 unsigned int tilegrdtly;
47 unsigned int tilewidth;
48 unsigned int tileheight;
49 unsigned int prcwidth;
50 unsigned int prcheight;
51 unsigned int cblkwidth;
52 unsigned int cblkheight;
53 enum compmode compmode;
54 unsigned int compressionSpec;
55 float compression;
56 unsigned int size;
57 unsigned int sizeSpec;
58 char * ilyrrates;
59 enum progression progression;
60 unsigned int numrlvls;
61 unsigned int numgbits;
62 unsigned int nomct;
63 unsigned int sop;
64 unsigned int eph;
65 unsigned int lazy;
66 unsigned int termall;
67 unsigned int segsym;
68 unsigned int vcausal;
69 unsigned int pterm;
70 unsigned int resetprob;
71 unsigned int debuglevel; /* Jasper library debug level */
72 unsigned int verbose;
73 };
74
75
76 static void
77 parseCommandLine(int argc, char ** argv,
78 struct CmdlineInfo * const cmdlineP) {
79 /*----------------------------------------------------------------------------
80 Note that many of the strings that this function returns in the
81 *cmdlineP structure are actually in the supplied argv array. And
82 sometimes, one of these strings is actually just a suffix of an entry
83 in argv!
84 -----------------------------------------------------------------------------*/
85 optEntry *option_def;
86 /* Instructions to OptParseOptions3 on how to parse our options.
87 */
88 optStruct3 opt;
89
90 unsigned int imgareatlxSpec, imgareatlySpec;
91 unsigned int tilegrdtlxSpec, tilegrdtlySpec;
92 unsigned int tilewidthSpec, tileheightSpec;
93 unsigned int prcwidthSpec, prcheightSpec;
94 unsigned int cblkwidthSpec, cblkheightSpec;
95 unsigned int modeSpec, ilyrratesSpec;
96 unsigned int progressionSpec, numrlvlsSpec, numgbitsSpec;
97 unsigned int debuglevelSpec;
98
99 char * progressionOpt;
100 char * modeOpt;
101
102 unsigned int option_def_index;
103
104 MALLOCARRAY_NOFAIL(option_def, 100);
105
106 option_def_index = 0; /* incremented by OPTENTRY */
107 OPTENT3(0, "imgareatlx", OPT_UINT, &cmdlineP->imgareatlx,
108 &imgareatlxSpec, 0);
109 OPTENT3(0, "imgareatly", OPT_UINT, &cmdlineP->imgareatly,
110 &imgareatlySpec, 0);
111 OPTENT3(0, "tilegrdtlx", OPT_UINT, &cmdlineP->tilegrdtlx,
112 &tilegrdtlxSpec, 0);
113 OPTENT3(0, "tilegrdtly", OPT_UINT, &cmdlineP->tilegrdtly,
114 &tilegrdtlySpec, 0);
115 OPTENT3(0, "tilewidth", OPT_UINT, &cmdlineP->tilewidth,
116 &tilewidthSpec, 0);
117 OPTENT3(0, "tileheight", OPT_UINT, &cmdlineP->tileheight,
118 &tileheightSpec, 0);
119 OPTENT3(0, "prcwidth", OPT_UINT, &cmdlineP->prcwidth,
120 &prcwidthSpec, 0);
121 OPTENT3(0, "prcheight", OPT_UINT, &cmdlineP->prcheight,
122 &prcheightSpec, 0);
123 OPTENT3(0, "cblkwidth", OPT_UINT, &cmdlineP->cblkwidth,
124 &cblkwidthSpec, 0);
125 OPTENT3(0, "cblkheight", OPT_UINT, &cmdlineP->cblkheight,
126 &cblkheightSpec, 0);
127 OPTENT3(0, "mode", OPT_STRING, &modeOpt,
128 &modeSpec, 0);
129 OPTENT3(0, "compression", OPT_FLOAT, &cmdlineP->compression,
130 &cmdlineP->compressionSpec, 0);
131 OPTENT3(0, "size", OPT_UINT, &cmdlineP->size,
132 &cmdlineP->sizeSpec, 0);
133 OPTENT3(0, "ilyrrates", OPT_STRING, &cmdlineP->ilyrrates,
134 &ilyrratesSpec, 0);
135 OPTENT3(0, "progression", OPT_STRING, &progressionOpt,
136 &progressionSpec, 0);
137 OPTENT3(0, "numrlvls", OPT_UINT, &cmdlineP->numrlvls,
138 &numrlvlsSpec, 0);
139 OPTENT3(0, "numgbits", OPT_UINT, &cmdlineP->numgbits,
140 &numgbitsSpec, 0);
141 OPTENT3(0, "nomct", OPT_FLAG, NULL,
142 &cmdlineP->nomct, 0);
143 OPTENT3(0, "sop", OPT_FLAG, NULL,
144 &cmdlineP->sop, 0);
145 OPTENT3(0, "eph", OPT_FLAG, NULL,
146 &cmdlineP->eph, 0);
147 OPTENT3(0, "lazy", OPT_FLAG, NULL,
148 &cmdlineP->lazy, 0);
149 OPTENT3(0, "termall", OPT_FLAG, NULL,
150 &cmdlineP->termall, 0);
151 OPTENT3(0, "segsym", OPT_FLAG, NULL,
152 &cmdlineP->segsym, 0);
153 OPTENT3(0, "vcausal", OPT_FLAG, NULL,
154 &cmdlineP->vcausal, 0);
155 OPTENT3(0, "pterm", OPT_FLAG, NULL,
156 &cmdlineP->pterm, 0);
157 OPTENT3(0, "resetprob", OPT_FLAG, NULL,
158 &cmdlineP->resetprob, 0);
159 OPTENT3(0, "verbose", OPT_FLAG, NULL,
160 &cmdlineP->verbose, 0);
161 OPTENT3(0, "debuglevel", OPT_UINT, &cmdlineP->debuglevel,
162 &debuglevelSpec, 0);
163
164 opt.opt_table = option_def;
165 opt.short_allowed = FALSE; /* We have no short (old-fashioned) options */
166 opt.allowNegNum = FALSE; /* We have no parms that are negative numbers */
167
168 pm_optParseOptions3(&argc, argv, opt, sizeof(opt), 0);
169
170 if (!imgareatlxSpec)
171 cmdlineP->imgareatlx = 0;
172 if (!imgareatlySpec)
173 cmdlineP->imgareatly = 0;
174 if (!tilegrdtlxSpec)
175 cmdlineP->tilegrdtlx = 0;
176 if (!tilegrdtlySpec)
177 cmdlineP->tilegrdtly = 0;
178 if (!tilewidthSpec)
179 cmdlineP->tilewidth = 0;
180 if (!tileheightSpec)
181 cmdlineP->tileheight = 0;
182 if (!prcwidthSpec)
183 cmdlineP->prcwidth = 32768;
184 if (!prcheightSpec)
185 cmdlineP->prcheight = 32768;
186 if (!cblkwidthSpec)
187 cmdlineP->cblkwidth = 64;
188 if (!cblkheightSpec)
189 cmdlineP->cblkheight = 64;
190 if (modeSpec) {
191 if (strcmp(modeOpt, "integer") == 0 || strcmp(modeOpt, "int") == 0)
192 cmdlineP->compmode = COMPMODE_INTEGER;
193 else if (strcmp(modeOpt, "real") == 0)
194 cmdlineP->compmode = COMPMODE_REAL;
195 else
196 pm_error("Invalid value for 'mode' option: '%s'. "
197 "valid values are 'INTEGER' and 'REAL'", modeOpt);
198 } else
199 cmdlineP->compmode = COMPMODE_INTEGER;
200 if (!ilyrratesSpec)
201 cmdlineP->ilyrrates = (char*) "";
202 if (progressionSpec) {
203 if (streq(progressionOpt, "lrcp"))
204 cmdlineP->progression = PROG_LRCP;
205 else if (streq(progressionOpt, "rlcp"))
206 cmdlineP->progression = PROG_RLCP;
207 else if (streq(progressionOpt, "rpcl"))
208 cmdlineP->progression = PROG_RPCL;
209 else if (streq(progressionOpt, "pcrl"))
210 cmdlineP->progression = PROG_PCRL;
211 else if (streq(progressionOpt, "cprl"))
212 cmdlineP->progression = PROG_CPRL;
213 else
214 pm_error("Invalid value for -progression: '%s'. "
215 "Valid values are lrcp, rlcp, rpcl, pcrl, and cprl.",
216 progressionOpt);
217 } else
218 cmdlineP->progression = PROG_LRCP;
219 if (!numrlvlsSpec)
220 cmdlineP->numrlvls = 6;
221 if (!numgbitsSpec)
222 cmdlineP->numgbits = 2;
223 if (!debuglevelSpec)
224 cmdlineP->debuglevel = 0;
225
226 if (cmdlineP->compressionSpec && cmdlineP->sizeSpec)
227 pm_error("You cannot specify by -compression and -size");
228
229 if (argc - 1 == 0)
230 cmdlineP->inputFilename = strdup("-"); /* he wants stdin */
231 else if (argc - 1 == 1)
232 cmdlineP->inputFilename = strdup(argv[1]);
233 else
234 pm_error("Too many arguments. The only argument accepted\n"
235 "is the input file specification");
236
237 }
238
239
240
241 static void
242 createJasperRaster(struct pam * const inpamP,
243 jas_image_t * const jasperP) {
244 /*----------------------------------------------------------------------------
245 Create the raster in the *jasperP object, reading the raster from the
246 input file described by *inpamP, which is positioned to the raster.
247 -----------------------------------------------------------------------------*/
248 jas_matrix_t ** matrix; /* malloc'ed */
249 /* matrix[X] is the data for Plane X of the current row */
250 unsigned int plane;
251 unsigned int row;
252 tuple * tuplerow;
253 bool oddMaxval;
254 sample jasperMaxval;
255
256 MALLOCARRAY_NOFAIL(matrix, inpamP->depth);
257
258 for (plane = 0; plane < inpamP->depth; ++plane) {
259 matrix[plane] = jas_matrix_create(1, inpamP->width);
260
261 if (matrix[plane] == NULL)
262 pm_error("Unable to create matrix for plane %u. "
263 "jas_matrix_create() failed.", plane);
264 }
265 tuplerow = pnm_allocpamrow(inpamP);
266
267 jasperMaxval = pm_bitstomaxval(pm_maxvaltobits(inpamP->maxval));
268 oddMaxval = jasperMaxval != inpamP->maxval;
269
270 for (row = 0; row < inpamP->height; ++row) {
271 unsigned int col;
272
273 pnm_readpamrow(inpamP, tuplerow);
274
275 for (col = 0; col < inpamP->width; ++col) {
276 unsigned int plane;
277 for (plane = 0; plane < inpamP->depth; ++plane) {
278 unsigned int jasperSample;
279
280 if (oddMaxval)
281 jasperSample = tuplerow[col][plane] *
282 jasperMaxval / inpamP->maxval;
283 else
284 jasperSample = tuplerow[col][plane];
285
286 jas_matrix_set(matrix[plane], 0, col, jasperSample);
287 }
288 }
289 {
290 unsigned int plane;
291
292 for (plane = 0; plane < inpamP->depth; ++plane) {
293 int rc;
294 rc = jas_image_writecmpt(jasperP, plane, 0, row,
295 inpamP->width, 1,
296 matrix[plane]);
297 if (rc != 0)
298 pm_error("jas_image_writecmpt() of plane %u failed.",
299 plane);
300 }
301 }
302 }
303
304 pnm_freepamrow(tuplerow);
305 for (plane = 0; plane < inpamP->depth; ++plane)
306 jas_matrix_destroy(matrix[plane]);
307
308 free(matrix);
309 }
310
311
312
313 static void
314 createJasperImage(struct pam * const inpamP,
315 jas_image_t ** const jasperPP) {
316
317 jas_image_cmptparm_t * cmptparms;
318 unsigned int plane;
319
320 MALLOCARRAY_NOFAIL(cmptparms, inpamP->depth);
321
322 for (plane = 0; plane < inpamP->depth; ++plane) {
323 cmptparms[plane].tlx = 0;
324 cmptparms[plane].tly = 0;
325 cmptparms[plane].hstep = 1;
326 cmptparms[plane].vstep = 1;
327 cmptparms[plane].width = inpamP->width;
328 cmptparms[plane].height = inpamP->height;
329 cmptparms[plane].prec = pm_maxvaltobits(inpamP->maxval);
330 cmptparms[plane].sgnd = 0;
331 }
332 *jasperPP =
333 jas_image_create(inpamP->depth, cmptparms, JAS_CLRSPC_UNKNOWN);
334 if (*jasperPP == NULL)
335 pm_error("Unable to create jasper image structure. "
336 "jas_image_create() failed.");
337
338 free(cmptparms);
339 }
340
341
342
343 static void
344 convertToJasperImage(struct pam * const inpamP,
345 jas_image_t ** const jasperPP) {
346
347 jas_image_t * jasperP;
348
349 createJasperImage(inpamP, &jasperP);
350
351 if (strneq(inpamP->tuple_type, "RGB", 3)) {
352 if (inpamP->depth < 3)
353 pm_error("Input tuple type is RGB*, but depth is only %d. "
354 "It should be at least 3.", inpamP->depth);
355 else {
356 jas_image_setclrspc(jasperP, JAS_CLRSPC_GENRGB);
357 jas_image_setcmpttype(jasperP, 0,
358 JAS_IMAGE_CT_COLOR(JAS_IMAGE_CT_RGB_R));
359 jas_image_setcmpttype(jasperP, 1,
360 JAS_IMAGE_CT_COLOR(JAS_IMAGE_CT_RGB_G));
361 jas_image_setcmpttype(jasperP, 2,
362 JAS_IMAGE_CT_COLOR(JAS_IMAGE_CT_RGB_B));
363 }
364 } else {
365 if (strneq(inpamP->tuple_type, "GRAYSCALE", 9) ||
366 strneq(inpamP->tuple_type, "BLACKANDWHITE", 13)) {
367 jas_image_setclrspc(jasperP, JAS_CLRSPC_GENGRAY);
368 jas_image_setcmpttype(jasperP, 0,
369 JAS_IMAGE_CT_COLOR(JAS_IMAGE_CT_GRAY_Y));
370 }
371 }
372
373 createJasperRaster(inpamP, jasperP);
374
375 *jasperPP = jasperP;
376 }
377
378
379
380 static void
381 writeJpc(jas_image_t * const jasperP,
382 struct CmdlineInfo const cmdline,
383 int const ofd) {
384 /*----------------------------------------------------------------------------
385 Write the image *jasperP to open file 'ofd'.
386 -----------------------------------------------------------------------------*/
387 jas_stream_t * outStreamP;
388 const char * options;
389 const char * ilyrratesOpt;
390 const char * prgValue;
391 char rateOpt[20+1];
392
393 /* Note: ilyrrates is a hack because we're too lazy to properly parse
394 command line options to get the information and then compose
395 a proper input to Jasper. So the user can screw things up by
396 specifying garbage for the -ilyrrates option
397 */
398 if (strlen(cmdline.ilyrrates) > 0)
399 pm_asprintf(&ilyrratesOpt, "ilyrrates=%s", cmdline.ilyrrates);
400 else
401 ilyrratesOpt = strdup("");
402
403 switch(cmdline.progression) {
404 case PROG_LRCP: prgValue = "lrcp"; break;
405 case PROG_RLCP: prgValue = "rlcp"; break;
406 case PROG_RPCL: prgValue = "rcpc"; break;
407 case PROG_PCRL: prgValue = "pcrl"; break;
408 case PROG_CPRL: prgValue = "cprl"; break;
409 }
410
411 /* Note that asprintfN() doesn't understand %f, but sprintf() does */
412
413 if (cmdline.compressionSpec)
414 sprintf(rateOpt, "rate=%1.9f", 1.0/cmdline.compression);
415 else if (cmdline.sizeSpec)
416 sprintf(rateOpt, "rate=%uB", cmdline.size);
417 else {
418 /* No 'rate' option. This means there is no constraint on the image
419 size, so the encoder will compress losslessly. Note that the
420 image may get larger, because of metadata.
421 */
422 rateOpt[0] = '\0';
423 }
424 pm_asprintf(&options,
425 "imgareatlx=%u "
426 "imgareatly=%u "
427 "tilegrdtlx=%u "
428 "tilegrdtly=%u "
429 "tilewidth=%u "
430 "tileheight=%u "
431 "prcwidth=%u "
432 "prcheight=%u "
433 "cblkwidth=%u "
434 "cblkheight=%u "
435 "mode=%s "
436 "%s " /* rate */
437 "%s " /* ilyrrates */
438 "prg=%s "
439 "numrlvls=%u "
440 "numgbits=%u "
441 "%s %s %s %s %s %s %s %s %s",
442
443 cmdline.imgareatlx,
444 cmdline.imgareatly,
445 cmdline.tilegrdtlx,
446 cmdline.tilegrdtly,
447 cmdline.tilewidth,
448 cmdline.tileheight,
449 cmdline.prcwidth,
450 cmdline.prcheight,
451 cmdline.cblkwidth,
452 cmdline.cblkheight,
453 cmdline.compmode == COMPMODE_INTEGER ? "int" : "real",
454 rateOpt,
455 ilyrratesOpt,
456 prgValue,
457 cmdline.numrlvls,
458 cmdline.numgbits,
459 cmdline.nomct ? "nomct" : "",
460 cmdline.sop ? "sop" : "",
461 cmdline.eph ? "eph" : "",
462 cmdline.lazy ? "lazy" : "",
463 cmdline.termall ? "termall" : "",
464 cmdline.segsym ? "segsym" : "",
465 cmdline.vcausal ? "vcausal" : "",
466 cmdline.pterm ? "pterm" : "",
467 cmdline.resetprob ? "resetprob" : ""
468 );
469
470 pm_strfree(ilyrratesOpt);
471
472 /* Open the output image file (Standard Output) */
473 outStreamP = jas_stream_fdopen(ofd, "w+b");
474 if (outStreamP == NULL)
475 pm_error("Unable to open output stream. jas_stream_fdopen() "
476 "failed");
477
478 {
479 int rc;
480
481 if (cmdline.verbose)
482 pm_message("Using Jasper to encode to 'jpc' format with options "
483 "'%s'", options);
484
485 rc = jas_image_encode(jasperP, outStreamP,
486 jas_image_strtofmt((char*)"jpc"),
487 (char *)options);
488 if (rc != 0)
489 pm_error("jas_image_encode() failed to encode the JPEG 2000 "
490 "image. Rc=%d", rc);
491 }
492 jas_stream_flush(outStreamP);
493
494 {
495 int rc;
496
497 rc = jas_stream_close(outStreamP);
498
499 if (rc != 0)
500 pm_error("Failed to close output stream, "
501 "jas_stream_close() rc = %d", rc);
502 }
503
504 jas_image_clearfmts();
505
506 pm_strfree(options);
507 }
508
509
510
511 int
512 main(int argc, char **argv)
513 {
514 struct CmdlineInfo cmdline;
515 FILE * ifP;
516 struct pam inpam;
517 jas_image_t * jasperP;
518
519 pnm_init(&argc, argv);
520
521 parseCommandLine(argc, argv, &cmdline);
522
523 {
524 int rc;
525
526 rc = jas_init();
527 if ( rc != 0 )
528 pm_error("Failed to initialize Jasper library. "
529 "jas_init() returns rc %d", rc );
530 }
531
532 jas_setdbglevel(cmdline.debuglevel);
533
534 ifP = pm_openr(cmdline.inputFilename);
535
536 pnm_readpaminit(ifP, &inpam, PAM_STRUCT_SIZE(tuple_type));
537
538 convertToJasperImage(&inpam, &jasperP);
539
540 writeJpc(jasperP, cmdline, fileno(stdout));
541
542 jas_image_destroy(jasperP);
543
544 pm_close(ifP);
545
546 pm_close(stdout);
547
548 return 0;
549 }