2 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
13 % Read/Write MGK Image Format. %
20 % Copyright 1999-2011 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 "magick/studio.h"
43 #include "magick/blob.h"
44 #include "magick/blob-private.h"
45 #include "magick/cache.h"
46 #include "magick/colorspace.h"
47 #include "magick/exception.h"
48 #include "magick/exception-private.h"
49 #include "magick/image.h"
50 #include "magick/image-private.h"
51 #include "magick/list.h"
52 #include "magick/magick.h"
53 #include "magick/memory_.h"
54 #include "magick/monitor.h"
55 #include "magick/monitor-private.h"
56 #include "magick/quantum-private.h"
57 #include "magick/static.h"
58 #include "magick/string_.h"
59 #include "magick/module.h"
64 static MagickBooleanType
65 WriteMGKImage(const ImageInfo *,Image *);
68 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
76 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
78 % IsMGK() returns MagickTrue if the image format type, identified by the
79 % magick string, is MGK.
81 % The format of the IsMGK method is:
83 % MagickBooleanType IsMGK(const unsigned char *magick,const size_t length)
85 % A description of each parameter follows:
87 % o magick: This string is generally the first few bytes of an image file
90 % o length: Specifies the length of the magick string.
93 static MagickBooleanType IsMGK(const unsigned char *magick,const size_t length)
97 if (LocaleNCompare((char *) magick,"id=mgk",7) == 0)
103 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
107 % R e a d M G K I m a g e %
111 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
113 % ReadMGKImage() reads a MGK image file and returns it. It allocates
114 % the memory necessary for the new Image structure and returns a pointer to
117 % The format of the ReadMGKImage method is:
119 % Image *ReadMGKImage(const ImageInfo *image_info,
120 % ExceptionInfo *exception)
122 % A description of each parameter follows:
124 % o image_info: the image info.
126 % o exception: return any errors or warnings in this structure.
129 static Image *ReadMGKImage(const ImageInfo *image_info,
130 ExceptionInfo *exception)
133 buffer[MaxTextExtent];
147 register unsigned char
164 assert(image_info != (const ImageInfo *) NULL);
165 assert(image_info->signature == MagickSignature);
166 if (image_info->debug != MagickFalse)
167 (void) LogMagickEvent(TraceEvent,GetMagickModule(),"%s",
168 image_info->filename);
169 assert(exception != (ExceptionInfo *) NULL);
170 assert(exception->signature == MagickSignature);
171 image=AcquireImage(image_info);
172 status=OpenBlob(image_info,image,ReadBinaryBlobMode,exception);
173 if (status == MagickFalse)
175 image=DestroyImageList(image);
176 return((Image *) NULL);
181 (void) ReadBlobString(image,buffer); /* read magic number */
182 if (IsMGK(buffer,7) == MagickFalse)
183 ThrowReaderException(CorruptImageError,"ImproperImageHeader");
184 (void) ReadBlobString(image,buffer);
185 count=(ssize_t) sscanf(buffer,"%lu %lu\n",&columns,&rows);
187 ThrowReaderException(CorruptImageError,"ImproperImageHeader");
191 Initialize image structure.
193 image->columns=columns;
196 if ((image_info->ping != MagickFalse) && (image_info->number_scenes != 0))
197 if (image->scene >= (image_info->scene+image_info->number_scenes-1))
200 Convert MGK raster image to pixel packets.
202 if (SetImageExtent(image,0,0) == MagickFalse)
204 InheritException(exception,&image->exception);
205 return(DestroyImageList(image));
207 pixels=(unsigned char *) AcquireQuantumMemory((size_t) image->columns,
208 3UL*sizeof(*pixels));
209 if (pixels == (unsigned char *) NULL)
210 ThrowReaderException(ResourceLimitError,"MemoryAllocationFailed");
211 for (y=0; y < (ssize_t) image->rows; y++)
213 count=(ssize_t) ReadBlob(image,(size_t) (3*image->columns),pixels);
214 if (count != (ssize_t) (3*image->columns))
215 ThrowReaderException(CorruptImageError,"UnableToReadImageData");
217 q=QueueAuthenticPixels(image,0,y,image->columns,1,exception);
218 if (q == (PixelPacket *) NULL)
220 for (x=0; x < (ssize_t) image->columns; x++)
222 SetPixelRed(q,ScaleCharToQuantum(*p++));
223 SetPixelGreen(q,ScaleCharToQuantum(*p++));
224 SetPixelBlue(q,ScaleCharToQuantum(*p++));
227 if (SyncAuthenticPixels(image,exception) == MagickFalse)
229 if ((image->previous == (Image *) NULL) &&
230 (SetImageProgress(image,LoadImageTag,y,image->rows) == MagickFalse))
233 pixels=(unsigned char *) RelinquishMagickMemory(pixels);
234 if (EOFBlob(image) != MagickFalse)
236 ThrowFileException(exception,CorruptImageError,"UnexpectedEndOfFile",
241 Proceed to next image.
243 if (image_info->number_scenes != 0)
244 if (image->scene >= (image_info->scene+image_info->number_scenes-1))
247 (void) ReadBlobString(image,buffer);
248 count=(ssize_t) sscanf(buffer,"%lu %lu\n",&columns,&rows);
252 Allocate next image structure.
254 AcquireNextImage(image_info,image);
255 if (GetNextImageInList(image) == (Image *) NULL)
257 image=DestroyImageList(image);
258 return((Image *) NULL);
260 image=SyncNextImageInList(image);
261 if (image->progress_monitor != (MagickProgressMonitor) NULL)
263 status=SetImageProgress(image,LoadImageTag,TellBlob(image),
265 if (status == MagickFalse)
270 (void) CloseBlob(image);
271 return(GetFirstImageInList(image));
275 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
279 % R e g i s t e r M G K I m a g e %
283 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
285 % RegisterMGKImage() adds attributes for the MGK image format to
286 % the list of supported formats. The attributes include the image format
287 % tag, a method to read and/or write the format, whether the format
288 % supports the saving of more than one frame to the same file or blob,
289 % whether the format supports native in-memory I/O, and a brief
290 % description of the format.
292 % The format of the RegisterMGKImage method is:
294 % size_t RegisterMGKImage(void)
297 ModuleExport size_t RegisterMGKImage(void)
302 entry=SetMagickInfo("MGK");
303 entry->decoder=(DecodeImageHandler *) ReadMGKImage;
304 entry->encoder=(EncodeImageHandler *) WriteMGKImage;
305 entry->magick=(IsImageFormatHandler *) IsMGK;
306 entry->description=ConstantString("MGK");
307 entry->module=ConstantString("MGK");
308 (void) RegisterMagickInfo(entry);
309 return(MagickImageCoderSignature);
313 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
317 % U n r e g i s t e r M G K I m a g e %
321 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
323 % UnregisterMGKImage() removes format registrations made by the
324 % MGK module from the list of supported formats.
326 % The format of the UnregisterMGKImage method is:
328 % UnregisterMGKImage(void)
331 ModuleExport void UnregisterMGKImage(void)
333 (void) UnregisterMagickInfo("MGK");
337 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
341 % W r i t e M G K I m a g e %
345 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
347 % WriteMGKImage() writes an image to a file in red, green, and blue
348 % MGK rasterfile format.
350 % The format of the WriteMGKImage method is:
352 % MagickBooleanType WriteMGKImage(const ImageInfo *image_info,
355 % A description of each parameter follows.
357 % o image_info: the image info.
359 % o image: The image.
362 static MagickBooleanType WriteMGKImage(const ImageInfo *image_info,
366 buffer[MaxTextExtent];
374 register const PixelPacket
380 register unsigned char
390 Open output image file.
392 assert(image_info != (const ImageInfo *) NULL);
393 assert(image_info->signature == MagickSignature);
394 assert(image != (Image *) NULL);
395 assert(image->signature == MagickSignature);
396 if (image->debug != MagickFalse)
397 (void) LogMagickEvent(TraceEvent,GetMagickModule(),"%s",image->filename);
398 status=OpenBlob(image_info,image,WriteBinaryBlobMode,&image->exception);
399 if (status == MagickFalse)
405 Allocate memory for pixels.
407 if (image->colorspace != RGBColorspace)
408 (void) SetImageColorspace(image,RGBColorspace);
409 pixels=(unsigned char *) AcquireQuantumMemory((size_t) image->columns,
410 3UL*sizeof(*pixels));
411 if (pixels == (unsigned char *) NULL)
412 ThrowWriterException(ResourceLimitError,"MemoryAllocationFailed");
414 Initialize raster file header.
416 (void) WriteBlobString(image,"id=mgk\n");
417 (void) FormatMagickString(buffer,MaxTextExtent,"%lu %lu\n",image->columns,
419 (void) WriteBlobString(image,buffer);
420 for (y=0; y < (ssize_t) image->rows; y++)
422 p=AcquireImagePixels(image,0,y,image->columns,1,&image->exception);
423 if (p == (const PixelPacket *) NULL)
426 for (x=0; x < (ssize_t) image->columns; x++)
428 *q++=ScaleQuantumToChar(GetRedSample(p));
429 *q++=ScaleQuantumToChar(GetGreenSample(p));
430 *q++=ScaleQuantumToChar(GetBlueSample(p));
433 (void) WriteBlob(image,(size_t) (q-pixels),pixels);
434 if ((image->previous == (Image *) NULL) &&
435 (SetImageProgress(image,SaveImageTag,y,image->rows) == MagickFalse))
438 pixels=(unsigned char *) RelinquishMagickMemory(pixels);
439 if (GetNextImageInList(image) == (Image *) NULL)
441 image=SyncNextImageInList(image);
442 status=SetImageProgress(image,SaveImagesTag,scene,
443 GetImageListLength(image));
444 if (status == MagickFalse)
447 } while (image_info->adjoin != MagickFalse);
448 (void) CloseBlob(image);