• 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-2021 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   Image
141     *image;
142 
143   int
144     height,
145     precision,
146     width;
147 
148   QuantumInfo
149     *quantum_info;
150 
151   MagickBooleanType
152     status;
153 
154   size_t
155     length;
156 
157   ssize_t
158     count,
159     y;
160 
161   unsigned char
162     *pixels;
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   pixels=GetQuantumPixels(quantum_info);
211   for (y=0; y < (ssize_t) image->rows; y++)
212   {
213     const void
214       *stream;
215 
216     Quantum
217       *magick_restrict q;
218 
219     q=QueueAuthenticPixels(image,0,y,image->columns,1,exception);
220     if (q == (Quantum *) NULL)
221       break;
222     stream=ReadBlobStream(image,length,pixels,&count);
223     if (count != (ssize_t) length)
224       break;
225     (void) ImportQuantumPixels(image,(CacheView *) NULL,quantum_info,
226       GrayQuantum,(unsigned char *) stream,exception);
227     if (SyncAuthenticPixels(image,exception) == MagickFalse)
228       break;
229     if (SetImageProgress(image,LoadImageTag,(MagickOffsetType) y,image->rows) == MagickFalse)
230       break;
231   }
232   SetQuantumImageType(image,GrayQuantum);
233   quantum_info=DestroyQuantumInfo(quantum_info);
234   if (EOFBlob(image) != MagickFalse)
235     ThrowFileException(exception,CorruptImageError,"UnexpectedEndOfFile",
236       image->filename);
237   (void) CloseBlob(image);
238   return(GetFirstImageInList(image));
239 }
240 
241 /*
242 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
243 %                                                                             %
244 %                                                                             %
245 %                                                                             %
246 %   R e g i s t e r P G X I m a g e                                           %
247 %                                                                             %
248 %                                                                             %
249 %                                                                             %
250 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
251 %
252 %  RegisterPGXImage() adds attributes for the PGX image format to
253 %  the list of supported formats.  The attributes include the image format
254 %  tag, a method to read and/or write the format, whether the format
255 %  supports the saving of more than one frame to the same file or blob,
256 %  whether the format supports native in-memory I/O, and a brief
257 %  description of the format.
258 %
259 %  The format of the RegisterPGXImage method is:
260 %
261 %      size_t RegisterPGXImage(void)
262 %
263 */
RegisterPGXImage(void)264 ModuleExport size_t RegisterPGXImage(void)
265 {
266   MagickInfo
267     *entry;
268 
269   entry=AcquireMagickInfo("PGX","PGX","JPEG 2000 uncompressed format");
270   entry->decoder=(DecodeImageHandler *) ReadPGXImage;
271   entry->encoder=(EncodeImageHandler *) WritePGXImage;
272   entry->magick=(IsImageFormatHandler *) IsPGX;
273   entry->flags^=CoderAdjoinFlag;
274   entry->flags^=CoderUseExtensionFlag;
275   (void) RegisterMagickInfo(entry);
276   return(MagickImageCoderSignature);
277 }
278 
279 /*
280 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
281 %                                                                             %
282 %                                                                             %
283 %                                                                             %
284 %   U n r e g i s t e r P G X I m a g e                                       %
285 %                                                                             %
286 %                                                                             %
287 %                                                                             %
288 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
289 %
290 %  UnregisterPGXImage() removes format registrations made by the
291 %  PGX module from the list of supported formats.
292 %
293 %  The format of the UnregisterPGXImage method is:
294 %
295 %      UnregisterPGXImage(void)
296 %
297 */
UnregisterPGXImage(void)298 ModuleExport void UnregisterPGXImage(void)
299 {
300   (void) UnregisterMagickInfo("PGX");
301 }
302 
303 /*
304 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
305 %                                                                             %
306 %                                                                             %
307 %                                                                             %
308 %   W r i t e P G X I m a g e                                                 %
309 %                                                                             %
310 %                                                                             %
311 %                                                                             %
312 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
313 %
314 %  WritePGXImage() writes an image of raw bits in LSB order to a file.
315 %
316 %  The format of the WritePGXImage method is:
317 %
318 %      MagickBooleanType WritePGXImage(const ImageInfo *image_info,
319 %        Image *image,ExceptionInfo *exception)
320 %
321 %  A description of each parameter follows.
322 %
323 %    o image_info: the image info.
324 %
325 %    o image:  The image.
326 %
327 %    o exception: return any errors or warnings in this structure.
328 %
329 */
WritePGXImage(const ImageInfo * image_info,Image * image,ExceptionInfo * exception)330 static MagickBooleanType WritePGXImage(const ImageInfo *image_info,Image *image,
331   ExceptionInfo *exception)
332 {
333   char
334     buffer[MagickPathExtent];
335 
336   MagickBooleanType
337     status;
338 
339   QuantumInfo
340     *quantum_info;
341 
342   const Quantum
343     *p;
344 
345   size_t
346     length;
347 
348   ssize_t
349     count,
350     y;
351 
352   unsigned char
353     *pixels;
354 
355   /*
356     Open output image file.
357   */
358   assert(image_info != (const ImageInfo *) NULL);
359   assert(image_info->signature == MagickCoreSignature);
360   assert(image != (Image *) NULL);
361   assert(image->signature == MagickCoreSignature);
362   if (image->debug != MagickFalse)
363     (void) LogMagickEvent(TraceEvent,GetMagickModule(),"%s",image->filename);
364   assert(exception != (ExceptionInfo *) NULL);
365   assert(exception->signature == MagickCoreSignature);
366   status=OpenBlob(image_info,image,WriteBinaryBlobMode,exception);
367   if (status == MagickFalse)
368     return(status);
369   (void) FormatLocaleString(buffer,MagickPathExtent,"PG ML + %g %g %g\n",
370     (double) image->depth,(double) image->columns,(double) image->rows);
371   (void) WriteBlob(image,strlen(buffer),(unsigned char *) buffer);
372   (void) TransformImageColorspace(image,sRGBColorspace,exception);
373   quantum_info=AcquireQuantumInfo(image_info,image);
374   if (quantum_info == (QuantumInfo *) NULL)
375     ThrowWriterException(ResourceLimitError,"MemoryAllocationFailed");
376   pixels=(unsigned char *) GetQuantumPixels(quantum_info);
377   for (y=0; y < (ssize_t) image->rows; y++)
378   {
379     p=GetVirtualPixels(image,0,y,image->columns,1,exception);
380     if (p == (const Quantum *) NULL)
381       break;
382     length=ExportQuantumPixels(image,(CacheView *) NULL,quantum_info,
383       GrayQuantum,pixels,exception);
384     count=WriteBlob(image,length,pixels);
385     if (count != (ssize_t) length)
386       break;
387     count=WriteBlob(image,(size_t) (-(ssize_t) length) & 0x01,pixels);
388     status=SetImageProgress(image,SaveImageTag,(MagickOffsetType) y,
389       image->rows);
390     if (status == MagickFalse)
391       break;
392   }
393   quantum_info=DestroyQuantumInfo(quantum_info);
394   if (y < (ssize_t) image->rows)
395     ThrowWriterException(CorruptImageError,"UnableToWriteImageData");
396   (void) CloseBlob(image);
397   return(status);
398 }
399