2 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
13 % Read/Write Slow Scan TeleVision Image Format %
20 % Copyright 1999-2019 ImageMagick Studio LLC, a non-profit organization %
21 % dedicated to making software imaging solutions freely available. %
23 % You may not use this file except in compliance with the License. You may %
24 % obtain a copy of the License at %
26 % https://imagemagick.org/script/license.php %
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. %
34 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
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"
66 static MagickBooleanType
67 WriteHRZImage(const ImageInfo *,Image *,ExceptionInfo *);
70 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
74 % R e a d H R Z I m a g e %
78 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
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.
84 % The format of the ReadHRZImage method is:
86 % Image *ReadHRZImage(const ImageInfo *image_info,ExceptionInfo *exception)
88 % A description of each parameter follows:
90 % o image_info: the image info.
92 % o exception: return any errors or warnings in this structure.
95 static Image *ReadHRZImage(const ImageInfo *image_info,ExceptionInfo *exception)
109 register unsigned char
125 assert(image_info != (const ImageInfo *) NULL);
126 assert(image_info->signature == MagickCoreSignature);
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 == MagickCoreSignature);
132 image=AcquireImage(image_info,exception);
133 status=OpenBlob(image_info,image,ReadBinaryBlobMode,exception);
134 if (status == MagickFalse)
136 image=DestroyImageList(image);
137 return((Image *) NULL);
140 Convert HRZ raster image to pixel packets.
145 status=SetImageExtent(image,image->columns,image->rows,exception);
146 if (status == MagickFalse)
147 return(DestroyImageList(image));
148 pixels=(unsigned char *) AcquireQuantumMemory(image->columns,3*
150 if (pixels == (unsigned char *) NULL)
151 ThrowReaderException(ResourceLimitError,"MemoryAllocationFailed");
152 length=(size_t) (3*image->columns);
153 for (y=0; y < (ssize_t) image->rows; y++)
155 count=ReadBlob(image,length,pixels);
156 if ((size_t) count != length)
158 pixels=(unsigned char *) RelinquishMagickMemory(pixels);
159 ThrowReaderException(CorruptImageError,"UnableToReadImageData");
162 q=QueueAuthenticPixels(image,0,y,image->columns,1,exception);
163 if (q == (Quantum *) NULL)
165 for (x=0; x < (ssize_t) image->columns; x++)
167 SetPixelRed(image,ScaleCharToQuantum(4**p++),q);
168 SetPixelGreen(image,ScaleCharToQuantum(4**p++),q);
169 SetPixelBlue(image,ScaleCharToQuantum(4**p++),q);
170 SetPixelAlpha(image,OpaqueAlpha,q);
171 q+=GetPixelChannels(image);
173 if (SyncAuthenticPixels(image,exception) == MagickFalse)
175 if (SetImageProgress(image,LoadImageTag,y,image->rows) == MagickFalse)
178 pixels=(unsigned char *) RelinquishMagickMemory(pixels);
179 if (EOFBlob(image) != MagickFalse)
180 ThrowFileException(exception,CorruptImageError,"UnexpectedEndOfFile",
182 (void) CloseBlob(image);
183 return(GetFirstImageInList(image));
187 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
191 % R e g i s t e r H R Z I m a g e %
195 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
197 % RegisterHRZImage() adds attributes for the HRZ X image format to the list
198 % of supported formats. The attributes include the image format tag, a
199 % method to read and/or write the format, whether the format supports the
200 % saving of more than one frame to the same file or blob, whether the format
201 % supports native in-memory I/O, and a brief description of the format.
203 % The format of the RegisterHRZImage method is:
205 % size_t RegisterHRZImage(void)
208 ModuleExport size_t RegisterHRZImage(void)
213 entry=AcquireMagickInfo("HRZ","HRZ","Slow Scan TeleVision");
214 entry->decoder=(DecodeImageHandler *) ReadHRZImage;
215 entry->encoder=(EncodeImageHandler *) WriteHRZImage;
216 entry->flags^=CoderAdjoinFlag;
217 (void) RegisterMagickInfo(entry);
218 return(MagickImageCoderSignature);
222 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
226 % U n r e g i s t e r H R Z I m a g e %
230 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
232 % UnregisterHRZImage() removes format registrations made by the
233 % HRZ module from the list of supported formats.
235 % The format of the UnregisterHRZImage method is:
237 % UnregisterHRZImage(void)
240 ModuleExport void UnregisterHRZImage(void)
242 (void) UnregisterMagickInfo("HRZ");
246 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
250 % W r i t e H R Z I m a g e %
254 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
256 % WriteHRZImage() writes an image to a file in HRZ X image format.
258 % The format of the WriteHRZImage method is:
260 % MagickBooleanType WriteHRZImage(const ImageInfo *image_info,
261 % Image *image,ExceptionInfo *exception)
263 % A description of each parameter follows.
265 % o image_info: the image info.
267 % o image: The image.
269 % o exception: return any errors or warnings in this structure.
272 static MagickBooleanType WriteHRZImage(const ImageInfo *image_info,Image *image,
273 ExceptionInfo *exception)
281 register const Quantum
288 register unsigned char
298 Open output image file.
300 assert(image_info != (const ImageInfo *) NULL);
301 assert(image_info->signature == MagickCoreSignature);
302 assert(image != (Image *) NULL);
303 assert(image->signature == MagickCoreSignature);
304 if (image->debug != MagickFalse)
305 (void) LogMagickEvent(TraceEvent,GetMagickModule(),"%s",image->filename);
306 assert(exception != (ExceptionInfo *) NULL);
307 assert(exception->signature == MagickCoreSignature);
308 status=OpenBlob(image_info,image,WriteBinaryBlobMode,exception);
309 if (status == MagickFalse)
311 hrz_image=ResizeImage(image,256,240,image->filter,exception);
312 if (hrz_image == (Image *) NULL)
314 (void) TransformImageColorspace(hrz_image,sRGBColorspace,exception);
316 Allocate memory for pixels.
318 pixels=(unsigned char *) AcquireQuantumMemory((size_t) hrz_image->columns,
320 if (pixels == (unsigned char *) NULL)
322 hrz_image=DestroyImage(hrz_image);
323 ThrowWriterException(ResourceLimitError,"MemoryAllocationFailed");
326 Convert MIFF to HRZ raster pixels.
328 for (y=0; y < (ssize_t) hrz_image->rows; y++)
330 p=GetVirtualPixels(hrz_image,0,y,hrz_image->columns,1,exception);
331 if (p == (const Quantum *) NULL)
334 for (x=0; x < (ssize_t) hrz_image->columns; x++)
336 *q++=ScaleQuantumToChar(GetPixelRed(hrz_image,p)/4);
337 *q++=ScaleQuantumToChar(GetPixelGreen(hrz_image,p)/4);
338 *q++=ScaleQuantumToChar(GetPixelBlue(hrz_image,p)/4);
339 p+=GetPixelChannels(hrz_image);
341 count=WriteBlob(image,(size_t) (q-pixels),pixels);
342 if (count != (ssize_t) (q-pixels))
344 status=SetImageProgress(image,SaveImageTag,y,hrz_image->rows);
345 if (status == MagickFalse)
348 pixels=(unsigned char *) RelinquishMagickMemory(pixels);
349 hrz_image=DestroyImage(hrz_image);
350 (void) CloseBlob(image);