1 /*
2 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
3 %                                                                             %
4 %                                                                             %
5 %                                                                             %
6 %                            PPPP   W   W  PPPP                               %
7 %                            P   P  W   W  P   P                              %
8 %                            PPPP   W   W  PPPP                               %
9 %                            P      W W W  P                                  %
10 %                            P       W W   P                                  %
11 %                                                                             %
12 %                                                                             %
13 %                    Read Seattle Film Works 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/blob.h"
44 #include "MagickCore/blob-private.h"
45 #include "MagickCore/constitute.h"
46 #include "MagickCore/exception.h"
47 #include "MagickCore/exception-private.h"
48 #include "MagickCore/image.h"
49 #include "MagickCore/image-private.h"
50 #include "MagickCore/list.h"
51 #include "MagickCore/magick.h"
52 #include "MagickCore/memory_.h"
53 #include "MagickCore/monitor.h"
54 #include "MagickCore/monitor-private.h"
55 #include "MagickCore/resource_.h"
56 #include "MagickCore/quantum-private.h"
57 #include "MagickCore/static.h"
58 #include "MagickCore/string_.h"
59 #include "MagickCore/module.h"
60 
61 /*
62 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
63 %                                                                             %
64 %                                                                             %
65 %                                                                             %
66 %   I s P W P                                                                 %
67 %                                                                             %
68 %                                                                             %
69 %                                                                             %
70 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
71 %
72 %  IsPWP() returns MagickTrue if the image format type, identified by the
73 %  magick string, is PWP.
74 %
75 %  The format of the IsPWP method is:
76 %
77 %      MagickBooleanType IsPWP(const unsigned char *magick,const size_t length)
78 %
79 %  A description of each parameter follows:
80 %
81 %    o magick: compare image format pattern against these bytes.
82 %
83 %    o length: Specifies the length of the magick string.
84 %
85 %
86 */
IsPWP(const unsigned char * magick,const size_t length)87 static MagickBooleanType IsPWP(const unsigned char *magick,const size_t length)
88 {
89   if (length < 5)
90     return(MagickFalse);
91   if (LocaleNCompare((char *) magick,"SFW95",5) == 0)
92     return(MagickTrue);
93   return(MagickFalse);
94 }
95 
96 /*
97 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
98 %                                                                             %
99 %                                                                             %
100 %                                                                             %
101 %   R e a d P W P I m a g e                                                   %
102 %                                                                             %
103 %                                                                             %
104 %                                                                             %
105 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
106 %
107 %  ReadPWPImage() reads a Seattle Film Works multi-image file and returns
108 %  it.  It allocates the memory necessary for the new Image structure and
109 %  returns a pointer to the new image.
110 %
111 %  The format of the ReadPWPImage method is:
112 %
113 %      Image *ReadPWPImage(const ImageInfo *image_info,ExceptionInfo *exception)
114 %
115 %  A description of each parameter follows:
116 %
117 %    o image_info: the image info.
118 %
119 %    o exception: return any errors or warnings in this structure.
120 %
121 */
ReadPWPImage(const ImageInfo * image_info,ExceptionInfo * exception)122 static Image *ReadPWPImage(const ImageInfo *image_info,ExceptionInfo *exception)
123 {
124   char
125     filename[MagickPathExtent];
126 
127   FILE
128     *file;
129 
130   Image
131     *image,
132     *next_image,
133     *pwp_image;
134 
135   ImageInfo
136     *read_info;
137 
138   int
139     c,
140     unique_file;
141 
142   MagickBooleanType
143     status;
144 
145   register Image
146     *p;
147 
148   register ssize_t
149     i;
150 
151   size_t
152     filesize,
153     length;
154 
155   ssize_t
156     count;
157 
158   unsigned char
159     magick[MagickPathExtent];
160 
161   /*
162     Open image file.
163   */
164   assert(image_info != (const ImageInfo *) NULL);
165   assert(image_info->signature == MagickCoreSignature);
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 == MagickCoreSignature);
171   image=AcquireImage(image_info,exception);
172   status=OpenBlob(image_info,image,ReadBinaryBlobMode,exception);
173   if (status == MagickFalse)
174     {
175       image=DestroyImage(image);
176       return((Image *) NULL);
177     }
178   pwp_image=image;
179   memset(magick,0,sizeof(magick));
180   count=ReadBlob(pwp_image,5,magick);
181   if ((count != 5) || (LocaleNCompare((char *) magick,"SFW95",5) != 0))
182     ThrowReaderException(CorruptImageError,"ImproperImageHeader");
183   read_info=CloneImageInfo(image_info);
184   (void) SetImageInfoProgressMonitor(read_info,(MagickProgressMonitor) NULL,
185     (void *) NULL);
186   SetImageInfoBlob(read_info,(void *) NULL,0);
187   unique_file=AcquireUniqueFileResource(filename);
188   (void) FormatLocaleString(read_info->filename,MagickPathExtent,"sfw:%s",
189     filename);
190   for ( ; ; )
191   {
192     (void) memset(magick,0,sizeof(magick));
193     for (c=ReadBlobByte(pwp_image); c != EOF; c=ReadBlobByte(pwp_image))
194     {
195       for (i=0; i < 17; i++)
196         magick[i]=magick[i+1];
197       magick[17]=(unsigned char) c;
198       if (LocaleNCompare((char *) (magick+12),"SFW94A",6) == 0)
199         break;
200     }
201     if (c == EOF)
202       {
203         (void) RelinquishUniqueFileResource(filename);
204         read_info=DestroyImageInfo(read_info);
205         ThrowReaderException(CorruptImageError,"UnexpectedEndOfFile");
206       }
207     if (LocaleNCompare((char *) (magick+12),"SFW94A",6) != 0)
208       {
209         (void) RelinquishUniqueFileResource(filename);
210         read_info=DestroyImageInfo(read_info);
211         ThrowReaderException(CorruptImageError,"ImproperImageHeader");
212       }
213     /*
214       Dump SFW image to a temporary file.
215     */
216     file=(FILE *) NULL;
217     if (unique_file != -1)
218       file=fdopen(unique_file,"wb");
219     if ((unique_file == -1) || (file == (FILE *) NULL))
220       {
221         (void) RelinquishUniqueFileResource(filename);
222         read_info=DestroyImageInfo(read_info);
223         ThrowFileException(exception,FileOpenError,"UnableToWriteFile",
224           image->filename);
225         image=DestroyImageList(image);
226         return((Image *) NULL);
227       }
228     length=fwrite("SFW94A",1,6,file);
229     (void) length;
230     filesize=65535UL*magick[2]+256L*magick[1]+magick[0];
231     for (i=0; i < (ssize_t) filesize; i++)
232     {
233       c=ReadBlobByte(pwp_image);
234       if (c == EOF)
235         break;
236       if (fputc(c,file) != c)
237         break;
238     }
239     (void) fclose(file);
240     if (c == EOF)
241       {
242         (void) RelinquishUniqueFileResource(filename);
243         read_info=DestroyImageInfo(read_info);
244         ThrowReaderException(CorruptImageError,"UnexpectedEndOfFile");
245       }
246     next_image=ReadImage(read_info,exception);
247     if (next_image == (Image *) NULL)
248       break;
249     (void) FormatLocaleString(next_image->filename,MagickPathExtent,
250       "slide_%02ld.sfw",(long) next_image->scene);
251     if (image == (Image *) NULL)
252       image=next_image;
253     else
254       {
255         /*
256           Link image into image list.
257         */
258         for (p=image; p->next != (Image *) NULL; p=GetNextImageInList(p)) ;
259         next_image->previous=p;
260         next_image->scene=p->scene+1;
261         p->next=next_image;
262       }
263     if (image_info->number_scenes != 0)
264       if (next_image->scene >= (image_info->scene+image_info->number_scenes-1))
265         break;
266     status=SetImageProgress(image,LoadImagesTag,TellBlob(pwp_image),
267       GetBlobSize(pwp_image));
268     if (status == MagickFalse)
269       break;
270   }
271   if (unique_file != -1)
272     (void) close(unique_file);
273   (void) RelinquishUniqueFileResource(filename);
274   read_info=DestroyImageInfo(read_info);
275   if (image != (Image *) NULL)
276     {
277       if (EOFBlob(image) != MagickFalse)
278         {
279           char
280             *message;
281 
282           message=GetExceptionMessage(errno);
283           (void) ThrowMagickException(exception,GetMagickModule(),
284             CorruptImageError,"UnexpectedEndOfFile","`%s': %s",image->filename,
285             message);
286           message=DestroyString(message);
287         }
288       (void) CloseBlob(image);
289     }
290   return(GetFirstImageInList(image));
291 }
292 
293 /*
294 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
295 %                                                                             %
296 %                                                                             %
297 %                                                                             %
298 %   R e g i s t e r P W P I m a g e                                           %
299 %                                                                             %
300 %                                                                             %
301 %                                                                             %
302 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
303 %
304 %  RegisterPWPImage() adds attributes for the PWP image format to
305 %  the list of supported formats.  The attributes include the image format
306 %  tag, a method to read and/or write the format, whether the format
307 %  supports the saving of more than one frame to the same file or blob,
308 %  whether the format supports native in-memory I/O, and a brief
309 %  description of the format.
310 %
311 %  The format of the RegisterPWPImage method is:
312 %
313 %      size_t RegisterPWPImage(void)
314 %
315 */
RegisterPWPImage(void)316 ModuleExport size_t RegisterPWPImage(void)
317 {
318   MagickInfo
319     *entry;
320 
321   entry=AcquireMagickInfo("PWP","PWP","Seattle Film Works");
322   entry->decoder=(DecodeImageHandler *) ReadPWPImage;
323   entry->magick=(IsImageFormatHandler *) IsPWP;
324   (void) RegisterMagickInfo(entry);
325   return(MagickImageCoderSignature);
326 }
327 
328 /*
329 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
330 %                                                                             %
331 %                                                                             %
332 %                                                                             %
333 %   U n r e g i s t e r P W P I m a g e                                       %
334 %                                                                             %
335 %                                                                             %
336 %                                                                             %
337 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
338 %
339 %  UnregisterPWPImage() removes format registrations made by the
340 %  PWP module from the list of supported formats.
341 %
342 %  The format of the UnregisterPWPImage method is:
343 %
344 %      UnregisterPWPImage(void)
345 %
346 */
UnregisterPWPImage(void)347 ModuleExport void UnregisterPWPImage(void)
348 {
349   (void) UnregisterMagickInfo("PWP");
350 }
351