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