42 #include "Jpeg2000Convert.h"
44 #include <QDataStream>
48 int imagetopnm(opj_image_t * image,
51 int *red, *green, *blue, *alpha = NULL;
54 unsigned int compno, ncomp;
55 int adjustR, adjustG, adjustB, adjustA;
56 int two, has_alpha, triple;
58 char bufferLocal[1024];
59 QDataStream str (&buffer);
61 if((prec = (
int)image->comps[0].prec) > 16)
63 fprintf(stderr,
"%s:%d:imagetopnm\n\tprecision %d is larger than 16"
64 "\n\t: refused.\n",__FILE__,__LINE__,prec);
68 ncomp = image->numcomps;
72 && image->comps[0].dx == image->comps[1].dx
73 && image->comps[1].dx == image->comps[2].dx
74 && image->comps[0].dy == image->comps[1].dy
75 && image->comps[1].dy == image->comps[2].dy
76 && image->comps[0].prec == image->comps[1].prec
77 && image->comps[1].prec == image->comps[2].prec
82 wr = (int)image->comps[0].w; hr = (
int)image->comps[0].h;
83 max = (1<<prec) - 1; has_alpha = (ncomp == 4 || ncomp == 2);
85 red = image->comps[0].data;
89 green = image->comps[1].data;
90 blue = image->comps[2].data;
92 else green = blue = NULL;
96 const char *tt = (triple?
"RGB_ALPHA":
"GRAYSCALE_ALPHA");
99 "P7\n# OpenJPEG-%s\nWIDTH %d\nHEIGHT %d\nDEPTH %d\n"
100 "MAXVAL %d\nTUPLTYPE %s\nENDHDR\n",
107 str.writeRawData (bufferLocal,
108 strlen (bufferLocal));
109 alpha = image->comps[ncomp - 1].data;
110 adjustA = (image->comps[ncomp - 1].sgnd ?
111 1 << (image->comps[ncomp - 1].prec - 1) : 0);
116 "P6\n# OpenJPEG-%s\n%d %d\n%d\n",
121 str.writeRawData (bufferLocal,
122 strlen (bufferLocal));
125 adjustR = (image->comps[0].sgnd ? 1 << (image->comps[0].prec - 1) : 0);
129 adjustG = (image->comps[1].sgnd ? 1 << (image->comps[1].prec - 1) : 0);
130 adjustB = (image->comps[2].sgnd ? 1 << (image->comps[2].prec - 1) : 0);
132 else adjustG = adjustB = 0;
134 for(i = 0; i < wr * hr; ++i)
138 v = *red + adjustR; ++red;
139 if(v > 65535) v = 65535;
else if(v < 0) v = 0;
143 (
unsigned char)(v>>8),
145 str.writeRawData (bufferLocal,
150 v = *green + adjustG; ++green;
151 if(v > 65535) v = 65535;
else if(v < 0) v = 0;
155 (
unsigned char)(v>>8),
157 str.writeRawData (bufferLocal,
160 v = *blue + adjustB; ++blue;
161 if(v > 65535) v = 65535;
else if(v < 0) v = 0;
165 (
unsigned char)(v>>8),
167 str.writeRawData (bufferLocal,
174 v = *alpha + adjustA; ++alpha;
175 if(v > 65535) v = 65535;
else if(v < 0) v = 0;
179 (
unsigned char)(v>>8),
181 str.writeRawData (bufferLocal,
190 if(v > 255) v = 255;
else if(v < 0) v = 0;
195 str.writeRawData (bufferLocal,
200 if(v > 255) v = 255;
else if(v < 0) v = 0;
205 str.writeRawData (bufferLocal,
208 if(v > 255) v = 255;
else if(v < 0) v = 0;
213 str.writeRawData (bufferLocal,
219 if(v > 255) v = 255;
else if(v < 0) v = 0;
224 str.writeRawData (bufferLocal,
234 if (image->numcomps > ncomp)
236 LOG4CPP_WARN_S ((*mainCat)) <<
"imagetopnm will only use the first component";
239 for (compno = 0; compno < ncomp; compno++)
241 wr = (int)image->comps[compno].w;
242 hr = (
int)image->comps[compno].h;
243 prec = (int)image->comps[compno].prec;
247 "P5\n#OpenJPEG-%s\n%d %d\n%d\n",
252 str.writeRawData (bufferLocal,
253 strlen (bufferLocal));
255 red = image->comps[compno].data;
257 (image->comps[compno].sgnd ? 1 << (image->comps[compno].prec - 1) : 0);
261 for (i = 0; i < wr * hr; i++)
263 v = *red + adjustR; ++red;
264 if(v > 65535) v = 65535;
else if(v < 0) v = 0;
268 (
unsigned char)(v>>8), (
unsigned char)v);
269 str.writeRawData (bufferLocal,
275 if(v > 65535) v = 65535;
else if(v < 0) v = 0;
279 (
unsigned char)(v>>8), (
unsigned char)v);
280 str.writeRawData (bufferLocal,
287 for(i = 0; i < wr * hr; ++i)
289 v = *red + adjustR; ++red;
290 if(v > 255) v = 255;
else if(v < 0) v = 0;
295 str.writeRawData (bufferLocal,