]> granicus.if.org Git - imagemagick/blob - coders/pwp.c
(no commit message)
[imagemagick] / coders / pwp.c
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 %                                John Cristy                                  %
17 %                                 July 1992                                   %
18 %                                                                             %
19 %                                                                             %
20 %  Copyright 1999-2011 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 "magick/studio.h"
43 #include "magick/blob.h"
44 #include "magick/blob-private.h"
45 #include "magick/constitute.h"
46 #include "magick/exception.h"
47 #include "magick/exception-private.h"
48 #include "magick/image.h"
49 #include "magick/image-private.h"
50 #include "magick/list.h"
51 #include "magick/magick.h"
52 #include "magick/memory_.h"
53 #include "magick/monitor.h"
54 #include "magick/monitor-private.h"
55 #include "magick/resource_.h"
56 #include "magick/quantum-private.h"
57 #include "magick/static.h"
58 #include "magick/string_.h"
59 #include "magick/module.h"
60 \f
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 */
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 \f
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 */
122 static Image *ReadPWPImage(const ImageInfo *image_info,ExceptionInfo *exception)
123 {
124   FILE
125     *file;
126
127   Image
128     *image,
129     *next_image,
130     *pwp_image;
131
132   ImageInfo
133     *read_info;
134
135   int
136     c,
137     unique_file;
138
139   MagickBooleanType
140     status;
141
142   register Image
143     *p;
144
145   register ssize_t
146     i;
147
148   size_t
149     length;
150
151   ssize_t
152     count;
153
154   unsigned char
155     magick[MaxTextExtent];
156
157   size_t
158     filesize;
159
160   /*
161     Open image file.
162   */
163   assert(image_info != (const ImageInfo *) NULL);
164   assert(image_info->signature == MagickSignature);
165   if (image_info->debug != MagickFalse)
166     (void) LogMagickEvent(TraceEvent,GetMagickModule(),"%s",
167       image_info->filename);
168   assert(exception != (ExceptionInfo *) NULL);
169   assert(exception->signature == MagickSignature);
170   pwp_image=AcquireImage(image_info);
171   image=pwp_image;
172   status=OpenBlob(image_info,pwp_image,ReadBinaryBlobMode,exception);
173   if (status == MagickFalse)
174     return((Image *) NULL);
175   count=ReadBlob(pwp_image,5,magick);
176   if ((count == 0) || (LocaleNCompare((char *) magick,"SFW95",5) != 0))
177     ThrowReaderException(CorruptImageError,"ImproperImageHeader");
178   read_info=CloneImageInfo(image_info);
179   (void) SetImageInfoProgressMonitor(read_info,(MagickProgressMonitor) NULL,
180     (void *) NULL);
181   SetImageInfoBlob(read_info,(void *) NULL,0);
182   unique_file=AcquireUniqueFileResource(read_info->filename);
183   for ( ; ; )
184   {
185     for (c=ReadBlobByte(pwp_image); c != EOF; c=ReadBlobByte(pwp_image))
186     {
187       for (i=0; i < 17; i++)
188         magick[i]=magick[i+1];
189       magick[17]=(unsigned char) c;
190       if (LocaleNCompare((char *) (magick+12),"SFW94A",6) == 0)
191         break;
192     }
193     if (c == EOF)
194       break;
195     if (LocaleNCompare((char *) (magick+12),"SFW94A",6) != 0)
196       ThrowReaderException(CorruptImageError,"ImproperImageHeader");
197     /*
198       Dump SFW image to a temporary file.
199     */
200     file=(FILE *) NULL;
201     if (unique_file != -1)
202       file=fdopen(unique_file,"wb");
203     if ((unique_file == -1) || (file == (FILE *) NULL))
204       {
205         ThrowFileException(exception,FileOpenError,"UnableToWriteFile",
206           image->filename);
207         image=DestroyImageList(image);
208         return((Image *) NULL);
209       }
210     length=fwrite("SFW94A",1,6,file);
211     (void) length;
212     filesize=65535UL*magick[2]+256L*magick[1]+magick[0];
213     for (i=0; i < (ssize_t) filesize; i++)
214     {
215       c=ReadBlobByte(pwp_image);
216       (void) fputc(c,file);
217     }
218     (void) fclose(file);
219     next_image=ReadImage(read_info,exception);
220     if (next_image == (Image *) NULL)
221       break;
222     (void) FormatMagickString(next_image->filename,MaxTextExtent,
223       "slide_%02ld.sfw",(long) next_image->scene);
224     if (image == (Image *) NULL)
225       image=next_image;
226     else
227       {
228         /*
229           Link image into image list.
230         */
231         for (p=image; p->next != (Image *) NULL; p=GetNextImageInList(p)) ;
232         next_image->previous=p;
233         next_image->scene=p->scene+1;
234         p->next=next_image;
235       }
236     if (image_info->number_scenes != 0)
237       if (next_image->scene >= (image_info->scene+image_info->number_scenes-1))
238         break;
239     status=SetImageProgress(image,LoadImagesTag,TellBlob(pwp_image),
240       GetBlobSize(pwp_image));
241     if (status == MagickFalse)
242       break;
243   }
244   (void) RelinquishUniqueFileResource(read_info->filename);
245   read_info=DestroyImageInfo(read_info);
246   (void) CloseBlob(pwp_image);
247   pwp_image=DestroyImage(pwp_image);
248   if (EOFBlob(image) != MagickFalse)
249     {
250       char
251         *message;
252
253       message=GetExceptionMessage(errno);
254       (void) ThrowMagickException(exception,GetMagickModule(),CorruptImageError,
255         "UnexpectedEndOfFile","`%s': %s",image->filename,message);
256       message=DestroyString(message);
257     }
258   (void) CloseBlob(image);
259   return(GetFirstImageInList(image));
260 }
261 \f
262 /*
263 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
264 %                                                                             %
265 %                                                                             %
266 %                                                                             %
267 %   R e g i s t e r P W P I m a g e                                           %
268 %                                                                             %
269 %                                                                             %
270 %                                                                             %
271 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
272 %
273 %  RegisterPWPImage() adds attributes for the PWP image format to
274 %  the list of supported formats.  The attributes include the image format
275 %  tag, a method to read and/or write the format, whether the format
276 %  supports the saving of more than one frame to the same file or blob,
277 %  whether the format supports native in-memory I/O, and a brief
278 %  description of the format.
279 %
280 %  The format of the RegisterPWPImage method is:
281 %
282 %      size_t RegisterPWPImage(void)
283 %
284 */
285 ModuleExport size_t RegisterPWPImage(void)
286 {
287   MagickInfo
288     *entry;
289
290   entry=SetMagickInfo("PWP");
291   entry->decoder=(DecodeImageHandler *) ReadPWPImage;
292   entry->magick=(IsImageFormatHandler *) IsPWP;
293   entry->description=ConstantString("Seattle Film Works");
294   entry->module=ConstantString("PWP");
295   (void) RegisterMagickInfo(entry);
296   return(MagickImageCoderSignature);
297 }
298 \f
299 /*
300 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
301 %                                                                             %
302 %                                                                             %
303 %                                                                             %
304 %   U n r e g i s t e r P W P I m a g e                                       %
305 %                                                                             %
306 %                                                                             %
307 %                                                                             %
308 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
309 %
310 %  UnregisterPWPImage() removes format registrations made by the
311 %  PWP module from the list of supported formats.
312 %
313 %  The format of the UnregisterPWPImage method is:
314 %
315 %      UnregisterPWPImage(void)
316 %
317 */
318 ModuleExport void UnregisterPWPImage(void)
319 {
320   (void) UnregisterMagickInfo("PWP");
321 }