43 #include "Jpeg2000Convert.h" 45 #include <QDataStream> 49 int imagetopnm(opj_image_t * image,
52 int *red, *green, *blue, *alpha = NULL;
55 unsigned int compno, ncomp;
56 int adjustR, adjustG, adjustB, adjustA;
57 int two, has_alpha, triple;
59 char bufferLocal[1024];
60 QDataStream str (&buffer);
62 if((prec = (
int)image->comps[0].prec) > 16)
64 fprintf(stderr,
"%s:%d:imagetopnm\n\tprecision %d is larger than 16" 65 "\n\t: refused.\n",__FILE__,__LINE__,prec);
69 ncomp = image->numcomps;
73 && image->comps[0].dx == image->comps[1].dx
74 && image->comps[1].dx == image->comps[2].dx
75 && image->comps[0].dy == image->comps[1].dy
76 && image->comps[1].dy == image->comps[2].dy
77 && image->comps[0].prec == image->comps[1].prec
78 && image->comps[1].prec == image->comps[2].prec
83 wr = (int)image->comps[0].w; hr = (
int)image->comps[0].h;
84 max = (1<<prec) - 1; has_alpha = (ncomp == 4 || ncomp == 2);
86 red = image->comps[0].data;
90 green = image->comps[1].data;
91 blue = image->comps[2].data;
93 else green = blue = NULL;
97 const char *tt = (triple?
"RGB_ALPHA":
"GRAYSCALE_ALPHA");
100 "P7\n# OpenJPEG-%s\nWIDTH %d\nHEIGHT %d\nDEPTH %d\n" 101 "MAXVAL %d\nTUPLTYPE %s\nENDHDR\n",
108 str.writeRawData (bufferLocal,
109 strlen (bufferLocal));
110 alpha = image->comps[ncomp - 1].data;
111 adjustA = (image->comps[ncomp - 1].sgnd ?
112 1 << (image->comps[ncomp - 1].prec - 1) : 0);
117 "P6\n# OpenJPEG-%s\n%d %d\n%d\n",
122 str.writeRawData (bufferLocal,
123 strlen (bufferLocal));
126 adjustR = (image->comps[0].sgnd ? 1 << (image->comps[0].prec - 1) : 0);
130 adjustG = (image->comps[1].sgnd ? 1 << (image->comps[1].prec - 1) : 0);
131 adjustB = (image->comps[2].sgnd ? 1 << (image->comps[2].prec - 1) : 0);
133 else adjustG = adjustB = 0;
135 for(i = 0; i < wr * hr; ++i)
139 v = *red + adjustR; ++red;
140 if(v > 65535) v = 65535;
else if(v < 0) v = 0;
144 (
unsigned char)(v>>8),
146 str.writeRawData (bufferLocal,
151 v = *green + adjustG; ++green;
152 if(v > 65535) v = 65535;
else if(v < 0) v = 0;
156 (
unsigned char)(v>>8),
158 str.writeRawData (bufferLocal,
161 v = *blue + adjustB; ++blue;
162 if(v > 65535) v = 65535;
else if(v < 0) v = 0;
166 (
unsigned char)(v>>8),
168 str.writeRawData (bufferLocal,
175 v = *alpha + adjustA; ++alpha;
176 if(v > 65535) v = 65535;
else if(v < 0) v = 0;
180 (
unsigned char)(v>>8),
182 str.writeRawData (bufferLocal,
191 if(v > 255) v = 255;
else if(v < 0) v = 0;
196 str.writeRawData (bufferLocal,
201 if(v > 255) v = 255;
else if(v < 0) v = 0;
206 str.writeRawData (bufferLocal,
209 if(v > 255) v = 255;
else if(v < 0) v = 0;
214 str.writeRawData (bufferLocal,
220 if(v > 255) v = 255;
else if(v < 0) v = 0;
225 str.writeRawData (bufferLocal,
235 if (image->numcomps > ncomp)
237 LOG4CPP_WARN_S ((*mainCat)) <<
"imagetopnm will only use the first component";
240 for (compno = 0; compno < ncomp; compno++)
242 wr = (int)image->comps[compno].w;
243 hr = (
int)image->comps[compno].h;
244 prec = (int)image->comps[compno].prec;
248 "P5\n#OpenJPEG-%s\n%d %d\n%d\n",
253 str.writeRawData (bufferLocal,
254 strlen (bufferLocal));
256 red = image->comps[compno].data;
258 (image->comps[compno].sgnd ? 1 << (image->comps[compno].prec - 1) : 0);
262 for (i = 0; i < wr * hr; i++)
264 v = *red + adjustR; ++red;
265 if(v > 65535) v = 65535;
else if(v < 0) v = 0;
269 (
unsigned char)(v>>8), (
unsigned char)v);
270 str.writeRawData (bufferLocal,
276 if(v > 65535) v = 65535;
else if(v < 0) v = 0;
280 (
unsigned char)(v>>8), (
unsigned char)v);
281 str.writeRawData (bufferLocal,
288 for(i = 0; i < wr * hr; ++i)
290 v = *red + adjustR; ++red;
291 if(v > 255) v = 255;
else if(v < 0) v = 0;
296 str.writeRawData (bufferLocal,