Engauge Digitizer  2
Functions
Jpeg2000Convert.cpp File Reference
#include "Logger.h"
#include "openjpeg.h"
#include "Jpeg2000Convert.h"
#include <QBuffer>
#include <QDataStream>
#include <QDebug>
#include <qmath.h>
#include <string.h>
Include dependency graph for Jpeg2000Convert.cpp:

Go to the source code of this file.

Functions

int imagetopnm (opj_image_t *image, QBuffer &buffer)
 

Function Documentation

◆ imagetopnm()

int imagetopnm ( opj_image_t *  image,
QBuffer &  buffer 
)

Definition at line 50 of file Jpeg2000Convert.cpp.

52 {
53  int *red, *green, *blue, *alpha = nullptr;
54  int wr, hr, max;
55  int i;
56  unsigned int compno, ncomp;
57  int adjustR, adjustG, adjustB, adjustA;
58  int two, has_alpha, triple;
59  int prec, v;
60  char bufferLocal[1024];
61  QDataStream str (&buffer);
62 
63  if((prec = qFloor (image->comps[0].prec)) > 16)
64  {
65  fprintf(stderr,"%s:%d:imagetopnm\n\tprecision %d is larger than 16"
66  "\n\t: refused.\n",__FILE__,__LINE__,prec);
67  return 1;
68  }
69  two = has_alpha = 0;
70  ncomp = image->numcomps;
71 
72  if (ncomp == 2 /* GRAYA */
73  || (ncomp > 2 /* RGB, RGBA */
74  && image->comps[0].dx == image->comps[1].dx
75  && image->comps[1].dx == image->comps[2].dx
76  && image->comps[0].dy == image->comps[1].dy
77  && image->comps[1].dy == image->comps[2].dy
78  && image->comps[0].prec == image->comps[1].prec
79  && image->comps[1].prec == image->comps[2].prec
80  ))
81  {
82  two = (prec > 8);
83  triple = (ncomp > 2);
84  wr = qFloor (image->comps[0].w);
85  hr = qFloor (image->comps[0].h);
86  max = (1<<prec) - 1; has_alpha = (ncomp == 4 || ncomp == 2);
87 
88  red = image->comps[0].data;
89 
90  if(triple)
91  {
92  green = image->comps[1].data;
93  blue = image->comps[2].data;
94  }
95  else green = blue = nullptr;
96 
97  if(has_alpha)
98  {
99  const char *tt = (triple?"RGB_ALPHA":"GRAYSCALE_ALPHA");
100 
101  sprintf(bufferLocal,
102  "P7\n# OpenJPEG-%s\nWIDTH %d\nHEIGHT %d\nDEPTH %d\n"
103  "MAXVAL %d\nTUPLTYPE %s\nENDHDR\n",
104  opj_version(),
105  wr,
106  hr,
107  ncomp,
108  max,
109  tt);
110  str.writeRawData (bufferLocal,
111  strlen (bufferLocal));
112  alpha = image->comps[ncomp - 1].data;
113  adjustA = (image->comps[ncomp - 1].sgnd ?
114  1 << (image->comps[ncomp - 1].prec - 1) : 0);
115  }
116  else
117  {
118  sprintf(bufferLocal,
119  "P6\n# OpenJPEG-%s\n%d %d\n%d\n",
120  opj_version(),
121  wr,
122  hr,
123  max);
124  str.writeRawData (bufferLocal,
125  strlen (bufferLocal));
126  adjustA = 0;
127  }
128  adjustR = (image->comps[0].sgnd ? 1 << (image->comps[0].prec - 1) : 0);
129 
130  if(triple)
131  {
132  adjustG = (image->comps[1].sgnd ? 1 << (image->comps[1].prec - 1) : 0);
133  adjustB = (image->comps[2].sgnd ? 1 << (image->comps[2].prec - 1) : 0);
134  }
135  else adjustG = adjustB = 0;
136 
137  for(i = 0; i < wr * hr; ++i)
138  {
139  if(two)
140  {
141  v = *red + adjustR; ++red;
142  if(v > 65535) v = 65535; else if(v < 0) v = 0;
143 
144  sprintf(bufferLocal,
145  "%c%c",
146  static_cast<unsigned char> (v>>8),
147  static_cast<unsigned char> (v));
148  str.writeRawData (bufferLocal,
149  2);
150 
151  if(triple)
152  {
153  v = *green + adjustG; ++green;
154  if(v > 65535) v = 65535; else if(v < 0) v = 0;
155 
156  sprintf(bufferLocal,
157  "%c%c",
158  static_cast<unsigned char> (v>>8),
159  static_cast<unsigned char> (v));
160  str.writeRawData (bufferLocal,
161  2);
162 
163  v = *blue + adjustB; ++blue;
164  if(v > 65535) v = 65535; else if(v < 0) v = 0;
165 
166  sprintf(bufferLocal,
167  "%c%c",
168  static_cast<unsigned char> (v>>8),
169  static_cast<unsigned char> (v));
170  str.writeRawData (bufferLocal,
171  2);
172 
173  }/* if(triple) */
174 
175  if(has_alpha)
176  {
177  v = *alpha + adjustA; ++alpha;
178  if(v > 65535) v = 65535; else if(v < 0) v = 0;
179 
180  sprintf(bufferLocal,
181  "%c%c",
182  static_cast<unsigned char> (v>>8),
183  static_cast<unsigned char> (v));
184  str.writeRawData (bufferLocal,
185  2);
186  }
187  continue;
188 
189  } /* if(two) */
190 
191  /* prec <= 8: */
192  v = *red++;
193  if(v > 255) v = 255; else if(v < 0) v = 0;
194 
195  sprintf(bufferLocal,
196  "%c",
197  static_cast<unsigned char> (v));
198  str.writeRawData (bufferLocal,
199  1);
200  if(triple)
201  {
202  v = *green++;
203  if(v > 255) v = 255; else if(v < 0) v = 0;
204 
205  sprintf(bufferLocal,
206  "%c",
207  static_cast<unsigned char> (v));
208  str.writeRawData (bufferLocal,
209  1);
210  v = *blue++;
211  if(v > 255) v = 255; else if(v < 0) v = 0;
212 
213  sprintf(bufferLocal,
214  "%c",
215  static_cast<unsigned char> (v));
216  str.writeRawData (bufferLocal,
217  1);
218  }
219  if(has_alpha)
220  {
221  v = *alpha++;
222  if(v > 255) v = 255; else if(v < 0) v = 0;
223 
224  sprintf(bufferLocal,
225  "%c",
226  static_cast<unsigned char> (v));
227  str.writeRawData (bufferLocal,
228  1);
229  }
230  } /* for(i */
231 
232  return 0;
233  }
234 
235  /* YUV or MONO: */
236 
237  if (image->numcomps > ncomp)
238  {
239  LOG4CPP_WARN_S ((*mainCat)) << "imagetopnm will only use the first component";
240  }
241 
242  for (compno = 0; compno < ncomp; compno++)
243  {
244  wr = qFloor (image->comps[compno].w);
245  hr = qFloor (image->comps[compno].h);
246  prec = qFloor (image->comps[compno].prec);
247  max = (1<<prec) - 1;
248 
249  sprintf(bufferLocal,
250  "P5\n#OpenJPEG-%s\n%d %d\n%d\n",
251  opj_version(),
252  wr,
253  hr,
254  max);
255  str.writeRawData (bufferLocal,
256  strlen (bufferLocal));
257 
258  red = image->comps[compno].data;
259  adjustR =
260  (image->comps[compno].sgnd ? 1 << (image->comps[compno].prec - 1) : 0);
261 
262  if(prec > 8)
263  {
264  for (i = 0; i < wr * hr; i++)
265  {
266  v = *red + adjustR; ++red;
267  if(v > 65535) v = 65535; else if(v < 0) v = 0;
268 
269  sprintf(bufferLocal,
270  "%c%c",
271  static_cast<unsigned char> (v>>8),
272  static_cast<unsigned char> (v));
273  str.writeRawData (bufferLocal,
274  2);
275 
276  if(has_alpha)
277  {
278  v = *alpha++;
279  if(v > 65535) v = 65535; else if(v < 0) v = 0;
280 
281  sprintf(bufferLocal,
282  "%c%c",
283  static_cast<unsigned char> (v>>8),
284  static_cast<unsigned char> (v));
285  str.writeRawData (bufferLocal,
286  2);
287  }
288  }/* for(i */
289  }
290  else /* prec <= 8 */
291  {
292  for(i = 0; i < wr * hr; ++i)
293  {
294  v = *red + adjustR; ++red;
295  if(v > 255) v = 255; else if(v < 0) v = 0;
296 
297  sprintf(bufferLocal,
298  "%c",
299  static_cast<unsigned char> (v));
300  str.writeRawData (bufferLocal,
301  1);
302  }
303  }
304  } /* for (compno */
305 
306  return 0;
307 }/* imagetopnm() */
log4cpp::Category * mainCat
Definition: Logger.cpp:14
#define LOG4CPP_WARN_S(logger)
Definition: convenience.h:14