2 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
13 % PGX JPEG 2000 Format %
20 % Copyright 1999-2017 ImageMagick Studio LLC, a non-profit organization %
21 % dedicated to making software imaging solutions freely available. %
23 % You may not use this file except in compliance with the License. You may %
24 % obtain a copy of the License at %
26 % https://www.imagemagick.org/script/license.php %
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. %
34 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
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"
68 static MagickBooleanType
69 WritePGXImage(const ImageInfo *,Image *,ExceptionInfo *);
72 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
80 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
82 % IsPGXreturns True if the image format type, identified by the magick
85 % The format of the IsPGX method is:
87 % unsigned int IsPGX(const unsigned char *magick,const size_t length)
89 % A description of each parameter follows:
91 % o magick: compare image format pattern against these bytes.
93 % o length: Specifies the length of the magick string.
96 static unsigned int IsPGX(const unsigned char *magick,const size_t length)
100 if ((memcmp(magick,"PG ML",5) == 0) || (memcmp(magick,"PG LM",5) == 0))
106 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
110 % R e a d P G X I m a g e %
114 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
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.
120 % The format of the ReadPGXImage method is:
122 % Image *ReadPGXImage(const ImageInfo *image_info,
123 % ExceptionInfo *exception)
125 % A description of each parameter follows:
127 % o image_info: the image info.
129 % o exception: return any errors or warnings in this structure.
132 static Image *ReadPGXImage(const ImageInfo *image_info,ExceptionInfo *exception)
135 buffer[MagickPathExtent],
136 endian[MagickPathExtent],
137 sans[MagickPathExtent],
138 sign[MagickPathExtent];
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)
178 image=DestroyImageList(image);
179 return((Image *) NULL);
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 image->depth=(size_t) precision;
186 if (LocaleCompare(endian,"ML") == 0)
187 image->endian=MSBEndian;
188 image->columns=(size_t) width;
189 image->rows=(size_t) height;
190 if ((image->columns == 0) || (image->rows == 0))
191 ThrowReaderException(CorruptImageError,"ImproperImageHeader");
192 if (image_info->ping != MagickFalse)
194 (void) CloseBlob(image);
195 return(GetFirstImageInList(image));
197 status=SetImageExtent(image,image->columns,image->rows,exception);
198 if (status == MagickFalse)
199 return(DestroyImageList(image));
203 (void) SetImageColorspace(image,GRAYColorspace,exception);
204 quantum_info=AcquireQuantumInfo(image_info,image);
205 if (quantum_info == (QuantumInfo *) NULL)
206 ThrowReaderException(ResourceLimitError,"MemoryAllocationFailed");
207 length=GetQuantumExtent(image,quantum_info,GrayQuantum);
208 for (y=0; y < (ssize_t) image->rows; y++)
213 q=QueueAuthenticPixels(image,0,y,image->columns,1,exception);
214 if (q == (Quantum *) NULL)
216 pixels=(const unsigned char *) ReadBlobStream(image,length,
217 GetQuantumPixels(quantum_info),&count);
218 if (count != (ssize_t) length)
219 ThrowReaderException(CorruptImageError,"UnableToReadImageData");
220 (void) ImportQuantumPixels(image,(CacheView *) NULL,quantum_info,
221 GrayQuantum,pixels,exception);
222 if (SyncAuthenticPixels(image,exception) == MagickFalse)
224 if (SetImageProgress(image,LoadImageTag,(MagickOffsetType) y,image->rows) == MagickFalse)
227 SetQuantumImageType(image,GrayQuantum);
228 quantum_info=DestroyQuantumInfo(quantum_info);
229 if (EOFBlob(image) != MagickFalse)
230 ThrowFileException(exception,CorruptImageError,"UnexpectedEndOfFile",
232 (void) CloseBlob(image);
233 return(GetFirstImageInList(image));
237 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
241 % R e g i s t e r P G X I m a g e %
245 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
247 % RegisterPGXImage() adds attributes for the PGX image format to
248 % the list of supported formats. The attributes include the image format
249 % tag, a method to read and/or write the format, whether the format
250 % supports the saving of more than one frame to the same file or blob,
251 % whether the format supports native in-memory I/O, and a brief
252 % description of the format.
254 % The format of the RegisterPGXImage method is:
256 % size_t RegisterPGXImage(void)
259 ModuleExport size_t RegisterPGXImage(void)
264 entry=AcquireMagickInfo("PGX","PGX","JPEG 2000 uncompressed format");
265 entry->decoder=(DecodeImageHandler *) ReadPGXImage;
266 entry->encoder=(EncodeImageHandler *) WritePGXImage;
267 entry->magick=(IsImageFormatHandler *) IsPGX;
268 entry->flags^=CoderAdjoinFlag;
269 entry->flags^=CoderUseExtensionFlag;
270 (void) RegisterMagickInfo(entry);
271 return(MagickImageCoderSignature);
275 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
279 % U n r e g i s t e r P G X I m a g e %
283 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
285 % UnregisterPGXImage() removes format registrations made by the
286 % PGX module from the list of supported formats.
288 % The format of the UnregisterPGXImage method is:
290 % UnregisterPGXImage(void)
293 ModuleExport void UnregisterPGXImage(void)
295 (void) UnregisterMagickInfo("PGX");
299 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
303 % W r i t e P G X I m a g e %
307 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
309 % WritePGXImage() writes an image of raw bits in LSB order to a file.
311 % The format of the WritePGXImage method is:
313 % MagickBooleanType WritePGXImage(const ImageInfo *image_info,
314 % Image *image,ExceptionInfo *exception)
316 % A description of each parameter follows.
318 % o image_info: the image info.
320 % o image: The image.
322 % o exception: return any errors or warnings in this structure.
325 static MagickBooleanType WritePGXImage(const ImageInfo *image_info,Image *image,
326 ExceptionInfo *exception)
329 buffer[MagickPathExtent];
337 register const Quantum
351 Open output image file.
353 assert(image_info != (const ImageInfo *) NULL);
354 assert(image_info->signature == MagickCoreSignature);
355 assert(image != (Image *) NULL);
356 assert(image->signature == MagickCoreSignature);
357 if (image->debug != MagickFalse)
358 (void) LogMagickEvent(TraceEvent,GetMagickModule(),"%s",image->filename);
359 assert(exception != (ExceptionInfo *) NULL);
360 assert(exception->signature == MagickCoreSignature);
361 status=OpenBlob(image_info,image,WriteBinaryBlobMode,exception);
362 if (status == MagickFalse)
364 (void) FormatLocaleString(buffer,MagickPathExtent,"PG ML + %ld %lu %lu\n",
365 image->depth,image->columns,image->rows);
366 (void) WriteBlob(image,strlen(buffer),(unsigned char *) buffer);
367 (void) TransformImageColorspace(image,sRGBColorspace,exception);
368 quantum_info=AcquireQuantumInfo(image_info,image);
369 if(quantum_info == (QuantumInfo *) NULL)
370 ThrowWriterException(ResourceLimitError,"MemoryAllocationFailed");
371 pixels=(unsigned char *) GetQuantumPixels(quantum_info);
372 for (y=0; y < (ssize_t) image->rows; y++)
374 p=GetVirtualPixels(image,0,y,image->columns,1,exception);
375 if (p == (const Quantum *) NULL)
377 length=ExportQuantumPixels(image,(CacheView *) NULL,quantum_info,
378 GrayQuantum,pixels,exception);
379 count=WriteBlob(image,length,pixels);
380 if (count != (ssize_t) length)
382 count=WriteBlob(image,(size_t) (-(ssize_t) length) & 0x01,pixels);
383 status=SetImageProgress(image,SaveImageTag,(MagickOffsetType) y,
385 if (status == MagickFalse)
388 quantum_info=DestroyQuantumInfo(quantum_info);
389 if (y < (ssize_t) image->rows)
390 ThrowWriterException(CorruptImageError,"UnableToWriteImageData");
391 (void) CloseBlob(image);