1 /*
2 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
3 %                                                                             %
4 %                                                                             %
5 %                                                                             %
6 %                            RRRR   L       AAA                               %
7 %                            R   R  L      A   A                              %
8 %                            RRRR   L      AAAAA                              %
9 %                            R R    L      A   A                              %
10 %                            R  R   LLLLL  A   A                              %
11 %                                                                             %
12 %                                                                             %
13 %                      Read Alias/Wavefront Image Format                      %
14 %                                                                             %
15 %                              Software Design                                %
16 %                                   Cristy                                    %
17 %                                 July 1992                                   %
18 %                                                                             %
19 %                                                                             %
20 %  Copyright 1999-2019 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 %    https://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/property.h"
44 #include "MagickCore/blob.h"
45 #include "MagickCore/blob-private.h"
46 #include "MagickCore/cache.h"
47 #include "MagickCore/exception.h"
48 #include "MagickCore/exception-private.h"
49 #include "MagickCore/image.h"
50 #include "MagickCore/image-private.h"
51 #include "MagickCore/list.h"
52 #include "MagickCore/magick.h"
53 #include "MagickCore/memory_.h"
54 #include "MagickCore/monitor.h"
55 #include "MagickCore/monitor-private.h"
56 #include "MagickCore/pixel-accessor.h"
57 #include "MagickCore/quantum-private.h"
58 #include "MagickCore/static.h"
59 #include "MagickCore/string_.h"
60 #include "MagickCore/module.h"
61 
62 /*
63 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
64 %                                                                             %
65 %                                                                             %
66 %                                                                             %
67 %   R e a d R L A I m a g e                                                   %
68 %                                                                             %
69 %                                                                             %
70 %                                                                             %
71 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
72 %
73 %  ReadRLAImage() reads a run-length encoded Wavefront RLA image file
74 %  and returns it.  It allocates the memory necessary for the new Image
75 %  structure and returns a pointer to the new image.
76 %
77 %  Note:  This module was contributed by Lester Vecsey (master@internexus.net).
78 %
79 %  The format of the ReadRLAImage method is:
80 %
81 %      Image *ReadRLAImage(const ImageInfo *image_info,ExceptionInfo *exception)
82 %
83 %  A description of each parameter follows:
84 %
85 %    o image_info: the image info.
86 %
87 %    o exception: return any errors or warnings in this structure.
88 %
89 */
ReadRLAImage(const ImageInfo * image_info,ExceptionInfo * exception)90 static Image *ReadRLAImage(const ImageInfo *image_info,ExceptionInfo *exception)
91 {
92   typedef struct _WindowFrame
93   {
94     short
95       left,
96       right,
97       bottom,
98       top;
99   } WindowFrame;
100 
101   typedef struct _RLAInfo
102   {
103     WindowFrame
104       window,
105       active_window;
106 
107     short
108       frame,
109       storage_type,
110       number_channels,
111       number_matte_channels,
112       number_auxiliary_channels,
113       revision;
114 
115     char
116       gamma[16+1],
117       red_primary[24+1],
118       green_primary[24+1],
119       blue_primary[24+1],
120       white_point[24+1];
121 
122     int
123       job_number;
124 
125     char
126       name[128+1],
127       description[128+1],
128       program[64+1],
129       machine[32+1],
130       user[32+1],
131       date[20+1],
132       aspect[24+1],
133       aspect_ratio[8+1],
134       chan[32+1];
135 
136     short
137       field;
138 
139     char
140       time[12],
141       filter[32];
142 
143     short
144       bits_per_channel,
145       matte_type,
146       matte_bits,
147       auxiliary_type,
148       auxiliary_bits;
149 
150     char
151       auxiliary[32+1],
152       space[36+1];
153 
154     int
155       next;
156   } RLAInfo;
157 
158   Image
159     *image;
160 
161   int
162     channel,
163     length,
164     runlength;
165 
166   MagickBooleanType
167     status;
168 
169   MagickOffsetType
170     offset,
171     *scanlines;
172 
173   register ssize_t
174     i,
175     x;
176 
177   register Quantum
178     *q;
179 
180   ssize_t
181     count,
182     y;
183 
184   RLAInfo
185     rla_info;
186 
187   unsigned char
188     byte;
189 
190   /*
191     Open image file.
192   */
193   assert(image_info != (const ImageInfo *) NULL);
194   assert(image_info->signature == MagickCoreSignature);
195   if (image_info->debug != MagickFalse)
196     (void) LogMagickEvent(TraceEvent,GetMagickModule(),"%s",
197       image_info->filename);
198   assert(exception != (ExceptionInfo *) NULL);
199   assert(exception->signature == MagickCoreSignature);
200   image=AcquireImage(image_info,exception);
201   status=OpenBlob(image_info,image,ReadBinaryBlobMode,exception);
202   if (status == MagickFalse)
203     {
204       image=DestroyImageList(image);
205       return((Image *) NULL);
206     }
207   (void) memset(&rla_info,0,sizeof(rla_info));
208   rla_info.window.left=(short) ReadBlobMSBShort(image);
209   rla_info.window.right=(short) ReadBlobMSBShort(image);
210   rla_info.window.bottom=(short) ReadBlobMSBShort(image);
211   rla_info.window.top=(short) ReadBlobMSBShort(image);
212   rla_info.active_window.left=(short) ReadBlobMSBShort(image);
213   rla_info.active_window.right=(short) ReadBlobMSBShort(image);
214   rla_info.active_window.bottom=(short) ReadBlobMSBShort(image);
215   rla_info.active_window.top=(short) ReadBlobMSBShort(image);
216   rla_info.frame=(short) ReadBlobMSBShort(image);
217   rla_info.storage_type=(short) ReadBlobMSBShort(image);
218   rla_info.number_channels=(short) ReadBlobMSBShort(image);
219   if (rla_info.number_channels < 0)
220     ThrowReaderException(CorruptImageError,"ImproperImageHeader");
221   rla_info.number_matte_channels=(short) ReadBlobMSBShort(image);
222   if (rla_info.number_matte_channels < 0)
223     ThrowReaderException(CorruptImageError,"ImproperImageHeader");
224   if ((rla_info.number_channels > 3) || (rla_info.number_matte_channels > 3))
225     ThrowReaderException(CoderError,"Unsupported number of channels");
226   if (rla_info.number_channels == 0)
227     rla_info.number_channels=3;
228   rla_info.number_channels+=rla_info.number_matte_channels;
229   rla_info.number_auxiliary_channels=(short) ReadBlobMSBShort(image);
230   rla_info.revision=(short) ReadBlobMSBShort(image);
231   (void) ReadBlob(image,16,(unsigned char *) rla_info.gamma);
232   (void) ReadBlob(image,24,(unsigned char *) rla_info.red_primary);
233   (void) ReadBlob(image,24,(unsigned char *) rla_info.green_primary);
234   (void) ReadBlob(image,24,(unsigned char *) rla_info.blue_primary);
235   (void) ReadBlob(image,24,(unsigned char *) rla_info.white_point);
236   rla_info.job_number=ReadBlobMSBSignedLong(image);
237   (void) ReadBlob(image,128,(unsigned char *) rla_info.name);
238   (void) ReadBlob(image,128,(unsigned char *) rla_info.description);
239   rla_info.description[127]='\0';
240   (void) ReadBlob(image,64,(unsigned char *) rla_info.program);
241   (void) ReadBlob(image,32,(unsigned char *) rla_info.machine);
242   (void) ReadBlob(image,32,(unsigned char *) rla_info.user);
243   (void) ReadBlob(image,20,(unsigned char *) rla_info.date);
244   (void) ReadBlob(image,24,(unsigned char *) rla_info.aspect);
245   (void) ReadBlob(image,8,(unsigned char *) rla_info.aspect_ratio);
246   (void) ReadBlob(image,32,(unsigned char *) rla_info.chan);
247   rla_info.field=(short) ReadBlobMSBShort(image);
248   (void) ReadBlob(image,12,(unsigned char *) rla_info.time);
249   (void) ReadBlob(image,32,(unsigned char *) rla_info.filter);
250   rla_info.bits_per_channel=(short) ReadBlobMSBShort(image);
251   rla_info.matte_type=(short) ReadBlobMSBShort(image);
252   rla_info.matte_bits=(short) ReadBlobMSBShort(image);
253   rla_info.auxiliary_type=(short) ReadBlobMSBShort(image);
254   rla_info.auxiliary_bits=(short) ReadBlobMSBShort(image);
255   (void) ReadBlob(image,32,(unsigned char *) rla_info.auxiliary);
256   count=ReadBlob(image,36,(unsigned char *) rla_info.space);
257   if ((size_t) count != 36)
258     ThrowReaderException(CorruptImageError,"UnableToReadImageData");
259   rla_info.next=ReadBlobMSBSignedLong(image);
260   /*
261     Initialize image structure.
262   */
263   image->alpha_trait=rla_info.number_matte_channels != 0 ? BlendPixelTrait :
264     UndefinedPixelTrait;
265   image->columns=(size_t) (rla_info.active_window.right-
266     rla_info.active_window.left+1);
267   image->rows=(size_t) (rla_info.active_window.top-
268     rla_info.active_window.bottom+1);
269   if (image_info->ping != MagickFalse)
270     {
271       (void) CloseBlob(image);
272       return(GetFirstImageInList(image));
273     }
274   status=SetImageExtent(image,image->columns,image->rows,exception);
275   if (status == MagickFalse)
276     return(DestroyImageList(image));
277   scanlines=(MagickOffsetType *) AcquireQuantumMemory(image->rows,
278     sizeof(*scanlines));
279   if (scanlines == (MagickOffsetType *) NULL)
280     ThrowReaderException(ResourceLimitError,"MemoryAllocationFailed");
281   if (*rla_info.description != '\0')
282     (void) SetImageProperty(image,"comment",rla_info.description,exception);
283   /*
284     Read offsets to each scanline data.
285   */
286   for (i=0; i < (ssize_t) image->rows; i++)
287     scanlines[i]=(MagickOffsetType) ReadBlobMSBSignedLong(image);
288   if (EOFBlob(image) != MagickFalse)
289     {
290       scanlines=(MagickOffsetType *) RelinquishMagickMemory(scanlines);
291       ThrowReaderException(CorruptImageError,"ImproperImageHeader");
292     }
293   /*
294     Read image data.
295   */
296   x=0;
297   for (y=0; y < (ssize_t) image->rows; y++)
298   {
299     offset=SeekBlob(image,scanlines[image->rows-y-1],SEEK_SET);
300     if (offset < 0)
301       {
302         scanlines=(MagickOffsetType *) RelinquishMagickMemory(scanlines);
303         ThrowReaderException(CorruptImageError,"ImproperImageHeader");
304       }
305     for (channel=0; channel < (int) rla_info.number_channels; channel++)
306     {
307       length=ReadBlobMSBSignedShort(image);
308       while (length > 0)
309       {
310         byte=(unsigned char) ReadBlobByte(image);
311         runlength=byte;
312         if (byte > 127)
313           runlength=byte-256;
314         length--;
315         if (length == 0)
316           break;
317         if (runlength < 0)
318           {
319             while (runlength < 0)
320             {
321               q=GetAuthenticPixels(image,(ssize_t) (x % image->columns),
322                 (ssize_t) (y % image->rows),1,1,exception);
323               if (q == (Quantum *) NULL)
324                 break;
325               byte=(unsigned char) ReadBlobByte(image);
326               length--;
327               switch (channel)
328               {
329                 case 0:
330                 {
331                   SetPixelRed(image,ScaleCharToQuantum(byte),q);
332                   break;
333                 }
334                 case 1:
335                 {
336                   SetPixelGreen(image,ScaleCharToQuantum(byte),q);
337                   break;
338                 }
339                 case 2:
340                 {
341                   SetPixelBlue(image,ScaleCharToQuantum(byte),q);
342                   break;
343                 }
344                 case 3:
345                 default:
346                 {
347                   SetPixelAlpha(image,ScaleCharToQuantum(byte),q);
348                   break;
349                 }
350               }
351               if (SyncAuthenticPixels(image,exception) == MagickFalse)
352                 break;
353               x++;
354               runlength++;
355             }
356             continue;
357           }
358         byte=(unsigned char) ReadBlobByte(image);
359         length--;
360         runlength++;
361         do
362         {
363           q=GetAuthenticPixels(image,(ssize_t) (x % image->columns),
364             (ssize_t) (y % image->rows),1,1,exception);
365           if (q == (Quantum *) NULL)
366             break;
367           switch (channel)
368           {
369             case 0:
370             {
371               SetPixelRed(image,ScaleCharToQuantum(byte),q);
372               break;
373             }
374             case 1:
375             {
376               SetPixelGreen(image,ScaleCharToQuantum(byte),q);
377               break;
378             }
379             case 2:
380             {
381               SetPixelBlue(image,ScaleCharToQuantum(byte),q);
382               break;
383             }
384             case 3:
385             default:
386             {
387               SetPixelAlpha(image,ScaleCharToQuantum(byte),q);
388               break;
389             }
390           }
391           if (SyncAuthenticPixels(image,exception) == MagickFalse)
392             break;
393           x++;
394           runlength--;
395         }
396         while (runlength > 0);
397       }
398     }
399     if (EOFBlob(image) != MagickFalse)
400       break;
401     status=SetImageProgress(image,LoadImageTag,(MagickOffsetType) y,
402       image->rows);
403     if (status == MagickFalse)
404       break;
405   }
406   scanlines=(MagickOffsetType *) RelinquishMagickMemory(scanlines);
407   if (EOFBlob(image) != MagickFalse)
408     ThrowFileException(exception,CorruptImageError,"UnexpectedEndOfFile",
409       image->filename);
410   (void) CloseBlob(image);
411   return(GetFirstImageInList(image));
412 }
413 
414 /*
415 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
416 %                                                                             %
417 %                                                                             %
418 %                                                                             %
419 %   R e g i s t e r R L A I m a g e                                           %
420 %                                                                             %
421 %                                                                             %
422 %                                                                             %
423 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
424 %
425 %  RegisterRLAImage() adds attributes for the RLA image format to
426 %  the list of supported formats.  The attributes include the image format
427 %  tag, a method to read and/or write the format, whether the format
428 %  supports the saving of more than one frame to the same file or blob,
429 %  whether the format supports native in-memory I/O, and a brief
430 %  description of the format.
431 %
432 %  The format of the RegisterRLAImage method is:
433 %
434 %      size_t RegisterRLAImage(void)
435 %
436 */
RegisterRLAImage(void)437 ModuleExport size_t RegisterRLAImage(void)
438 {
439   MagickInfo
440     *entry;
441 
442   entry=AcquireMagickInfo("RLA","RLA","Alias/Wavefront image");
443   entry->decoder=(DecodeImageHandler *) ReadRLAImage;
444   entry->flags^=CoderAdjoinFlag;
445   entry->flags|=CoderDecoderSeekableStreamFlag;
446   (void) RegisterMagickInfo(entry);
447   return(MagickImageCoderSignature);
448 }
449 
450 /*
451 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
452 %                                                                             %
453 %                                                                             %
454 %                                                                             %
455 %   U n r e g i s t e r R L A I m a g e                                       %
456 %                                                                             %
457 %                                                                             %
458 %                                                                             %
459 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
460 %
461 %  UnregisterRLAImage() removes format registrations made by the
462 %  RLA module from the list of supported formats.
463 %
464 %  The format of the UnregisterRLAImage method is:
465 %
466 %      UnregisterRLAImage(void)
467 %
468 */
UnregisterRLAImage(void)469 ModuleExport void UnregisterRLAImage(void)
470 {
471   (void) UnregisterMagickInfo("RLA");
472 }
473