blob: 95a4608ae2ac70fe3464f675b2350c7ada285bdd [file] [log] [blame]
cristy47c10da2013-08-26 23:06:51 +00001/*
2%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
3% %
4% %
5% %
6% RRRR GGG FFFFF %
7% R R G F %
8% RRRR G GG FFF %
9% R R G G F %
10% R R GGG F %
11% %
12% %
13% Read/Write LEGO Mindstorms EV3 Robot Graphics File %
14% %
15% Software Design %
16% Brian Wheeler %
17% August 2013 %
18% %
19% %
cristyfe676ee2013-11-18 13:03:38 +000020% Copyright 1999-2014 ImageMagick Studio LLC, a non-profit organization %
cristy47c10da2013-08-26 23:06:51 +000021% 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% http://www.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/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"
66
67/*
68 Forward declarations.
69*/
70static MagickBooleanType
71 WriteRGFImage(const ImageInfo *,Image *,ExceptionInfo *);
72
73/*
74%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
75% %
76% %
77% %
78% R e a d X B M I m a g e %
79% %
80% %
81% %
82%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
83%
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.
87%
88% The format of the ReadRGFImage method is:
89%
90% Image *ReadRGFImage(const ImageInfo *image_info,ExceptionInfo *exception)
91%
92% A description of each parameter follows:
93%
94% o image_info: the image info.
95%
96% o exception: return any errors or warnings in this structure.
97%
98*/
cristy47c10da2013-08-26 23:06:51 +000099static Image *ReadRGFImage(const ImageInfo *image_info,ExceptionInfo *exception)
100{
101 Image
102 *image;
103
104 MagickBooleanType
105 status;
106
107 register ssize_t
108 i,
109 x;
110
111 register Quantum
112 *q;
113
114 register unsigned char
115 *p;
116
117 size_t
118 bit,
cristy7cf0a6b2013-09-01 18:55:38 +0000119 byte;
cristy47c10da2013-08-26 23:06:51 +0000120
121 ssize_t
122 y;
123
124 unsigned char
125 *data;
126
127
128 /*
129 Open image file.
130 */
131 assert(image_info != (const ImageInfo *) NULL);
132 assert(image_info->signature == MagickSignature);
133 if (image_info->debug != MagickFalse)
134 (void) LogMagickEvent(TraceEvent,GetMagickModule(),"%s",
135 image_info->filename);
136 assert(exception != (ExceptionInfo *) NULL);
137 assert(exception->signature == MagickSignature);
138 image=AcquireImage(image_info,exception);
139 status=OpenBlob(image_info,image,ReadBinaryBlobMode,exception);
140 if (status == MagickFalse)
141 {
142 image=DestroyImageList(image);
143 return((Image *) NULL);
144 }
145 /*
146 Read RGF header.
147 */
148 image->columns = (unsigned long) ReadBlobByte(image);
149 image->rows = (unsigned long) ReadBlobByte(image);
150 image->depth=8;
151 image->storage_class=PseudoClass;
152 image->colors=2;
153 /*
154 Initialize image structure.
155 */
156 if (AcquireImageColormap(image,image->colors,exception) == MagickFalse)
157 ThrowReaderException(ResourceLimitError,"MemoryAllocationFailed");
158 /*
159 Initialize colormap.
160 */
161 image->colormap[0].red=QuantumRange;
162 image->colormap[0].green=QuantumRange;
163 image->colormap[0].blue=QuantumRange;
164 image->colormap[1].red=(Quantum) 0;
165 image->colormap[1].green=(Quantum) 0;
166 image->colormap[1].blue=(Quantum) 0;
167 if (image_info->ping != MagickFalse)
168 {
169 (void) CloseBlob(image);
170 return(GetFirstImageInList(image));
171 }
172
173
174 /*
175 Read hex image data.
176 */
cristy47c10da2013-08-26 23:06:51 +0000177 data=(unsigned char *) AcquireQuantumMemory(image->rows,image->columns*
178 sizeof(*data));
179 if (data == (unsigned char *) NULL)
180 ThrowReaderException(ResourceLimitError,"MemoryAllocationFailed");
181 p=data;
182 for (i=0; i < (ssize_t) (image->columns * image->rows); i++)
183 {
184 *p++=ReadBlobByte(image);
185 }
186
187 /*
188 Convert RGF image to pixel packets.
189 */
190 p=data;
191 for (y=0; y < (ssize_t) image->rows; y++)
192 {
193 q=QueueAuthenticPixels(image,0,y,image->columns,1,exception);
194 if (q == (Quantum *) NULL)
195 break;
196 bit=0;
197 byte=0;
198 for (x=0; x < (ssize_t) image->columns; x++)
199 {
200 if (bit == 0)
201 byte=(size_t) (*p++);
202 SetPixelIndex(image,(Quantum) ((byte & 0x01) != 0 ? 0x01 : 0x00),q);
203 bit++;
204 byte>>=1;
205 if (bit == 8)
206 bit=0;
207 q+=GetPixelChannels(image);
208 }
209 if (SyncAuthenticPixels(image,exception) == MagickFalse)
210 break;
211 status=SetImageProgress(image,LoadImageTag,(MagickOffsetType) y,
212 image->rows);
213 if (status == MagickFalse)
214 break;
215 }
216 data=(unsigned char *) RelinquishMagickMemory(data);
217 (void) SyncImage(image,exception);
218 (void) CloseBlob(image);
219 return(GetFirstImageInList(image));
220}
221
222/*
223%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
224% %
225% %
226% %
227% R e g i s t e r R G F I m a g e %
228% %
229% %
230% %
231%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
232%
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.
239%
240% The format of the RegisterRGFImage method is:
241%
242% size_t RegisterRGFImage(void)
243%
244*/
245ModuleExport size_t RegisterRGFImage(void)
246{
247 MagickInfo
248 *entry;
249
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);
259}
260
261/*
262%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
263% %
264% %
265% %
266% U n r e g i s t e r R G F I m a g e %
267% %
268% %
269% %
270%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
271%
272% UnregisterRGFImage() removes format registrations made by the
273% RGF module from the list of supported formats.
274%
275% The format of the UnregisterRGFImage method is:
276%
277% UnregisterRGFImage(void)
278%
279*/
280ModuleExport void UnregisterRGFImage(void)
281{
282 (void) UnregisterMagickInfo("RGF");
283}
284
285/*
286%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
287% %
288% %
289% %
290% W r i t e R G F I m a g e %
291% %
292% %
293% %
294%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
295%
296% WriteRGFImage() writes an image to a file in the X bitmap format.
297%
298% The format of the WriteRGFImage method is:
299%
300% MagickBooleanType WriteRGFImage(const ImageInfo *image_info,
301% Image *image,ExceptionInfo *exception)
302%
303% A description of each parameter follows.
304%
305% o image_info: the image info.
306%
307% o image: The image.
308%
309% o exception: return any errors or warnings in this structure.
310%
311*/
312static MagickBooleanType WriteRGFImage(const ImageInfo *image_info,Image *image,
313 ExceptionInfo *exception)
314{
315 MagickBooleanType
316 status;
317
318 register const Quantum
319 *p;
320
321 register ssize_t
322 x;
323
324 size_t
325 bit,
326 byte;
327
328 ssize_t
cristy47c10da2013-08-26 23:06:51 +0000329 y;
330
331 /*
332 Open output image file.
333 */
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)
344 return(status);
345 if (IssRGBCompatibleColorspace(image->colorspace) == MagickFalse)
346 (void) TransformImageColorspace(image,sRGBColorspace,exception);
347 if((image->columns > 255L) || (image->rows > 255L))
348 ThrowWriterException(ImageError,"Dimensions must be less than 255x255");
349
350 /*
351 Write header (just the image dimensions)
352 */
cristyd48a4a92013-09-05 15:54:43 +0000353 (void) WriteBlobByte(image,image->columns & 0xff);
354 (void) WriteBlobByte(image,image->rows & 0xff);
cristy47c10da2013-08-26 23:06:51 +0000355
356 /*
357 Convert MIFF to bit pixels.
358 */
359 (void) SetImageType(image,BilevelType,exception);
cristy47c10da2013-08-26 23:06:51 +0000360 x=0;
361 y=0;
362 for (y=0; y < (ssize_t) image->rows; y++)
363 {
364 p=GetVirtualPixels(image,0,y,image->columns,1,exception);
365 if (p == (const Quantum *) NULL)
366 break;
cristy70370492013-09-06 14:52:28 +0000367 bit=0;
368 byte=0;
cristy47c10da2013-08-26 23:06:51 +0000369 for (x=0; x < (ssize_t) image->columns; x++)
370 {
371 byte>>=1;
372 if (GetPixelLuma(image,p) < (QuantumRange/2))
373 byte|=0x80;
374 bit++;
375 if (bit == 8)
376 {
377 /*
378 Write a bitmap byte to the image file.
379 */
cristy70370492013-09-06 14:52:28 +0000380 (void) WriteBlobByte(image,byte);
cristy47c10da2013-08-26 23:06:51 +0000381 bit=0;
382 byte=0;
383 }
cristy70370492013-09-06 14:52:28 +0000384 p+=GetPixelChannels(image);
385 }
386 if (bit != 0)
387 (void) WriteBlobByte(image,byte);
cristy47c10da2013-08-26 23:06:51 +0000388 status=SetImageProgress(image,SaveImageTag,(MagickOffsetType) y,
389 image->rows);
390 if (status == MagickFalse)
391 break;
392 }
393 (void) CloseBlob(image);
394 return(MagickTrue);
395}