1 /*
2 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
3 %                                                                             %
4 %                                                                             %
5 %                                                                             %
6 %                            SSSSS   CCCC  TTTTT                              %
7 %                            SS     C        T                                %
8 %                             SSS   C        T                                %
9 %                               SS  C        T                                %
10 %                            SSSSS   CCCC    T                                %
11 %                                                                             %
12 %                                                                             %
13 %                    Read Scitex HandShake Image Format                       %
14 %                                                                             %
15 %                              Software Design                                %
16 %                                   Cristy                                    %
17 %                                 July 1992                                   %
18 %                                                                             %
19 %                                                                             %
20 %  Copyright 1999-2021 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 %    https://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 "MagickCore/studio.h"
43 #include "MagickCore/blob.h"
44 #include "MagickCore/blob-private.h"
45 #include "MagickCore/cache.h"
46 #include "MagickCore/exception.h"
47 #include "MagickCore/exception-private.h"
48 #include "MagickCore/image.h"
49 #include "MagickCore/image-private.h"
50 #include "MagickCore/list.h"
51 #include "MagickCore/magick.h"
52 #include "MagickCore/memory_.h"
53 #include "MagickCore/module.h"
54 #include "MagickCore/monitor.h"
55 #include "MagickCore/monitor-private.h"
56 #include "MagickCore/pixel-accessor.h"
57 #include "MagickCore/quantum-private.h"
58 #include "MagickCore/static.h"
59 #include "MagickCore/string_.h"
60 #include "MagickCore/string-private.h"
61 
62 /*
63 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
64 %                                                                             %
65 %                                                                             %
66 %                                                                             %
67 %   I s S C T                                                                 %
68 %                                                                             %
69 %                                                                             %
70 %                                                                             %
71 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
72 %
73 %  IsSCT() returns MagickTrue if the image format type, identified by the
74 %  magick string, is SCT.
75 %
76 %  The format of the IsSCT method is:
77 %
78 %      MagickBooleanType IsSCT(const unsigned char *magick,const size_t length)
79 %
80 %  A description of each parameter follows:
81 %
82 %    o magick: compare image format pattern against these bytes.
83 %
84 %    o length: Specifies the length of the magick string.
85 %
86 */
IsSCT(const unsigned char * magick,const size_t length)87 static MagickBooleanType IsSCT(const unsigned char *magick,const size_t length)
88 {
89   if (length < 2)
90     return(MagickFalse);
91   if (LocaleNCompare((const char *) magick,"CT",2) == 0)
92     return(MagickTrue);
93   return(MagickFalse);
94 }
95 
96 /*
97 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
98 %                                                                             %
99 %                                                                             %
100 %                                                                             %
101 %   R e a d S C T I m a g e                                                   %
102 %                                                                             %
103 %                                                                             %
104 %                                                                             %
105 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
106 %
107 %  ReadSCTImage() reads a Scitex image file and returns it.  It allocates
108 %  the memory necessary for the new Image structure and returns a pointer to
109 %  the new image.
110 %
111 %  The format of the ReadSCTImage method is:
112 %
113 %      Image *ReadSCTImage(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 */
ReadSCTImage(const ImageInfo * image_info,ExceptionInfo * exception)122 static Image *ReadSCTImage(const ImageInfo *image_info,ExceptionInfo *exception)
123 {
124   char
125     magick[2];
126 
127   Image
128     *image;
129 
130   MagickBooleanType
131     status;
132 
133   double
134     height,
135     width;
136 
137   int
138     c;
139 
140   Quantum
141     pixel;
142 
143   ssize_t
144     i,
145     x;
146 
147   Quantum
148     *q;
149 
150   ssize_t
151     count,
152     y;
153 
154   unsigned char
155     buffer[768];
156 
157   size_t
158     separations,
159     separations_mask,
160     units;
161 
162   /*
163     Open image file.
164   */
165   assert(image_info != (const ImageInfo *) NULL);
166   assert(image_info->signature == MagickCoreSignature);
167   if (image_info->debug != MagickFalse)
168     (void) LogMagickEvent(TraceEvent,GetMagickModule(),"%s",
169       image_info->filename);
170   assert(exception != (ExceptionInfo *) NULL);
171   assert(exception->signature == MagickCoreSignature);
172   image=AcquireImage(image_info,exception);
173   status=OpenBlob(image_info,image,ReadBinaryBlobMode,exception);
174   if (status == MagickFalse)
175     {
176       image=DestroyImageList(image);
177       return((Image *) NULL);
178     }
179   /*
180     Read control block.
181   */
182   memset(magick,0,sizeof(magick));
183   memset(buffer,0,sizeof(buffer));
184   count=ReadBlob(image,80,buffer);
185   (void) count;
186   count=ReadBlob(image,2,(unsigned char *) magick);
187   if ((LocaleNCompare((char *) magick,"CT",2) != 0) &&
188       (LocaleNCompare((char *) magick,"LW",2) != 0) &&
189       (LocaleNCompare((char *) magick,"BM",2) != 0) &&
190       (LocaleNCompare((char *) magick,"PG",2) != 0) &&
191       (LocaleNCompare((char *) magick,"TX",2) != 0))
192     ThrowReaderException(CorruptImageError,"ImproperImageHeader");
193   if ((LocaleNCompare((char *) magick,"LW",2) == 0) ||
194       (LocaleNCompare((char *) magick,"BM",2) == 0) ||
195       (LocaleNCompare((char *) magick,"PG",2) == 0) ||
196       (LocaleNCompare((char *) magick,"TX",2) == 0))
197     ThrowReaderException(CoderError,"OnlyContinuousTonePictureSupported");
198   count=ReadBlob(image,174,buffer);
199   count=ReadBlob(image,768,buffer);
200   /*
201     Read paramter block.
202   */
203   units=1UL*ReadBlobByte(image);
204   if (units == 0)
205     image->units=PixelsPerCentimeterResolution;
206   separations=1UL*ReadBlobByte(image);
207   separations_mask=ReadBlobMSBShort(image);
208   count=ReadBlob(image,14,buffer);
209   buffer[14]='\0';
210   height=StringToDouble((char *) buffer,(char **) NULL);
211   count=ReadBlob(image,14,buffer);
212   width=StringToDouble((char *) buffer,(char **) NULL);
213   count=ReadBlob(image,12,buffer);
214   buffer[12]='\0';
215   image->rows=StringToUnsignedLong((char *) buffer);
216   count=ReadBlob(image,12,buffer);
217   image->columns=StringToUnsignedLong((char *) buffer);
218   count=ReadBlob(image,200,buffer);
219   count=ReadBlob(image,768,buffer);
220   if (separations_mask == 0x0f)
221     SetImageColorspace(image,CMYKColorspace,exception);
222   if ((image->columns < 1) || (image->rows < 1) ||
223       (width < MagickEpsilon) || (height < MagickEpsilon))
224     ThrowReaderException(CorruptImageError,"ImproperImageHeader");
225   image->resolution.x=1.0*image->columns/width;
226   image->resolution.y=1.0*image->rows/height;
227   if (image_info->ping != MagickFalse)
228     {
229       (void) CloseBlob(image);
230       return(GetFirstImageInList(image));
231     }
232   status=SetImageExtent(image,image->columns,image->rows,exception);
233   if (status == MagickFalse)
234     return(DestroyImageList(image));
235   /*
236     Convert SCT raster image to pixel packets.
237   */
238   (void) SetImageBackgroundColor(image,exception);
239   c=0;
240   for (y=0; y < (ssize_t) image->rows; y++)
241   {
242     for (i=0; i < (ssize_t) separations; i++)
243     {
244       q=GetAuthenticPixels(image,0,y,image->columns,1,exception);
245       if (q == (Quantum *) NULL)
246         break;
247       for (x=0; x < (ssize_t) image->columns; x++)
248       {
249         c=ReadBlobByte(image);
250         if (c == EOF)
251           break;
252         pixel=(Quantum) ScaleCharToQuantum((unsigned char) c);
253         if (image->colorspace == CMYKColorspace)
254           pixel=(Quantum) (QuantumRange-pixel);
255         switch (i)
256         {
257           case 0:
258           {
259             SetPixelRed(image,pixel,q);
260             SetPixelGreen(image,pixel,q);
261             SetPixelBlue(image,pixel,q);
262             break;
263           }
264           case 1:
265           {
266             SetPixelGreen(image,pixel,q);
267             break;
268           }
269           case 2:
270           {
271             SetPixelBlue(image,pixel,q);
272             break;
273           }
274           case 3:
275           {
276             if (image->colorspace == CMYKColorspace)
277               SetPixelBlack(image,pixel,q);
278             break;
279           }
280         }
281         q+=GetPixelChannels(image);
282       }
283       if (x < (ssize_t) image->columns)
284         break;
285       if (SyncAuthenticPixels(image,exception) == MagickFalse)
286         break;
287       if ((image->columns % 2) != 0)
288         (void) ReadBlobByte(image);  /* pad */
289     }
290     if (i < (ssize_t) separations)
291       break;
292     status=SetImageProgress(image,LoadImageTag,(MagickOffsetType) y,
293       image->rows);
294     if (status == MagickFalse)
295       break;
296   }
297   if (EOFBlob(image) != MagickFalse)
298     ThrowFileException(exception,CorruptImageError,"UnexpectedEndOfFile",
299       image->filename);
300   (void) CloseBlob(image);
301   return(GetFirstImageInList(image));
302 }
303 
304 /*
305 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
306 %                                                                             %
307 %                                                                             %
308 %                                                                             %
309 %   R e g i s t e r S C T I m a g e                                           %
310 %                                                                             %
311 %                                                                             %
312 %                                                                             %
313 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
314 %
315 %  RegisterSCTImage() adds attributes for the SCT image format to
316 %  the list of supported formats.  The attributes include the image format
317 %  tag, a method to read and/or write the format, whether the format
318 %  supports the saving of more than one frame to the same file or blob,
319 %  whether the format supports native in-memory I/O, and a brief
320 %  description of the format.
321 %
322 %  The format of the RegisterSCTImage method is:
323 %
324 %      size_t RegisterSCTImage(void)
325 %
326 */
RegisterSCTImage(void)327 ModuleExport size_t RegisterSCTImage(void)
328 {
329   MagickInfo
330     *entry;
331 
332   entry=AcquireMagickInfo("SCT","SCT","Scitex HandShake");
333   entry->decoder=(DecodeImageHandler *) ReadSCTImage;
334   entry->magick=(IsImageFormatHandler *) IsSCT;
335   entry->flags^=CoderAdjoinFlag;
336   (void) RegisterMagickInfo(entry);
337   return(MagickImageCoderSignature);
338 }
339 
340 /*
341 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
342 %                                                                             %
343 %                                                                             %
344 %                                                                             %
345 %   U n r e g i s t e r S C T I m a g e                                       %
346 %                                                                             %
347 %                                                                             %
348 %                                                                             %
349 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
350 %
351 %  UnregisterSCTImage() removes format registrations made by the
352 %  SCT module from the list of supported formats.
353 %
354 %  The format of the UnregisterSCTImage method is:
355 %
356 %      UnregisterSCTImage(void)
357 %
358 */
UnregisterSCTImage(void)359 ModuleExport void UnregisterSCTImage(void)
360 {
361   (void) UnregisterMagickInfo("SCT");
362 }
363