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 % http://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 status: Method IsPGX returns True if the image format type is PGX.
93 % o magick: This string is generally the first few bytes of an image file
96 % o length: Specifies the length of the magick string.
100 static unsigned int IsPGX(const unsigned char *magick,const size_t length)
104 if ((memcmp(magick,"PG ML",5) == 0) || (memcmp(magick,"PG LM",5) == 0))
110 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
114 % R e a d P G X I m a g e %
118 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
120 % ReadPGXImage() reads an image of raw bits in LSB order and returns it.
121 % It allocates the memory necessary for the new Image structure and returns
122 % a pointer to the new image.
124 % The format of the ReadPGXImage method is:
126 % Image *ReadPGXImage(const ImageInfo *image_info,
127 % ExceptionInfo *exception)
129 % A description of each parameter follows:
131 % o image_info: the image info.
133 % o exception: return any errors or warnings in this structure.
136 static Image *ReadPGXImage(const ImageInfo *image_info,ExceptionInfo *exception)
139 buffer[MagickPathExtent],
140 endian[MagickPathExtent],
141 sans[MagickPathExtent],
142 sign[MagickPathExtent];
171 assert(image_info != (const ImageInfo *) NULL);
172 assert(image_info->signature == MagickCoreSignature);
173 if (image_info->debug != MagickFalse)
174 (void) LogMagickEvent(TraceEvent,GetMagickModule(),"%s",
175 image_info->filename);
176 assert(exception != (ExceptionInfo *) NULL);
177 assert(exception->signature == MagickCoreSignature);
178 image=AcquireImage(image_info,exception);
179 status=OpenBlob(image_info,image,ReadBinaryBlobMode,exception);
180 if (status == MagickFalse)
182 image=DestroyImageList(image);
183 return((Image *) NULL);
185 if (ReadBlobString(image,buffer) == (char *) NULL)
186 ThrowReaderException(CorruptImageError,"ImproperImageHeader");
187 count=(ssize_t) sscanf(buffer,"PG%[ \t]%2s%[ \t+-]%d%[ \t]%d%[ \t]%d",sans,
188 endian,sign,&precision,sans,&width,sans,&height);
189 image->depth=(size_t) precision;
190 if (LocaleCompare(endian,"ML") == 0)
191 image->endian=MSBEndian;
192 image->columns=(size_t) width;
193 image->rows=(size_t) height;
194 if ((image->columns == 0) || (image->rows == 0))
195 ThrowReaderException(CorruptImageError,"ImproperImageHeader");
196 if (image_info->ping != MagickFalse)
198 (void) CloseBlob(image);
199 return(GetFirstImageInList(image));
201 status=SetImageExtent(image,image->columns,image->rows,exception);
202 if (status == MagickFalse)
203 return(DestroyImageList(image));
207 (void) SetImageColorspace(image,GRAYColorspace,exception);
208 quantum_info=AcquireQuantumInfo(image_info,image);
209 if (quantum_info == (QuantumInfo *) NULL)
210 ThrowReaderException(ResourceLimitError,"MemoryAllocationFailed");
211 length=GetQuantumExtent(image,quantum_info,GrayQuantum);
212 for (y=0; y < (ssize_t) image->rows; y++)
217 q=QueueAuthenticPixels(image,0,y,image->columns,1,exception);
218 if (q == (Quantum *) NULL)
220 pixels=(const unsigned char *) ReadBlobStream(image,length,
221 GetQuantumPixels(quantum_info),&count);
222 if (count != (ssize_t) length)
223 ThrowReaderException(CorruptImageError,"UnableToReadImageData");
224 (void) ImportQuantumPixels(image,(CacheView *) NULL,quantum_info,
225 GrayQuantum,pixels,exception);
226 if (SyncAuthenticPixels(image,exception) == MagickFalse)
228 if (SetImageProgress(image,LoadImageTag,(MagickOffsetType) y,image->rows) == MagickFalse)
231 SetQuantumImageType(image,GrayQuantum);
232 quantum_info=DestroyQuantumInfo(quantum_info);
233 if (EOFBlob(image) != MagickFalse)
234 ThrowFileException(exception,CorruptImageError,"UnexpectedEndOfFile",
236 (void) CloseBlob(image);
237 return(GetFirstImageInList(image));
241 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
245 % R e g i s t e r P G X I m a g e %
249 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
251 % RegisterPGXImage() adds attributes for the PGX image format to
252 % the list of supported formats. The attributes include the image format
253 % tag, a method to read and/or write the format, whether the format
254 % supports the saving of more than one frame to the same file or blob,
255 % whether the format supports native in-memory I/O, and a brief
256 % description of the format.
258 % The format of the RegisterPGXImage method is:
260 % size_t RegisterPGXImage(void)
263 ModuleExport size_t RegisterPGXImage(void)
268 entry=AcquireMagickInfo("PGX","PGX","JPEG 2000 uncompressed format");
269 entry->decoder=(DecodeImageHandler *) ReadPGXImage;
270 entry->encoder=(EncodeImageHandler *) WritePGXImage;
271 entry->magick=(IsImageFormatHandler *) IsPGX;
272 entry->flags^=CoderAdjoinFlag;
273 entry->flags^=CoderUseExtensionFlag;
274 (void) RegisterMagickInfo(entry);
275 return(MagickImageCoderSignature);
279 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
283 % U n r e g i s t e r P G X I m a g e %
287 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
289 % UnregisterPGXImage() removes format registrations made by the
290 % PGX module from the list of supported formats.
292 % The format of the UnregisterPGXImage method is:
294 % UnregisterPGXImage(void)
297 ModuleExport void UnregisterPGXImage(void)
299 (void) UnregisterMagickInfo("PGX");
303 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
307 % W r i t e P G X I m a g e %
311 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
313 % WritePGXImage() writes an image of raw bits in LSB order to a file.
315 % The format of the WritePGXImage method is:
317 % MagickBooleanType WritePGXImage(const ImageInfo *image_info,
318 % Image *image,ExceptionInfo *exception)
320 % A description of each parameter follows.
322 % o image_info: the image info.
324 % o image: The image.
326 % o exception: return any errors or warnings in this structure.
329 static MagickBooleanType WritePGXImage(const ImageInfo *image_info,Image *image,
330 ExceptionInfo *exception)
333 buffer[MagickPathExtent];
341 register const Quantum
355 Open output image file.
357 assert(image_info != (const ImageInfo *) NULL);
358 assert(image_info->signature == MagickCoreSignature);
359 assert(image != (Image *) NULL);
360 assert(image->signature == MagickCoreSignature);
361 if (image->debug != MagickFalse)
362 (void) LogMagickEvent(TraceEvent,GetMagickModule(),"%s",image->filename);
363 assert(exception != (ExceptionInfo *) NULL);
364 assert(exception->signature == MagickCoreSignature);
365 status=OpenBlob(image_info,image,WriteBinaryBlobMode,exception);
366 if (status == MagickFalse)
368 (void) FormatLocaleString(buffer,MagickPathExtent,"PG ML + %ld %lu %lu\n",
369 image->depth,image->columns,image->rows);
370 (void) WriteBlob(image,strlen(buffer),(unsigned char *) buffer);
371 (void) TransformImageColorspace(image,sRGBColorspace,exception);
372 quantum_info=AcquireQuantumInfo(image_info,image);
373 pixels=(unsigned char *) GetQuantumPixels(quantum_info);
374 for (y=0; y < (ssize_t) image->rows; y++)
376 p=GetVirtualPixels(image,0,y,image->columns,1,exception);
377 if (p == (const Quantum *) NULL)
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 ThrowWriterException(CorruptImageError,"UnableToWriteImageData");
384 count=WriteBlob(image,(size_t) (-(ssize_t) length) & 0x01,pixels);
385 status=SetImageProgress(image,SaveImageTag,(MagickOffsetType) y,
387 if (status == MagickFalse)
390 quantum_info=DestroyQuantumInfo(quantum_info);
391 (void) CloseBlob(image);