]> granicus.if.org Git - imagemagick/blob - coders/rgf.c
(no commit message)
[imagemagick] / coders / rgf.c
1 /*
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 %                                                                             %
20 %  Copyright 1999-2014 ImageMagick Studio LLC, a non-profit organization      %
21 %  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 \f
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 \f
67 /*
68   Forward declarations.
69 */
70 static MagickBooleanType
71   WriteRGFImage(const ImageInfo *,Image *,ExceptionInfo *);
72 \f
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 */
99 static Image *ReadRGFImage(const ImageInfo *image_info,ExceptionInfo *exception)
100 {
101   Image
102     *image;
103
104   int
105     bit;
106
107   MagickBooleanType
108     status;
109
110   register ssize_t
111     i,
112     x;
113
114   register Quantum
115     *q;
116
117   register unsigned char
118     *p;
119
120   ssize_t
121     y;
122
123   unsigned char
124     byte,
125     *data;
126
127   /*
128     Open image file.
129   */
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)
140     {
141       image=DestroyImageList(image);
142       return((Image *) NULL);
143     }
144   /*
145     Read RGF header.
146   */
147   image->columns = (unsigned long) ReadBlobByte(image);
148   image->rows = (unsigned long) ReadBlobByte(image);
149   image->depth=8;
150   image->storage_class=PseudoClass;
151   image->colors=2;
152   /*
153     Initialize image structure.
154   */
155   if (AcquireImageColormap(image,image->colors,exception) == MagickFalse)
156     ThrowReaderException(ResourceLimitError,"MemoryAllocationFailed");
157   /*
158     Initialize colormap.
159   */
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)
167     {
168       (void) CloseBlob(image);
169       return(GetFirstImageInList(image));
170     }
171
172
173   /*
174     Read hex image data.
175   */
176   data=(unsigned char *) AcquireQuantumMemory(image->rows,image->columns*
177     sizeof(*data));
178   if (data == (unsigned char *) NULL)
179     ThrowReaderException(ResourceLimitError,"MemoryAllocationFailed");
180   p=data;
181   for (i=0; i < (ssize_t) (image->columns * image->rows); i++) 
182     {
183       *p++=ReadBlobByte(image);
184     }
185
186   /*
187     Convert RGF image to pixel packets.
188   */
189   p=data;
190   for (y=0; y < (ssize_t) image->rows; y++)
191   {
192     q=QueueAuthenticPixels(image,0,y,image->columns,1,exception);
193     if (q == (Quantum *) NULL)
194       break;
195     bit=0;
196     byte=0;
197     for (x=0; x < (ssize_t) image->columns; x++)
198     {
199       if (bit == 0)
200         byte=(size_t) (*p++);
201       SetPixelIndex(image,(Quantum) ((byte & 0x01) != 0 ? 0x01 : 0x00),q);
202       bit++;
203       byte>>=1;
204       if (bit == 8)
205         bit=0;
206       q+=GetPixelChannels(image);
207     }
208     if (SyncAuthenticPixels(image,exception) == MagickFalse)
209       break;
210     status=SetImageProgress(image,LoadImageTag,(MagickOffsetType) y,
211       image->rows);
212     if (status == MagickFalse)
213       break;
214   }
215   data=(unsigned char *) RelinquishMagickMemory(data);
216   (void) SyncImage(image,exception);
217   (void) CloseBlob(image);
218   return(GetFirstImageInList(image));
219 }
220 \f
221 /*
222 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
223 %                                                                             %
224 %                                                                             %
225 %                                                                             %
226 %   R e g i s t e r R G F I m a g e                                           %
227 %                                                                             %
228 %                                                                             %
229 %                                                                             %
230 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
231 %
232 %  RegisterRGFImage() adds attributes for the RGF image format to
233 %  the list of supported formats.  The attributes include the image format
234 %  tag, a method to read and/or write the format, whether the format
235 %  supports the saving of more than one frame to the same file or blob,
236 %  whether the format supports native in-memory I/O, and a brief
237 %  description of the format.
238 %
239 %  The format of the RegisterRGFImage method is:
240 %
241 %      size_t RegisterRGFImage(void)
242 %
243 */
244 ModuleExport size_t RegisterRGFImage(void)
245 {
246   MagickInfo
247     *entry;
248
249   entry=SetMagickInfo("RGF");
250   entry->decoder=(DecodeImageHandler *) ReadRGFImage;
251   entry->encoder=(EncodeImageHandler *) WriteRGFImage;
252   entry->adjoin=MagickFalse;
253   entry->description=ConstantString(
254     "LEGO Mindstorms EV3 Robot Graphic Format (black and white)");
255   entry->module=ConstantString("RGF");
256   (void) RegisterMagickInfo(entry);
257   return(MagickImageCoderSignature);
258 }
259 \f
260 /*
261 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
262 %                                                                             %
263 %                                                                             %
264 %                                                                             %
265 %   U n r e g i s t e r R G F I m a g e                                       %
266 %                                                                             %
267 %                                                                             %
268 %                                                                             %
269 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
270 %
271 %  UnregisterRGFImage() removes format registrations made by the
272 %  RGF module from the list of supported formats.
273 %
274 %  The format of the UnregisterRGFImage method is:
275 %
276 %      UnregisterRGFImage(void)
277 %
278 */
279 ModuleExport void UnregisterRGFImage(void)
280 {
281   (void) UnregisterMagickInfo("RGF");
282 }
283 \f
284 /*
285 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
286 %                                                                             %
287 %                                                                             %
288 %                                                                             %
289 %   W r i t e R G F I m a g e                                                 %
290 %                                                                             %
291 %                                                                             %
292 %                                                                             %
293 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
294 %
295 %  WriteRGFImage() writes an image to a file in the X bitmap format.
296 %
297 %  The format of the WriteRGFImage method is:
298 %
299 %      MagickBooleanType WriteRGFImage(const ImageInfo *image_info,
300 %        Image *image,ExceptionInfo *exception)
301 %
302 %  A description of each parameter follows.
303 %
304 %    o image_info: the image info.
305 %
306 %    o image:  The image.
307 %
308 %    o exception: return any errors or warnings in this structure.
309 %
310 */
311 static MagickBooleanType WriteRGFImage(const ImageInfo *image_info,Image *image,
312   ExceptionInfo *exception)
313 {
314   MagickBooleanType
315     status;
316
317   register const Quantum
318     *p;
319
320   register ssize_t
321     x;
322
323   size_t
324     bit,
325     byte;
326
327   ssize_t
328     y;
329
330   /*
331     Open output image file.
332   */
333   assert(image_info != (const ImageInfo *) NULL);
334   assert(image_info->signature == MagickSignature);
335   assert(image != (Image *) NULL);
336   assert(image->signature == MagickSignature);
337   if (image->debug != MagickFalse)
338     (void) LogMagickEvent(TraceEvent,GetMagickModule(),"%s",image->filename);
339   assert(exception != (ExceptionInfo *) NULL);
340   assert(exception->signature == MagickSignature);
341   status=OpenBlob(image_info,image,WriteBinaryBlobMode,exception);
342   if (status == MagickFalse)
343   return(status);
344     (void) TransformImageColorspace(image,sRGBColorspace,exception);
345   if((image->columns > 255L) || (image->rows > 255L))
346     ThrowWriterException(ImageError,"Dimensions must be less than 255x255");
347   /*
348     Write header (just the image dimensions)
349    */
350   (void) WriteBlobByte(image,image->columns & 0xff);
351   (void) WriteBlobByte(image,image->rows & 0xff);
352   /*
353     Convert MIFF to bit pixels.
354   */
355   (void) SetImageType(image,BilevelType,exception);
356   x=0;
357   y=0;
358   for (y=0; y < (ssize_t) image->rows; y++)
359   {
360     p=GetVirtualPixels(image,0,y,image->columns,1,exception);
361     if (p == (const Quantum *) NULL)
362       break;
363     bit=0;
364     byte=0;
365     for (x=0; x < (ssize_t) image->columns; x++)
366     {
367       byte>>=1;
368       if (GetPixelLuma(image,p) < (QuantumRange/2))
369         byte|=0x80;
370       bit++;
371       if (bit == 8)
372         {
373           /*
374             Write a bitmap byte to the image file.
375           */
376           (void) WriteBlobByte(image,byte);
377           bit=0;
378           byte=0;
379         }
380       p+=GetPixelChannels(image);
381     }
382     if (bit != 0)
383       (void) WriteBlobByte(image,byte);
384     status=SetImageProgress(image,SaveImageTag,(MagickOffsetType) y,
385       image->rows);
386     if (status == MagickFalse)
387       break;
388   }
389   (void) CloseBlob(image);
390   return(MagickTrue);
391 }