blob: 4a9f21c7bc5f7532732d3d68d1e61c472db7574c [file] [log] [blame]
cristy3ed852e2009-09-05 21:47:34 +00001/*
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% %
cristy16af1cb2009-12-11 21:38:29 +000020% Copyright 1999-2010 ImageMagick Studio LLC, a non-profit organization %
cristy3ed852e2009-09-05 21:47:34 +000021% 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
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
61/*
62 Forward declarations.
63*/
64static MagickBooleanType
65 WriteHRZImage(const ImageInfo *,Image *);
66
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*/
93static Image *ReadHRZImage(const ImageInfo *image_info,ExceptionInfo *exception)
94{
95 Image
96 *image;
97
cristybb503372010-05-27 20:51:26 +000098 ssize_t
cristy3ed852e2009-09-05 21:47:34 +000099 y;
100
101 MagickBooleanType
102 status;
103
cristybb503372010-05-27 20:51:26 +0000104 register ssize_t
cristy3ed852e2009-09-05 21:47:34 +0000105 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);
cristybb503372010-05-27 20:51:26 +0000150 for (y=0; y < (ssize_t) image->rows; y++)
cristy3ed852e2009-09-05 21:47:34 +0000151 {
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;
cristybb503372010-05-27 20:51:26 +0000159 for (x=0; x < (ssize_t) image->columns; x++)
cristy3ed852e2009-09-05 21:47:34 +0000160 {
161 q->red=4*ScaleCharToQuantum(*p++);
162 q->green=4*ScaleCharToQuantum(*p++);
163 q->blue=4*ScaleCharToQuantum(*p++);
cristyce70c172010-01-07 17:15:30 +0000164 SetOpacityPixelComponent(q,OpaqueOpacity);
cristy3ed852e2009-09-05 21:47:34 +0000165 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
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%
cristybb503372010-05-27 20:51:26 +0000199% size_t RegisterHRZImage(void)
cristy3ed852e2009-09-05 21:47:34 +0000200%
201*/
cristybb503372010-05-27 20:51:26 +0000202ModuleExport size_t RegisterHRZImage(void)
cristy3ed852e2009-09-05 21:47:34 +0000203{
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
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*/
236ModuleExport void UnregisterHRZImage(void)
237{
238 (void) UnregisterMagickInfo("HRZ");
239}
240
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*/
265static 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
cristybb503372010-05-27 20:51:26 +0000276 register ssize_t
cristy3ed852e2009-09-05 21:47:34 +0000277 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 */
cristybb503372010-05-27 20:51:26 +0000320 for (y=0; y < (ssize_t) hrz_image->rows; y++)
cristy3ed852e2009-09-05 21:47:34 +0000321 {
322 p=GetVirtualPixels(hrz_image,0,y,hrz_image->columns,1,&image->exception);
323 if (p == (PixelPacket *) NULL)
324 break;
325 q=pixels;
cristybb503372010-05-27 20:51:26 +0000326 for (x=0; x < (ssize_t) hrz_image->columns; x++)
cristy3ed852e2009-09-05 21:47:34 +0000327 {
cristyce70c172010-01-07 17:15:30 +0000328 *q++=ScaleQuantumToChar(GetRedPixelComponent(p))/4;
329 *q++=ScaleQuantumToChar(GetGreenPixelComponent(p))/4;
330 *q++=ScaleQuantumToChar(GetBluePixelComponent(p))/4;
cristy3ed852e2009-09-05 21:47:34 +0000331 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}