• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /*
2 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
3 %                                                                             %
4 %                                                                             %
5 %                                                                             %
6 %                            PPPP    GGGG  X   X                              %
7 %                            P   P  G       X X                               %
8 %                            PPPP   G  GG    X                                %
9 %                            P      G   G   X X                               %
10 %                            P       GGG   X   X                              %
11 %                                                                             %
12 %                                                                             %
13 %                           PGX JPEG 2000 Format                              %
14 %                                                                             %
15 %                              Software Design                                %
16 %                                   Cristy                                    %
17 %                                 July 2016                                   %
18 %                                                                             %
19 %                                                                             %
20 %  Copyright 1999-2019 ImageMagick Studio LLC, a non-profit organization      %
21 %  dedicated to making software imaging solutions freely available.           %
22 %                                                                             %
23 %  You may not use this file except in compliance with the License.  You may  %
24 %  obtain a copy of the License at                                            %
25 %                                                                             %
26 %    https://imagemagick.org/script/license.php                               %
27 %                                                                             %
28 %  Unless required by applicable law or agreed to in writing, software        %
29 %  distributed under the License is distributed on an "AS IS" BASIS,          %
30 %  WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.   %
31 %  See the License for the specific language governing permissions and        %
32 %  limitations under the License.                                             %
33 %                                                                             %
34 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
35 %
36 %
37 */
38 
39 /*
40   Include declarations.
41 */
42 #include "MagickCore/studio.h"
43 #include "MagickCore/attribute.h"
44 #include "MagickCore/blob.h"
45 #include "MagickCore/blob-private.h"
46 #include "MagickCore/cache.h"
47 #include "MagickCore/color-private.h"
48 #include "MagickCore/colormap.h"
49 #include "MagickCore/colorspace.h"
50 #include "MagickCore/colorspace-private.h"
51 #include "MagickCore/exception.h"
52 #include "MagickCore/exception-private.h"
53 #include "MagickCore/image.h"
54 #include "MagickCore/image-private.h"
55 #include "MagickCore/list.h"
56 #include "MagickCore/magick.h"
57 #include "MagickCore/memory_.h"
58 #include "MagickCore/monitor.h"
59 #include "MagickCore/monitor-private.h"
60 #include "MagickCore/quantum-private.h"
61 #include "MagickCore/static.h"
62 #include "MagickCore/string_.h"
63 #include "MagickCore/module.h"
64 
65 /*
66   Forward declarations.
67 */
68 static MagickBooleanType
69   WritePGXImage(const ImageInfo *,Image *,ExceptionInfo *);
70 
71 /*
72 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
73 %                                                                             %
74 %                                                                             %
75 %                                                                             %
76 %   I s P G X                                                                 %
77 %                                                                             %
78 %                                                                             %
79 %                                                                             %
80 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
81 %
82 %  IsPGXreturns True if the image format type, identified by the magick
83 %  string, is PGX.
84 %
85 %  The format of the IsPGX method is:
86 %
87 %      unsigned int IsPGX(const unsigned char *magick,const size_t length)
88 %
89 %  A description of each parameter follows:
90 %
91 %    o magick: compare image format pattern against these bytes.
92 %
93 %    o length: Specifies the length of the magick string.
94 %
95 */
IsPGX(const unsigned char * magick,const size_t length)96 static unsigned int IsPGX(const unsigned char *magick,const size_t length)
97 {
98   if (length < 5)
99     return(MagickFalse);
100   if ((memcmp(magick,"PG ML",5) == 0) || (memcmp(magick,"PG LM",5) == 0))
101     return(MagickTrue);
102   return(MagickFalse);
103 }
104 
105 /*
106 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
107 %                                                                             %
108 %                                                                             %
109 %                                                                             %
110 %   R e a d P G X I m a g e                                                   %
111 %                                                                             %
112 %                                                                             %
113 %                                                                             %
114 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
115 %
116 %  ReadPGXImage() reads an image of raw bits in LSB order and returns it.
117 %  It allocates the memory necessary for the new Image structure and returns
118 %  a pointer to the new image.
119 %
120 %  The format of the ReadPGXImage method is:
121 %
122 %      Image *ReadPGXImage(const ImageInfo *image_info,
123 %        ExceptionInfo *exception)
124 %
125 %  A description of each parameter follows:
126 %
127 %    o image_info: the image info.
128 %
129 %    o exception: return any errors or warnings in this structure.
130 %
131 */
ReadPGXImage(const ImageInfo * image_info,ExceptionInfo * exception)132 static Image *ReadPGXImage(const ImageInfo *image_info,ExceptionInfo *exception)
133 {
134   char
135     buffer[MagickPathExtent],
136     endian[MagickPathExtent],
137     sans[MagickPathExtent],
138     sign[MagickPathExtent];
139 
140   const unsigned char
141     *pixels;
142 
143   Image
144     *image;
145 
146   int
147     height,
148     precision,
149     width;
150 
151   QuantumInfo
152     *quantum_info;
153 
154   MagickBooleanType
155     status;
156 
157   size_t
158     length;
159 
160   ssize_t
161     count,
162     y;
163 
164   /*
165     Open image file.
166   */
167   assert(image_info != (const ImageInfo *) NULL);
168   assert(image_info->signature == MagickCoreSignature);
169   if (image_info->debug != MagickFalse)
170     (void) LogMagickEvent(TraceEvent,GetMagickModule(),"%s",
171       image_info->filename);
172   assert(exception != (ExceptionInfo *) NULL);
173   assert(exception->signature == MagickCoreSignature);
174   image=AcquireImage(image_info,exception);
175   status=OpenBlob(image_info,image,ReadBinaryBlobMode,exception);
176   if (status == MagickFalse)
177     {
178       image=DestroyImageList(image);
179       return((Image *) NULL);
180     }
181   if (ReadBlobString(image,buffer) == (char *) NULL)
182     ThrowReaderException(CorruptImageError,"ImproperImageHeader");
183   count=(ssize_t) sscanf(buffer,"PG%[ \t]%2s%[ \t+-]%d%[ \t]%d%[ \t]%d",sans,
184     endian,sign,&precision,sans,&width,sans,&height);
185   if (count != 8)
186     ThrowReaderException(CorruptImageError,"ImproperImageHeader");
187   image->depth=(size_t) precision;
188   if (LocaleCompare(endian,"ML") == 0)
189     image->endian=MSBEndian;
190   image->columns=(size_t) width;
191   image->rows=(size_t) height;
192   if ((image->columns == 0) || (image->rows == 0))
193     ThrowReaderException(CorruptImageError,"ImproperImageHeader");
194   if (image_info->ping != MagickFalse)
195     {
196       (void) CloseBlob(image);
197       return(GetFirstImageInList(image));
198     }
199   status=SetImageExtent(image,image->columns,image->rows,exception);
200   if (status == MagickFalse)
201     return(DestroyImageList(image));
202   /*
203     Convert PGX image.
204   */
205   (void) SetImageColorspace(image,GRAYColorspace,exception);
206   quantum_info=AcquireQuantumInfo(image_info,image);
207   if (quantum_info == (QuantumInfo *) NULL)
208     ThrowReaderException(ResourceLimitError,"MemoryAllocationFailed");
209   length=GetQuantumExtent(image,quantum_info,GrayQuantum);
210   for (y=0; y < (ssize_t) image->rows; y++)
211   {
212     register Quantum
213       *magick_restrict q;
214 
215     q=QueueAuthenticPixels(image,0,y,image->columns,1,exception);
216     if (q == (Quantum *) NULL)
217       break;
218     pixels=(const unsigned char *) ReadBlobStream(image,length,
219       GetQuantumPixels(quantum_info),&count);
220     if (count != (ssize_t) length)
221       break;
222     (void) ImportQuantumPixels(image,(CacheView *) NULL,quantum_info,
223       GrayQuantum,pixels,exception);
224     if (SyncAuthenticPixels(image,exception) == MagickFalse)
225       break;
226     if (SetImageProgress(image,LoadImageTag,(MagickOffsetType) y,image->rows) == MagickFalse)
227       break;
228   }
229   SetQuantumImageType(image,GrayQuantum);
230   quantum_info=DestroyQuantumInfo(quantum_info);
231   if (EOFBlob(image) != MagickFalse)
232     ThrowFileException(exception,CorruptImageError,"UnexpectedEndOfFile",
233       image->filename);
234   (void) CloseBlob(image);
235   return(GetFirstImageInList(image));
236 }
237 
238 /*
239 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
240 %                                                                             %
241 %                                                                             %
242 %                                                                             %
243 %   R e g i s t e r P G X I m a g e                                           %
244 %                                                                             %
245 %                                                                             %
246 %                                                                             %
247 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
248 %
249 %  RegisterPGXImage() adds attributes for the PGX image format to
250 %  the list of supported formats.  The attributes include the image format
251 %  tag, a method to read and/or write the format, whether the format
252 %  supports the saving of more than one frame to the same file or blob,
253 %  whether the format supports native in-memory I/O, and a brief
254 %  description of the format.
255 %
256 %  The format of the RegisterPGXImage method is:
257 %
258 %      size_t RegisterPGXImage(void)
259 %
260 */
RegisterPGXImage(void)261 ModuleExport size_t RegisterPGXImage(void)
262 {
263   MagickInfo
264     *entry;
265 
266   entry=AcquireMagickInfo("PGX","PGX","JPEG 2000 uncompressed format");
267   entry->decoder=(DecodeImageHandler *) ReadPGXImage;
268   entry->encoder=(EncodeImageHandler *) WritePGXImage;
269   entry->magick=(IsImageFormatHandler *) IsPGX;
270   entry->flags^=CoderAdjoinFlag;
271   entry->flags^=CoderUseExtensionFlag;
272   (void) RegisterMagickInfo(entry);
273   return(MagickImageCoderSignature);
274 }
275 
276 /*
277 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
278 %                                                                             %
279 %                                                                             %
280 %                                                                             %
281 %   U n r e g i s t e r P G X I m a g e                                       %
282 %                                                                             %
283 %                                                                             %
284 %                                                                             %
285 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
286 %
287 %  UnregisterPGXImage() removes format registrations made by the
288 %  PGX module from the list of supported formats.
289 %
290 %  The format of the UnregisterPGXImage method is:
291 %
292 %      UnregisterPGXImage(void)
293 %
294 */
UnregisterPGXImage(void)295 ModuleExport void UnregisterPGXImage(void)
296 {
297   (void) UnregisterMagickInfo("PGX");
298 }
299 
300 /*
301 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
302 %                                                                             %
303 %                                                                             %
304 %                                                                             %
305 %   W r i t e P G X I m a g e                                                 %
306 %                                                                             %
307 %                                                                             %
308 %                                                                             %
309 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
310 %
311 %  WritePGXImage() writes an image of raw bits in LSB order to a file.
312 %
313 %  The format of the WritePGXImage method is:
314 %
315 %      MagickBooleanType WritePGXImage(const ImageInfo *image_info,
316 %        Image *image,ExceptionInfo *exception)
317 %
318 %  A description of each parameter follows.
319 %
320 %    o image_info: the image info.
321 %
322 %    o image:  The image.
323 %
324 %    o exception: return any errors or warnings in this structure.
325 %
326 */
WritePGXImage(const ImageInfo * image_info,Image * image,ExceptionInfo * exception)327 static MagickBooleanType WritePGXImage(const ImageInfo *image_info,Image *image,
328   ExceptionInfo *exception)
329 {
330   char
331     buffer[MagickPathExtent];
332 
333   MagickBooleanType
334     status;
335 
336   QuantumInfo
337     *quantum_info;
338 
339   register const Quantum
340     *p;
341 
342   size_t
343     length;
344 
345   ssize_t
346     count,
347     y;
348 
349   unsigned char
350     *pixels;
351 
352   /*
353     Open output image file.
354   */
355   assert(image_info != (const ImageInfo *) NULL);
356   assert(image_info->signature == MagickCoreSignature);
357   assert(image != (Image *) NULL);
358   assert(image->signature == MagickCoreSignature);
359   if (image->debug != MagickFalse)
360     (void) LogMagickEvent(TraceEvent,GetMagickModule(),"%s",image->filename);
361   assert(exception != (ExceptionInfo *) NULL);
362   assert(exception->signature == MagickCoreSignature);
363   status=OpenBlob(image_info,image,WriteBinaryBlobMode,exception);
364   if (status == MagickFalse)
365     return(status);
366   (void) FormatLocaleString(buffer,MagickPathExtent,"PG ML + %g %g %g\n",
367     (double) image->depth,(double) image->columns,(double) image->rows);
368   (void) WriteBlob(image,strlen(buffer),(unsigned char *) buffer);
369   (void) TransformImageColorspace(image,sRGBColorspace,exception);
370   quantum_info=AcquireQuantumInfo(image_info,image);
371   if (quantum_info == (QuantumInfo *) NULL)
372     ThrowWriterException(ResourceLimitError,"MemoryAllocationFailed");
373   pixels=(unsigned char *) GetQuantumPixels(quantum_info);
374   for (y=0; y < (ssize_t) image->rows; y++)
375   {
376     p=GetVirtualPixels(image,0,y,image->columns,1,exception);
377     if (p == (const Quantum *) NULL)
378       break;
379     length=ExportQuantumPixels(image,(CacheView *) NULL,quantum_info,
380       GrayQuantum,pixels,exception);
381     count=WriteBlob(image,length,pixels);
382     if (count != (ssize_t) length)
383       break;
384     count=WriteBlob(image,(size_t) (-(ssize_t) length) & 0x01,pixels);
385     status=SetImageProgress(image,SaveImageTag,(MagickOffsetType) y,
386       image->rows);
387     if (status == MagickFalse)
388       break;
389   }
390   quantum_info=DestroyQuantumInfo(quantum_info);
391   if (y < (ssize_t) image->rows)
392     ThrowWriterException(CorruptImageError,"UnableToWriteImageData");
393   (void) CloseBlob(image);
394   return(status);
395 }
396