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