2 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
13 % Read/Write LEGO Mindstorms EV3 Robot Graphics File %
20 % Copyright 1999-2015 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/pixel-accessor.h"
61 #include "MagickCore/quantum-private.h"
62 #include "MagickCore/static.h"
63 #include "MagickCore/string_.h"
64 #include "MagickCore/module.h"
65 #include "MagickCore/utility.h"
70 static MagickBooleanType
71 WriteRGFImage(const ImageInfo *,Image *,ExceptionInfo *);
74 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
78 % R e a d X B M I m a g e %
82 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
84 % ReadRGFImage() reads an RGF bitmap image file and returns it. It
85 % allocates the memory necessary for the new Image structure and returns a
86 % pointer to the new image.
88 % The format of the ReadRGFImage method is:
90 % Image *ReadRGFImage(const ImageInfo *image_info,ExceptionInfo *exception)
92 % A description of each parameter follows:
94 % o image_info: the image info.
96 % o exception: return any errors or warnings in this structure.
99 static Image *ReadRGFImage(const ImageInfo *image_info,ExceptionInfo *exception)
117 register unsigned char
130 assert(image_info != (const ImageInfo *) NULL);
131 assert(image_info->signature == MagickSignature);
132 if (image_info->debug != MagickFalse)
133 (void) LogMagickEvent(TraceEvent,GetMagickModule(),"%s",
134 image_info->filename);
135 assert(exception != (ExceptionInfo *) NULL);
136 assert(exception->signature == MagickSignature);
137 image=AcquireImage(image_info,exception);
138 status=OpenBlob(image_info,image,ReadBinaryBlobMode,exception);
139 if (status == MagickFalse)
141 image=DestroyImageList(image);
142 return((Image *) NULL);
147 image->columns = (unsigned long) ReadBlobByte(image);
148 image->rows = (unsigned long) ReadBlobByte(image);
150 image->storage_class=PseudoClass;
153 Initialize image structure.
155 if (AcquireImageColormap(image,image->colors,exception) == MagickFalse)
156 ThrowReaderException(ResourceLimitError,"MemoryAllocationFailed");
160 image->colormap[0].red=QuantumRange;
161 image->colormap[0].green=QuantumRange;
162 image->colormap[0].blue=QuantumRange;
163 image->colormap[1].red=(Quantum) 0;
164 image->colormap[1].green=(Quantum) 0;
165 image->colormap[1].blue=(Quantum) 0;
166 if (image_info->ping != MagickFalse)
168 (void) CloseBlob(image);
169 return(GetFirstImageInList(image));
171 status=SetImageExtent(image,image->columns,image->rows,exception);
172 if (status == MagickFalse)
173 return(DestroyImageList(image));
177 data=(unsigned char *) AcquireQuantumMemory(image->rows,image->columns*
179 if (data == (unsigned char *) NULL)
180 ThrowReaderException(ResourceLimitError,"MemoryAllocationFailed");
182 for (i=0; i < (ssize_t) (image->columns * image->rows); i++)
184 *p++=ReadBlobByte(image);
188 Convert RGF image to pixel packets.
191 for (y=0; y < (ssize_t) image->rows; y++)
193 q=QueueAuthenticPixels(image,0,y,image->columns,1,exception);
194 if (q == (Quantum *) NULL)
198 for (x=0; x < (ssize_t) image->columns; x++)
201 byte=(size_t) (*p++);
202 SetPixelIndex(image,(Quantum) ((byte & 0x01) != 0 ? 0x01 : 0x00),q);
207 q+=GetPixelChannels(image);
209 if (SyncAuthenticPixels(image,exception) == MagickFalse)
211 status=SetImageProgress(image,LoadImageTag,(MagickOffsetType) y,
213 if (status == MagickFalse)
216 data=(unsigned char *) RelinquishMagickMemory(data);
217 (void) SyncImage(image,exception);
218 (void) CloseBlob(image);
219 return(GetFirstImageInList(image));
223 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
227 % R e g i s t e r R G F I m a g e %
231 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
233 % RegisterRGFImage() adds attributes for the RGF image format to
234 % the list of supported formats. The attributes include the image format
235 % tag, a method to read and/or write the format, whether the format
236 % supports the saving of more than one frame to the same file or blob,
237 % whether the format supports native in-memory I/O, and a brief
238 % description of the format.
240 % The format of the RegisterRGFImage method is:
242 % size_t RegisterRGFImage(void)
245 ModuleExport size_t RegisterRGFImage(void)
250 entry=SetMagickInfo("RGF");
251 entry->decoder=(DecodeImageHandler *) ReadRGFImage;
252 entry->encoder=(EncodeImageHandler *) WriteRGFImage;
253 entry->adjoin=MagickFalse;
254 entry->description=ConstantString(
255 "LEGO Mindstorms EV3 Robot Graphic Format (black and white)");
256 entry->module=ConstantString("RGF");
257 (void) RegisterMagickInfo(entry);
258 return(MagickImageCoderSignature);
262 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
266 % U n r e g i s t e r R G F I m a g e %
270 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
272 % UnregisterRGFImage() removes format registrations made by the
273 % RGF module from the list of supported formats.
275 % The format of the UnregisterRGFImage method is:
277 % UnregisterRGFImage(void)
280 ModuleExport void UnregisterRGFImage(void)
282 (void) UnregisterMagickInfo("RGF");
286 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
290 % W r i t e R G F I m a g e %
294 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
296 % WriteRGFImage() writes an image to a file in the X bitmap format.
298 % The format of the WriteRGFImage method is:
300 % MagickBooleanType WriteRGFImage(const ImageInfo *image_info,
301 % Image *image,ExceptionInfo *exception)
303 % A description of each parameter follows.
305 % o image_info: the image info.
307 % o image: The image.
309 % o exception: return any errors or warnings in this structure.
312 static MagickBooleanType WriteRGFImage(const ImageInfo *image_info,Image *image,
313 ExceptionInfo *exception)
318 register const Quantum
332 Open output image file.
334 assert(image_info != (const ImageInfo *) NULL);
335 assert(image_info->signature == MagickSignature);
336 assert(image != (Image *) NULL);
337 assert(image->signature == MagickSignature);
338 if (image->debug != MagickFalse)
339 (void) LogMagickEvent(TraceEvent,GetMagickModule(),"%s",image->filename);
340 assert(exception != (ExceptionInfo *) NULL);
341 assert(exception->signature == MagickSignature);
342 status=OpenBlob(image_info,image,WriteBinaryBlobMode,exception);
343 if (status == MagickFalse)
345 (void) TransformImageColorspace(image,sRGBColorspace,exception);
346 if((image->columns > 255L) || (image->rows > 255L))
347 ThrowWriterException(ImageError,"Dimensions must be less than 255x255");
349 Write header (just the image dimensions)
351 (void) WriteBlobByte(image,image->columns & 0xff);
352 (void) WriteBlobByte(image,image->rows & 0xff);
354 Convert MIFF to bit pixels.
356 (void) SetImageType(image,BilevelType,exception);
359 for (y=0; y < (ssize_t) image->rows; y++)
361 p=GetVirtualPixels(image,0,y,image->columns,1,exception);
362 if (p == (const Quantum *) NULL)
366 for (x=0; x < (ssize_t) image->columns; x++)
369 if (GetPixelLuma(image,p) < (QuantumRange/2))
375 Write a bitmap byte to the image file.
377 (void) WriteBlobByte(image,(unsigned char) byte);
381 p+=GetPixelChannels(image);
384 (void) WriteBlobByte(image,(unsigned char) byte);
385 status=SetImageProgress(image,SaveImageTag,(MagickOffsetType) y,
387 if (status == MagickFalse)
390 (void) CloseBlob(image);