]> 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-2013 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/blob.h"
44 #include "MagickCore/blob-private.h"
45 #include "MagickCore/cache.h"
46 #include "MagickCore/colorspace.h"
47 #include "MagickCore/colorspace-private.h"
48 #include "MagickCore/exception.h"
49 #include "MagickCore/exception-private.h"
50 #include "MagickCore/image.h"
51 #include "MagickCore/image-private.h"
52 #include "MagickCore/list.h"
53 #include "MagickCore/magick.h"
54 #include "MagickCore/memory_.h"
55 #include "MagickCore/monitor.h"
56 #include "MagickCore/monitor-private.h"
57 #include "MagickCore/pixel-accessor.h"
58 #include "MagickCore/quantum-private.h"
59 #include "MagickCore/static.h"
60 #include "MagickCore/string_.h"
61 #include "MagickCore/module.h"
62 \f
63 /*
64   Forward declarations.
65 */
66 static MagickBooleanType
67   WriteHRZImage(const ImageInfo *,Image *,ExceptionInfo *);
68 \f
69 /*
70 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
71 %                                                                             %
72 %                                                                             %
73 %                                                                             %
74 %   R e a d H R Z I m a g e                                                   %
75 %                                                                             %
76 %                                                                             %
77 %                                                                             %
78 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
79 %
80 %  ReadHRZImage() reads a Slow Scan TeleVision image file and returns it.  It
81 %  allocates the memory necessary for the new Image structure and returns a
82 %  pointer to the new image.
83 %
84 %  The format of the ReadHRZImage method is:
85 %
86 %      Image *ReadHRZImage(const ImageInfo *image_info,ExceptionInfo *exception)
87 %
88 %  A description of each parameter follows:
89 %
90 %    o image_info: the image info.
91 %
92 %    o exception: return any errors or warnings in this structure.
93 %
94 */
95 static Image *ReadHRZImage(const ImageInfo *image_info,ExceptionInfo *exception)
96 {
97   Image
98     *image;
99
100   MagickBooleanType
101     status;
102
103   register ssize_t
104     x;
105
106   register Quantum
107     *q;
108
109   register unsigned char
110     *p;
111
112   ssize_t
113     count,
114     y;
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,exception);
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,3*
146     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 == (Quantum *) NULL)
158       break;
159     for (x=0; x < (ssize_t) image->columns; x++)
160     {
161       SetPixelRed(image,ScaleCharToQuantum(4**p++),q);
162       SetPixelGreen(image,ScaleCharToQuantum(4**p++),q);
163       SetPixelBlue(image,ScaleCharToQuantum(4**p++),q);
164       SetPixelAlpha(image,OpaqueAlpha,q);
165       q+=GetPixelChannels(image);
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,
257 %        Image *image,ExceptionInfo *exception)
258 %
259 %  A description of each parameter follows.
260 %
261 %    o image_info: the image info.
262 %
263 %    o image:  The image.
264 %
265 %    o exception: return any errors or warnings in this structure.
266 %
267 */
268 static MagickBooleanType WriteHRZImage(const ImageInfo *image_info,Image *image,
269   ExceptionInfo *exception)
270 {
271   Image
272     *hrz_image;
273
274   MagickBooleanType
275     status;
276
277   register const Quantum
278     *p;
279
280   register ssize_t
281     x,
282     y;
283
284   register unsigned char
285     *q;
286
287   ssize_t
288     count;
289
290   unsigned char
291     *pixels;
292
293   /*
294     Open output image file.
295   */
296   assert(image_info != (const ImageInfo *) NULL);
297   assert(image_info->signature == MagickSignature);
298   assert(image != (Image *) NULL);
299   assert(image->signature == MagickSignature);
300   if (image->debug != MagickFalse)
301     (void) LogMagickEvent(TraceEvent,GetMagickModule(),"%s",image->filename);
302   assert(exception != (ExceptionInfo *) NULL);
303   assert(exception->signature == MagickSignature);
304   status=OpenBlob(image_info,image,WriteBinaryBlobMode,exception);
305   if (status == MagickFalse)
306     return(status);
307   hrz_image=ResizeImage(image,256,240,image->filter,exception);
308   if (hrz_image == (Image *) NULL)
309     return(MagickFalse);
310   if (IssRGBCompatibleColorspace(hrz_image->colorspace) == MagickFalse)
311     (void) TransformImageColorspace(hrz_image,sRGBColorspace,exception);
312   /*
313     Allocate memory for pixels.
314   */
315   pixels=(unsigned char *) AcquireQuantumMemory((size_t) hrz_image->columns,
316     3*sizeof(*pixels));
317   if (pixels == (unsigned char *) NULL)
318     {
319       hrz_image=DestroyImage(hrz_image);
320       ThrowWriterException(ResourceLimitError,"MemoryAllocationFailed");
321     }
322   /*
323     Convert MIFF to HRZ raster pixels.
324   */
325   for (y=0; y < (ssize_t) hrz_image->rows; y++)
326   {
327     p=GetVirtualPixels(hrz_image,0,y,hrz_image->columns,1,exception);
328     if (p == (const Quantum *) NULL)
329       break;
330     q=pixels;
331     for (x=0; x < (ssize_t) hrz_image->columns; x++)
332     {
333       *q++=ScaleQuantumToChar(GetPixelRed(hrz_image,p)/4);
334       *q++=ScaleQuantumToChar(GetPixelGreen(hrz_image,p)/4);
335       *q++=ScaleQuantumToChar(GetPixelBlue(hrz_image,p)/4);
336       p+=GetPixelChannels(hrz_image);
337     }
338     count=WriteBlob(image,(size_t) (q-pixels),pixels);
339     if (count != (ssize_t) (q-pixels))
340       break;
341     status=SetImageProgress(image,SaveImageTag,y,hrz_image->rows);
342     if (status == MagickFalse)
343       break;
344   }
345   pixels=(unsigned char *) RelinquishMagickMemory(pixels);
346   hrz_image=DestroyImage(hrz_image);
347   (void) CloseBlob(image);
348   return(MagickTrue);
349 }