]> granicus.if.org Git - imagemagick/blob - coders/hrz.c
(no commit message)
[imagemagick] / coders / hrz.c
1 /*
2 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
3 %                                                                             %
4 %                                                                             %
5 %                                                                             %
6 %                            H   H  RRRR   ZZZZZ                              %
7 %                            H   H  R   R     ZZ                              %
8 %                            HHHHH  RRRR     Z                                %
9 %                            H   H  R R    ZZ                                 %
10 %                            H   H  R  R   ZZZZZ                              %
11 %                                                                             %
12 %                                                                             %
13 %                Read/Write Slow Scan TeleVision 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/cache.h"
46 #include "magick/colorspace.h"
47 #include "magick/exception.h"
48 #include "magick/exception-private.h"
49 #include "magick/image.h"
50 #include "magick/image-private.h"
51 #include "magick/list.h"
52 #include "magick/magick.h"
53 #include "magick/memory_.h"
54 #include "magick/monitor.h"
55 #include "magick/monitor-private.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   Forward declarations.
63 */
64 static MagickBooleanType
65   WriteHRZImage(const ImageInfo *,Image *);
66 \f
67 /*
68 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
69 %                                                                             %
70 %                                                                             %
71 %                                                                             %
72 %   R e a d H R Z I m a g e                                                   %
73 %                                                                             %
74 %                                                                             %
75 %                                                                             %
76 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
77 %
78 %  ReadHRZImage() reads a Slow Scan TeleVision image file and returns it.  It
79 %  allocates the memory necessary for the new Image structure and returns a
80 %  pointer to the new image.
81 %
82 %  The format of the ReadHRZImage method is:
83 %
84 %      Image *ReadHRZImage(const ImageInfo *image_info,ExceptionInfo *exception)
85 %
86 %  A description of each parameter follows:
87 %
88 %    o image_info: the image info.
89 %
90 %    o exception: return any errors or warnings in this structure.
91 %
92 */
93 static Image *ReadHRZImage(const ImageInfo *image_info,ExceptionInfo *exception)
94 {
95   Image
96     *image;
97
98   ssize_t
99     y;
100
101   MagickBooleanType
102     status;
103
104   register ssize_t
105     x;
106
107   register PixelPacket
108     *q;
109
110   register unsigned char
111     *p;
112
113   ssize_t
114     count;
115
116   size_t
117     length;
118
119   unsigned char
120     *pixels;
121
122   /*
123     Open image file.
124   */
125   assert(image_info != (const ImageInfo *) NULL);
126   assert(image_info->signature == MagickSignature);
127   if (image_info->debug != MagickFalse)
128     (void) LogMagickEvent(TraceEvent,GetMagickModule(),"%s",
129       image_info->filename);
130   assert(exception != (ExceptionInfo *) NULL);
131   assert(exception->signature == MagickSignature);
132   image=AcquireImage(image_info);
133   status=OpenBlob(image_info,image,ReadBinaryBlobMode,exception);
134   if (status == MagickFalse)
135     {
136       image=DestroyImageList(image);
137       return((Image *) NULL);
138     }
139   /*
140     Convert HRZ raster image to pixel packets.
141   */
142   image->columns=256;
143   image->rows=240;
144   image->depth=8;
145   pixels=(unsigned char *) AcquireQuantumMemory(image->columns,
146     3*sizeof(*pixels));
147   if (pixels == (unsigned char *) NULL) 
148     ThrowReaderException(ResourceLimitError,"MemoryAllocationFailed");
149   length=(size_t) (3*image->columns);
150   for (y=0; y < (ssize_t) image->rows; y++)
151   {
152     count=ReadBlob(image,length,pixels);
153     if ((size_t) count != length)
154       ThrowReaderException(CorruptImageError,"UnableToReadImageData");
155     p=pixels;
156     q=QueueAuthenticPixels(image,0,y,image->columns,1,exception);
157     if (q == (PixelPacket *) NULL)
158       break;
159     for (x=0; x < (ssize_t) image->columns; x++)
160     {
161       q->red=4*ScaleCharToQuantum(*p++);
162       q->green=4*ScaleCharToQuantum(*p++);
163       q->blue=4*ScaleCharToQuantum(*p++);
164       SetOpacityPixelComponent(q,OpaqueOpacity);
165       q++;
166     }
167     if (SyncAuthenticPixels(image,exception) == MagickFalse)
168       break;
169     if (SetImageProgress(image,LoadImageTag,y,image->rows) == MagickFalse)
170       break;
171   }
172   pixels=(unsigned char *) RelinquishMagickMemory(pixels);
173   if (EOFBlob(image) != MagickFalse)
174     ThrowFileException(exception,CorruptImageError,"UnexpectedEndOfFile",
175       image->filename);
176   (void) CloseBlob(image);
177   return(GetFirstImageInList(image));
178 }
179 \f
180 /*
181 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
182 %                                                                             %
183 %                                                                             %
184 %                                                                             %
185 %   R e g i s t e r H R Z I m a g e                                           %
186 %                                                                             %
187 %                                                                             %
188 %                                                                             %
189 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
190 %
191 %  RegisterHRZImage() adds attributes for the HRZ X image format to the list
192 %  of supported formats.  The attributes include the image format tag, a
193 %  method to read and/or write the format, whether the format supports the
194 %  saving of more than one frame to the same file or blob, whether the format
195 %  supports native in-memory I/O, and a brief description of the format.
196 %
197 %  The format of the RegisterHRZImage method is:
198 %
199 %      size_t RegisterHRZImage(void)
200 %
201 */
202 ModuleExport size_t RegisterHRZImage(void)
203 {
204   MagickInfo
205     *entry;
206
207   entry=SetMagickInfo("HRZ");
208   entry->decoder=(DecodeImageHandler *) ReadHRZImage;
209   entry->encoder=(EncodeImageHandler *) WriteHRZImage;
210   entry->adjoin=MagickFalse;
211   entry->description=ConstantString("Slow Scan TeleVision");
212   entry->module=ConstantString("HRZ");
213   (void) RegisterMagickInfo(entry);
214   return(MagickImageCoderSignature);
215 }
216 \f
217 /*
218 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
219 %                                                                             %
220 %                                                                             %
221 %                                                                             %
222 %   U n r e g i s t e r H R Z I m a g e                                       %
223 %                                                                             %
224 %                                                                             %
225 %                                                                             %
226 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
227 %
228 %  UnregisterHRZImage() removes format registrations made by the
229 %  HRZ module from the list of supported formats.
230 %
231 %  The format of the UnregisterHRZImage method is:
232 %
233 %      UnregisterHRZImage(void)
234 %
235 */
236 ModuleExport void UnregisterHRZImage(void)
237 {
238   (void) UnregisterMagickInfo("HRZ");
239 }
240 \f
241 /*
242 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
243 %                                                                             %
244 %                                                                             %
245 %                                                                             %
246 %   W r i t e H R Z I m a g e                                                 %
247 %                                                                             %
248 %                                                                             %
249 %                                                                             %
250 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
251 %
252 %  WriteHRZImage() writes an image to a file in HRZ X image format.
253 %
254 %  The format of the WriteHRZImage method is:
255 %
256 %      MagickBooleanType WriteHRZImage(const ImageInfo *image_info,Image *image)
257 %
258 %  A description of each parameter follows.
259 %
260 %    o image_info: the image info.
261 %
262 %    o image:  The image.
263 %
264 */
265 static MagickBooleanType WriteHRZImage(const ImageInfo *image_info,Image *image)
266 {
267   Image
268     *hrz_image;
269
270   MagickBooleanType
271     status;
272
273   register const PixelPacket
274     *p;
275
276   register ssize_t
277     x,
278     y;
279
280   register unsigned char
281     *q;
282
283   ssize_t
284     count;
285
286   unsigned char
287     *pixels;
288
289   /*
290     Open output image file.
291   */
292   assert(image_info != (const ImageInfo *) NULL);
293   assert(image_info->signature == MagickSignature);
294   assert(image != (Image *) NULL);
295   assert(image->signature == MagickSignature);
296   if (image->debug != MagickFalse)
297     (void) LogMagickEvent(TraceEvent,GetMagickModule(),"%s",image->filename);
298   status=OpenBlob(image_info,image,WriteBinaryBlobMode,&image->exception);
299   if (status == MagickFalse)
300     return(status);
301   hrz_image=ResizeImage(image,256,240,image->filter,image->blur,
302     &image->exception);
303   if (hrz_image == (Image *) NULL)
304     return(MagickFalse);
305   if (hrz_image->colorspace != RGBColorspace)
306     (void) TransformImageColorspace(hrz_image,RGBColorspace);
307   /*
308     Allocate memory for pixels.
309   */
310   pixels=(unsigned char *) AcquireQuantumMemory((size_t) hrz_image->columns,
311     3*sizeof(*pixels));
312   if (pixels == (unsigned char *) NULL)
313     {
314       hrz_image=DestroyImage(hrz_image);
315       ThrowWriterException(ResourceLimitError,"MemoryAllocationFailed");
316     }
317   /*
318     Convert MIFF to HRZ raster pixels.
319   */
320   for (y=0; y < (ssize_t) hrz_image->rows; y++)
321   {
322     p=GetVirtualPixels(hrz_image,0,y,hrz_image->columns,1,&image->exception);
323     if (p == (PixelPacket *) NULL)
324       break;
325     q=pixels;
326     for (x=0; x < (ssize_t) hrz_image->columns; x++)
327     {
328       *q++=ScaleQuantumToChar(GetRedPixelComponent(p))/4;
329       *q++=ScaleQuantumToChar(GetGreenPixelComponent(p))/4;
330       *q++=ScaleQuantumToChar(GetBluePixelComponent(p))/4;
331       p++;
332     }
333     count=WriteBlob(image,(size_t) (q-pixels),pixels);
334     if (count != (ssize_t) (q-pixels))
335       break;
336     status=SetImageProgress(image,SaveImageTag,y,hrz_image->rows);
337     if (status == MagickFalse)
338       break;
339   }
340   pixels=(unsigned char *) RelinquishMagickMemory(pixels);
341   hrz_image=DestroyImage(hrz_image);
342   (void) CloseBlob(image);
343   return(MagickTrue);
344 }