1 /* Copyright (c) 2001 David Janssens
2 * Copyright (c) 2002 Yannick Verschueren
3 * Copyright (c) 2002 Communications and remote sensing Laboratory, Universite catholique de Louvain, Belgium
6 * Redistribution and use in source and binary forms, with or without
7 * modification, are permitted provided that the following conditions
9 * 1. Redistributions of source code must retain the above copyright
10 * notice, this list of conditions and the following disclaimer.
11 * 2. Redistributions in binary form must reproduce the above copyright
12 * notice, this list of conditions and the following disclaimer in the
13 * documentation and/or other materials provided with the distribution.
15 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS `AS IS'
16 * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
17 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
18 * ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE
19 * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
20 * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
21 * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
22 * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
23 * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
24 * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
25 * POSSIBILITY OF SUCH DAMAGE.
35 int ceildiv(int a, int b)
37 return (a + b - 1) / b;
40 int main(int argc, char **argv)
44 char *dest, S1, S2, S3;
49 int i, image_type = -1;
52 fprintf(stderr, "usage: %s j2k-file pnm-file\n", argv[0]);
56 f = fopen(argv[1], "rb");
58 fprintf(stderr, "failed to open %s for reading\n", argv[1]);
64 while (*dest && *dest != '.') {
74 if ((S1 == 'p' && S2 == 'g' && S3 == 'x')
75 || (S1 == 'P' && S2 == 'G' && S3 == 'X')) {
79 if ((S1 == 'p' && S2 == 'n' && S3 == 'm')
80 || (S1 == 'P' && S2 == 'N' && S3 == 'M') || (S1 == 'p' && S2 == 'g'
82 || (S1 == 'P' && S2 == 'G' && S3 == 'M') || (S1 == 'P' && S2 == 'P'
84 || (S1 == 'p' && S2 == 'p' && S3 == 'm')) {
88 if ((S1 == 'b' && S2 == 'm' && S3 == 'p')
89 || (S1 == 'B' && S2 == 'M' && S3 == 'P')) {
92 if (image_type == -1) {
94 "\033[0;33m!! Unrecognized format for infile [accept only *.pnm, *.pgm, *.ppm, *.pgx or *.bmp] !!\033[0;39m\n\n");
98 fseek(f, 0, SEEK_END);
100 fseek(f, 0, SEEK_SET);
101 src = (char *) malloc(len);
102 fread(src, 1, len, f);
105 if (!j2k_decode(src, len, &img, &cp)) {
106 fprintf(stderr, "j2k_to_image: failed to decode image!\n");
111 /* ------------------ CREATE OUT IMAGE WITH THE RIGHT FORMAT ----------------------- */
113 /* ------------------------ / */
115 /* FORMAT : PNM, PGM or PPM / */
117 /* ------------------------ / */
119 if (image_type == 1) { /* PNM PGM PPM */
120 if (img->numcomps == 3 && img->comps[0].dx == img->comps[1].dx
121 && img->comps[1].dx == img->comps[2].dx
122 && img->comps[0].dy == img->comps[1].dy
123 && img->comps[1].dy == img->comps[2].dy
124 && img->comps[0].prec == img->comps[1].prec
125 && img->comps[1].prec == img->comps[2].prec) {
126 f = fopen(argv[2], "wb");
127 w = ceildiv(img->x1 - img->x0, img->comps[0].dx);
128 h = ceildiv(img->y1 - img->y0, img->comps[0].dy);
129 max = (1 << img->comps[0].prec) - 1;
130 fprintf(f, "P6\n%d %d\n%d\n", w, h, max);
131 for (i = 0; i < w * h; i++) {
133 r = img->comps[0].data[i];
134 g = img->comps[1].data[i];
135 b = img->comps[2].data[i];
136 fprintf(f, "%c%c%c", r, g, b);
141 for (compno = 0; compno < img->numcomps; compno++) {
143 if (img->numcomps > 1) {
144 sprintf(name, "%d.%s", compno, argv[2]);
146 sprintf(name, "%s", argv[2]);
149 f = fopen(name, "wb");
151 w = ceildiv(img->x1 - img->x0, img->comps[compno].dx);
152 h = ceildiv(img->y1 - img->y0, img->comps[compno].dy);
153 max = (1 << img->comps[compno].prec) - 1;
154 fprintf(f, "P5\n%d %d\n%d\n", w, h, max);
155 for (i = 0; i < w * h; i++) {
157 l = img->comps[compno].data[i];
164 /* ------------------------ / */
168 /* ------------------------ / */
170 if (image_type == 0) { /* PGX */
172 for (compno = 0; compno < img->numcomps; compno++) {
173 j2k_comp_t *comp = &img->comps[compno];
175 /* sprintf(name, "%s-%d.pgx", argv[2], compno); */
176 sprintf(name, "%s", argv[2]);
177 f = fopen(name, "wb");
178 w = ceildiv(img->x1 - img->x0, comp->dx);
179 h = ceildiv(img->y1 - img->y0, comp->dy);
180 fprintf(f, "PG LM %c %d %d %d\n", comp->sgnd ? '-' : '+', comp->prec,
182 for (i = 0; i < w * h; i++) {
183 int v = img->comps[compno].data[i];
184 if (comp->prec <= 8) {
187 } else if (comp->prec <= 16) {
197 /* ------------------------ / */
201 /* ------------------------ / */
204 if (image_type == 2) { /* BMP */
205 if (img->numcomps == 3 && img->comps[0].dx == img->comps[1].dx
206 && img->comps[1].dx == img->comps[2].dx
207 && img->comps[0].dy == img->comps[1].dy
208 && img->comps[1].dy == img->comps[2].dy
209 && img->comps[0].prec == img->comps[1].prec
210 && img->comps[1].prec == img->comps[2].prec) {
211 /* -->> -->> -->> -->>
215 <<-- <<-- <<-- <<-- */
217 f = fopen(argv[2], "wb");
218 w = ceildiv(img->x1 - img->x0, img->comps[0].dx);
219 h = ceildiv(img->y1 - img->y0, img->comps[0].dy);
225 fprintf(f, "%c%c%c%c",
226 (unsigned char) (h * w * 3 + 3 * h * (w % 2) + 54) & 0xff,
227 (unsigned char) ((h * w * 3 + 3 * h * (w % 2) + 54) >> 8) &
229 (unsigned char) ((h * w * 3 + 3 * h * (w % 2) + 54) >> 16) &
231 (unsigned char) ((h * w * 3 + 3 * h * (w % 2) + 54) >> 24) &
233 fprintf(f, "%c%c%c%c", (0) & 0xff, ((0) >> 8) & 0xff,
234 ((0) >> 16) & 0xff, ((0) >> 24) & 0xff);
235 fprintf(f, "%c%c%c%c", (54) & 0xff, ((54) >> 8) & 0xff,
236 ((54) >> 16) & 0xff, ((54) >> 24) & 0xff);
239 fprintf(f, "%c%c%c%c", (40) & 0xff, ((40) >> 8) & 0xff,
240 ((40) >> 16) & 0xff, ((40) >> 24) & 0xff);
241 fprintf(f, "%c%c%c%c", (unsigned char) ((w) & 0xff),
242 (unsigned char) ((w) >> 8) & 0xff,
243 (unsigned char) ((w) >> 16) & 0xff,
244 (unsigned char) ((w) >> 24) & 0xff);
245 fprintf(f, "%c%c%c%c", (unsigned char) ((h) & 0xff),
246 (unsigned char) ((h) >> 8) & 0xff,
247 (unsigned char) ((h) >> 16) & 0xff,
248 (unsigned char) ((h) >> 24) & 0xff);
249 fprintf(f, "%c%c", (1) & 0xff, ((1) >> 8) & 0xff);
250 fprintf(f, "%c%c", (24) & 0xff, ((24) >> 8) & 0xff);
251 fprintf(f, "%c%c%c%c", (0) & 0xff, ((0) >> 8) & 0xff,
252 ((0) >> 16) & 0xff, ((0) >> 24) & 0xff);
253 fprintf(f, "%c%c%c%c",
254 (unsigned char) (3 * h * w + 3 * h * (w % 2)) & 0xff,
255 (unsigned char) ((h * w * 3 + 3 * h * (w % 2)) >> 8) & 0xff,
256 (unsigned char) ((h * w * 3 + 3 * h * (w % 2)) >> 16) & 0xff,
257 (unsigned char) ((h * w * 3 + 3 * h * (w % 2)) >> 24) &
259 fprintf(f, "%c%c%c%c", (7834) & 0xff, ((7834) >> 8) & 0xff,
260 ((7834) >> 16) & 0xff, ((7834) >> 24) & 0xff);
261 fprintf(f, "%c%c%c%c", (7834) & 0xff, ((7834) >> 8) & 0xff,
262 ((7834) >> 16) & 0xff, ((7834) >> 24) & 0xff);
263 fprintf(f, "%c%c%c%c", (0) & 0xff, ((0) >> 8) & 0xff,
264 ((0) >> 16) & 0xff, ((0) >> 24) & 0xff);
265 fprintf(f, "%c%c%c%c", (0) & 0xff, ((0) >> 8) & 0xff,
266 ((0) >> 16) & 0xff, ((0) >> 24) & 0xff);
268 for (i = 0; i < w * h; i++) {
269 unsigned char R, G, B;
271 R = img->comps[0].data[w * h - ((i) / (w) + 1) * w + (i) % (w)];
272 G = img->comps[1].data[w * h - ((i) / (w) + 1) * w + (i) % (w)];
273 B = img->comps[2].data[w * h - ((i) / (w) + 1) * w + (i) % (w)];
274 fprintf(f, "%c%c%c", B, G, R);
275 if (((i + 1) % w == 0 && w % 2))
276 fprintf(f, "%c%c%c", (0) & 0xff, ((0) >> 8) & 0xff,
280 } else { /* Gray-scale */
282 /* -->> -->> -->> -->>
284 8 bits non code (Gray scale)
286 <<-- <<-- <<-- <<-- */
287 f = fopen(argv[2], "wb");
288 w = ceildiv(img->x1 - img->x0, img->comps[0].dx);
289 h = ceildiv(img->y1 - img->y0, img->comps[0].dy);
295 fprintf(f, "%c%c%c%c",
296 (unsigned char) (h * w + 54 + 1024 + h * (w % 2)) & 0xff,
297 (unsigned char) ((h * w + 54 + 1024 + h * (w % 2)) >> 8) &
299 (unsigned char) ((h * w + 54 + 1024 + h * (w % 2)) >> 16) &
301 (unsigned char) ((h * w + 54 + 1024 + w * (w % 2)) >> 24) &
303 fprintf(f, "%c%c%c%c", (0) & 0xff, ((0) >> 8) & 0xff,
304 ((0) >> 16) & 0xff, ((0) >> 24) & 0xff);
305 fprintf(f, "%c%c%c%c", (54 + 1024) & 0xff, ((54 + 1024) >> 8) & 0xff,
306 ((54 + 1024) >> 16) & 0xff, ((54 + 1024) >> 24) & 0xff);
309 fprintf(f, "%c%c%c%c", (40) & 0xff, ((40) >> 8) & 0xff,
310 ((40) >> 16) & 0xff, ((40) >> 24) & 0xff);
311 fprintf(f, "%c%c%c%c", (unsigned char) ((w) & 0xff),
312 (unsigned char) ((w) >> 8) & 0xff,
313 (unsigned char) ((w) >> 16) & 0xff,
314 (unsigned char) ((w) >> 24) & 0xff);
315 fprintf(f, "%c%c%c%c", (unsigned char) ((h) & 0xff),
316 (unsigned char) ((h) >> 8) & 0xff,
317 (unsigned char) ((h) >> 16) & 0xff,
318 (unsigned char) ((h) >> 24) & 0xff);
319 fprintf(f, "%c%c", (1) & 0xff, ((1) >> 8) & 0xff);
320 fprintf(f, "%c%c", (8) & 0xff, ((8) >> 8) & 0xff);
321 fprintf(f, "%c%c%c%c", (0) & 0xff, ((0) >> 8) & 0xff,
322 ((0) >> 16) & 0xff, ((0) >> 24) & 0xff);
323 fprintf(f, "%c%c%c%c", (unsigned char) (h * w + h * (w % 2)) & 0xff,
324 (unsigned char) ((h * w + h * (w % 2)) >> 8) & 0xff,
325 (unsigned char) ((h * w + h * (w % 2)) >> 16) & 0xff,
326 (unsigned char) ((h * w + h * (w % 2)) >> 24) & 0xff);
327 fprintf(f, "%c%c%c%c", (7834) & 0xff, ((7834) >> 8) & 0xff,
328 ((7834) >> 16) & 0xff, ((7834) >> 24) & 0xff);
329 fprintf(f, "%c%c%c%c", (7834) & 0xff, ((7834) >> 8) & 0xff,
330 ((7834) >> 16) & 0xff, ((7834) >> 24) & 0xff);
331 fprintf(f, "%c%c%c%c", (256) & 0xff, ((256) >> 8) & 0xff,
332 ((256) >> 16) & 0xff, ((256) >> 24) & 0xff);
333 fprintf(f, "%c%c%c%c", (256) & 0xff, ((256) >> 8) & 0xff,
334 ((256) >> 16) & 0xff, ((256) >> 24) & 0xff);
337 for (i = 0; i < 256; i++) {
338 fprintf(f, "%c%c%c%c", i, i, i, 0);
341 for (i = 0; i < w * h; i++) {
343 img->comps[0].data[w * h - ((i) / (w) + 1) * w + (i) % (w)]);
344 if (((i + 1) % w == 0 && w % 2))