Engauge Digitizer  2
Jpeg2000Convert.cpp
1 /*
2  * The copyright in this software is being made available under the 2-clauses
3  * BSD License, included below. This software may be subject to other third
4  * party and contributor rights, including patent rights, and no such rights
5  * are granted under this license.
6  *
7  * Copyright (c) 2002-2014, Universite catholique de Louvain (UCL), Belgium
8  * Copyright (c) 2002-2014, Professor Benoit Macq
9  * Copyright (c) 2001-2003, David Janssens
10  * Copyright (c) 2002-2003, Yannick Verschueren
11  * Copyright (c) 2003-2007, Francois-Olivier Devaux
12  * Copyright (c) 2003-2014, Antonin Descampe
13  * Copyright (c) 2005, Herve Drolon, FreeImage Team
14  * Copyright (c) 2006-2007, Parvatha Elangovan
15  * All rights reserved.
16  *
17  * Redistribution and use in source and binary forms, with or without
18  * modification, are permitted provided that the following conditions
19  * are met:
20  * 1. Redistributions of source code must retain the above copyright
21  * notice, this list of conditions and the following disclaimer.
22  * 2. Redistributions in binary form must reproduce the above copyright
23  * notice, this list of conditions and the following disclaimer in the
24  * documentation and/or other materials provided with the distribution.
25  *
26  * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS `AS IS'
27  * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
28  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
29  * ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE
30  * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
31  * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
32  * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
33  * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
34  * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
35  * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
36  * POSSIBILITY OF SUCH DAMAGE.
37  */
38 
39 //#include "opj_apps_config.h"
40 
41 #include "Logger.h"
42 #include "openjpeg.h"
43 #include "Jpeg2000Convert.h"
44 #include <QBuffer>
45 #include <QDataStream>
46 #include <QDebug>
47 #include <string.h>
48 
49 int imagetopnm(opj_image_t * image,
50  QBuffer &buffer)
51 {
52  int *red, *green, *blue, *alpha = NULL;
53  int wr, hr, max;
54  int i;
55  unsigned int compno, ncomp;
56  int adjustR, adjustG, adjustB, adjustA;
57  int two, has_alpha, triple;
58  int prec, v;
59  char bufferLocal[1024];
60  QDataStream str (&buffer);
61 
62  if((prec = (int)image->comps[0].prec) > 16)
63  {
64  fprintf(stderr,"%s:%d:imagetopnm\n\tprecision %d is larger than 16"
65  "\n\t: refused.\n",__FILE__,__LINE__,prec);
66  return 1;
67  }
68  two = has_alpha = 0;
69  ncomp = image->numcomps;
70 
71  if (ncomp == 2 /* GRAYA */
72  || (ncomp > 2 /* RGB, RGBA */
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
79  ))
80  {
81  two = (prec > 8);
82  triple = (ncomp > 2);
83  wr = (int)image->comps[0].w; hr = (int)image->comps[0].h;
84  max = (1<<prec) - 1; has_alpha = (ncomp == 4 || ncomp == 2);
85 
86  red = image->comps[0].data;
87 
88  if(triple)
89  {
90  green = image->comps[1].data;
91  blue = image->comps[2].data;
92  }
93  else green = blue = NULL;
94 
95  if(has_alpha)
96  {
97  const char *tt = (triple?"RGB_ALPHA":"GRAYSCALE_ALPHA");
98 
99  sprintf(bufferLocal,
100  "P7\n# OpenJPEG-%s\nWIDTH %d\nHEIGHT %d\nDEPTH %d\n"
101  "MAXVAL %d\nTUPLTYPE %s\nENDHDR\n",
102  opj_version(),
103  wr,
104  hr,
105  ncomp,
106  max,
107  tt);
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);
113  }
114  else
115  {
116  sprintf(bufferLocal,
117  "P6\n# OpenJPEG-%s\n%d %d\n%d\n",
118  opj_version(),
119  wr,
120  hr,
121  max);
122  str.writeRawData (bufferLocal,
123  strlen (bufferLocal));
124  adjustA = 0;
125  }
126  adjustR = (image->comps[0].sgnd ? 1 << (image->comps[0].prec - 1) : 0);
127 
128  if(triple)
129  {
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);
132  }
133  else adjustG = adjustB = 0;
134 
135  for(i = 0; i < wr * hr; ++i)
136  {
137  if(two)
138  {
139  v = *red + adjustR; ++red;
140  if(v > 65535) v = 65535; else if(v < 0) v = 0;
141 
142  sprintf(bufferLocal,
143  "%c%c",
144  (unsigned char)(v>>8),
145  (unsigned char)v);
146  str.writeRawData (bufferLocal,
147  2);
148 
149  if(triple)
150  {
151  v = *green + adjustG; ++green;
152  if(v > 65535) v = 65535; else if(v < 0) v = 0;
153 
154  sprintf(bufferLocal,
155  "%c%c",
156  (unsigned char)(v>>8),
157  (unsigned char)v);
158  str.writeRawData (bufferLocal,
159  2);
160 
161  v = *blue + adjustB; ++blue;
162  if(v > 65535) v = 65535; else if(v < 0) v = 0;
163 
164  sprintf(bufferLocal,
165  "%c%c",
166  (unsigned char)(v>>8),
167  (unsigned char)v);
168  str.writeRawData (bufferLocal,
169  2);
170 
171  }/* if(triple) */
172 
173  if(has_alpha)
174  {
175  v = *alpha + adjustA; ++alpha;
176  if(v > 65535) v = 65535; else if(v < 0) v = 0;
177 
178  sprintf(bufferLocal,
179  "%c%c",
180  (unsigned char)(v>>8),
181  (unsigned char)v);
182  str.writeRawData (bufferLocal,
183  2);
184  }
185  continue;
186 
187  } /* if(two) */
188 
189  /* prec <= 8: */
190  v = *red++;
191  if(v > 255) v = 255; else if(v < 0) v = 0;
192 
193  sprintf(bufferLocal,
194  "%c",
195  (unsigned char)v);
196  str.writeRawData (bufferLocal,
197  1);
198  if(triple)
199  {
200  v = *green++;
201  if(v > 255) v = 255; else if(v < 0) v = 0;
202 
203  sprintf(bufferLocal,
204  "%c",
205  (unsigned char)v);
206  str.writeRawData (bufferLocal,
207  1);
208  v = *blue++;
209  if(v > 255) v = 255; else if(v < 0) v = 0;
210 
211  sprintf(bufferLocal,
212  "%c",
213  (unsigned char)v);
214  str.writeRawData (bufferLocal,
215  1);
216  }
217  if(has_alpha)
218  {
219  v = *alpha++;
220  if(v > 255) v = 255; else if(v < 0) v = 0;
221 
222  sprintf(bufferLocal,
223  "%c",
224  (unsigned char)v);
225  str.writeRawData (bufferLocal,
226  1);
227  }
228  } /* for(i */
229 
230  return 0;
231  }
232 
233  /* YUV or MONO: */
234 
235  if (image->numcomps > ncomp)
236  {
237  LOG4CPP_WARN_S ((*mainCat)) << "imagetopnm will only use the first component";
238  }
239 
240  for (compno = 0; compno < ncomp; compno++)
241  {
242  wr = (int)image->comps[compno].w;
243  hr = (int)image->comps[compno].h;
244  prec = (int)image->comps[compno].prec;
245  max = (1<<prec) - 1;
246 
247  sprintf(bufferLocal,
248  "P5\n#OpenJPEG-%s\n%d %d\n%d\n",
249  opj_version(),
250  wr,
251  hr,
252  max);
253  str.writeRawData (bufferLocal,
254  strlen (bufferLocal));
255 
256  red = image->comps[compno].data;
257  adjustR =
258  (image->comps[compno].sgnd ? 1 << (image->comps[compno].prec - 1) : 0);
259 
260  if(prec > 8)
261  {
262  for (i = 0; i < wr * hr; i++)
263  {
264  v = *red + adjustR; ++red;
265  if(v > 65535) v = 65535; else if(v < 0) v = 0;
266 
267  sprintf(bufferLocal,
268  "%c%c",
269  (unsigned char)(v>>8), (unsigned char)v);
270  str.writeRawData (bufferLocal,
271  2);
272 
273  if(has_alpha)
274  {
275  v = *alpha++;
276  if(v > 65535) v = 65535; else if(v < 0) v = 0;
277 
278  sprintf(bufferLocal,
279  "%c%c",
280  (unsigned char)(v>>8), (unsigned char)v);
281  str.writeRawData (bufferLocal,
282  2);
283  }
284  }/* for(i */
285  }
286  else /* prec <= 8 */
287  {
288  for(i = 0; i < wr * hr; ++i)
289  {
290  v = *red + adjustR; ++red;
291  if(v > 255) v = 255; else if(v < 0) v = 0;
292 
293  sprintf(bufferLocal,
294  "%c",
295  (unsigned char)v);
296  str.writeRawData (bufferLocal,
297  1);
298  }
299  }
300  } /* for (compno */
301 
302  return 0;
303 }/* imagetopnm() */