]> granicus.if.org Git - imagemagick/blob - coders/cip.c
(no commit message)
[imagemagick] / coders / cip.c
1 /*
2 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
3 %                                                                             %
4 %                                                                             %
5 %                             CCCC  IIIII  PPPP                               %
6 %                            C        I    P   P                              %
7 %                            C        I    PPPP                               %
8 %                            C        I    P                                  %
9 %                             CCCC  IIIII  P                                  %
10 %                                                                             %
11 %                                                                             %
12 %                  Read/Write Cisco IP Phone Image Format                     %
13 %                                                                             %
14 %                              Software Design                                %
15 %                                John Cristy                                  %
16 %                                April 2004                                   %
17 %                                                                             %
18 %                                                                             %
19 %  Copyright 1999-2011 ImageMagick Studio LLC, a non-profit organization      %
20 %  dedicated to making software imaging solutions freely available.           %
21 %                                                                             %
22 %  You may not use this file except in compliance with the License.  You may  %
23 %  obtain a copy of the License at                                            %
24 %                                                                             %
25 %    http://www.imagemagick.org/script/license.php                            %
26 %                                                                             %
27 %  Unless required by applicable law or agreed to in writing, software        %
28 %  distributed under the License is distributed on an "AS IS" BASIS,          %
29 %  WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.   %
30 %  See the License for the specific language governing permissions and        %
31 %  limitations under the License.                                             %
32 %                                                                             %
33 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
34 %
35 %
36 */
37 \f
38 /*
39   Include declarations.
40 */
41 #include "MagickCore/studio.h"
42 #include "MagickCore/blob.h"
43 #include "MagickCore/blob-private.h"
44 #include "MagickCore/cache.h"
45 #include "MagickCore/color-private.h"
46 #include "MagickCore/colorspace.h"
47 #include "MagickCore/constitute.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/property.h"
59 #include "MagickCore/quantize.h"
60 #include "MagickCore/quantum-private.h"
61 #include "MagickCore/static.h"
62 #include "MagickCore/string_.h"
63 #include "MagickCore/module.h"
64 #include "MagickCore/utility.h"
65 \f
66 /*
67   Forward declarations.
68 */
69 static MagickBooleanType
70   WriteCIPImage(const ImageInfo *,Image *);
71 \f
72 /*
73 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
74 %                                                                             %
75 %                                                                             %
76 %                                                                             %
77 %   R e g i s t e r C I P I m a g e                                           %
78 %                                                                             %
79 %                                                                             %
80 %                                                                             %
81 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
82 %
83 %  RegisterCIPImage() adds properties for the CIP IP phone image format to
84 %  the list of supported formats.  The properties include the image format
85 %  tag, a method to read and/or write the format, whether the format
86 %  supports the saving of more than one frame to the same file or blob,
87 %  whether the format supports native in-memory I/O, and a brief
88 %  description of the format.
89 %
90 %  The format of the RegisterCIPImage method is:
91 %
92 %      size_t RegisterCIPImage(void)
93 %
94 */
95 ModuleExport size_t RegisterCIPImage(void)
96 {
97   MagickInfo
98     *entry;
99
100   entry=SetMagickInfo("CIP");
101   entry->encoder=(EncodeImageHandler *) WriteCIPImage;
102   entry->adjoin=MagickFalse;
103   entry->description=ConstantString("Cisco IP phone image format");
104   entry->module=ConstantString("CIP");
105   (void) RegisterMagickInfo(entry);
106   return(MagickImageCoderSignature);
107 }
108 \f
109 /*
110 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
111 %                                                                             %
112 %                                                                             %
113 %                                                                             %
114 %   U n r e g i s t e r C I P I m a g e                                       %
115 %                                                                             %
116 %                                                                             %
117 %                                                                             %
118 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
119 %
120 %  UnregisterCIPImage() removes format registrations made by the
121 %  CIP module from the list of supported formats.
122 %
123 %  The format of the UnregisterCIPImage method is:
124 %
125 %      UnregisterCIPImage(void)
126 %
127 */
128 ModuleExport void UnregisterCIPImage(void)
129 {
130   (void) UnregisterMagickInfo("CIP");
131 }
132 \f
133 /*
134 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
135 %                                                                             %
136 %                                                                             %
137 %                                                                             %
138 %   W r i t e C I P I m a g e                                                 %
139 %                                                                             %
140 %                                                                             %
141 %                                                                             %
142 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
143 %
144 %  Procedure WriteCIPImage() writes an image to a file in the Cisco IP phone
145 %  image format.
146 %
147 %  The format of the WriteCIPImage method is:
148 %
149 %      MagickBooleanType WriteCIPImage(const ImageInfo *image_info,Image *image)
150 %
151 %  A description of each parameter follows.
152 %
153 %    o image_info: the image info.
154 %
155 %    o image:  The image.
156 %
157 */
158
159 static inline ssize_t MagickMin(const ssize_t x,const ssize_t y)
160 {
161   if (x < y)
162     return(x);
163   return(y);
164 }
165
166 static MagickBooleanType WriteCIPImage(const ImageInfo *image_info,Image *image)
167 {
168   char
169     buffer[MaxTextExtent];
170
171   const char
172     *value;
173
174   MagickBooleanType
175     status;
176
177   register const Quantum
178     *p;
179
180   register ssize_t
181     i,
182     x;
183
184   ssize_t
185     y;
186
187   unsigned char
188     byte;
189
190   /*
191     Open output image file.
192   */
193   assert(image_info != (const ImageInfo *) NULL);
194   assert(image_info->signature == MagickSignature);
195   assert(image != (Image *) NULL);
196   assert(image->signature == MagickSignature);
197   if (image->debug != MagickFalse)
198     (void) LogMagickEvent(TraceEvent,GetMagickModule(),"%s",image->filename);
199   status=OpenBlob(image_info,image,WriteBinaryBlobMode,&image->exception);
200   if (status == MagickFalse)
201     return(status);
202   (void) WriteBlobString(image,"<CiscoIPPhoneImage>\n");
203   value=GetImageProperty(image,"label");
204   if (value != (const char *) NULL)
205     (void) FormatLocaleString(buffer,MaxTextExtent,"<Title>%s</Title>\n",value);
206   else
207     {
208       char
209         basename[MaxTextExtent];
210
211       GetPathComponent(image->filename,BasePath,basename);
212       (void) FormatLocaleString(buffer,MaxTextExtent,"<Title>%s</Title>\n",
213         basename);
214     }
215   (void) WriteBlobString(image,buffer);
216   (void) FormatLocaleString(buffer,MaxTextExtent,
217     "<LocationX>%.20g</LocationX>\n",(double) image->page.x);
218   (void) WriteBlobString(image,buffer);
219   (void) FormatLocaleString(buffer,MaxTextExtent,
220     "<LocationY>%.20g</LocationY>\n",(double) image->page.y);
221   (void) WriteBlobString(image,buffer);
222   (void) FormatLocaleString(buffer,MaxTextExtent,"<Width>%.20g</Width>\n",
223     (double) (image->columns+(image->columns % 2)));
224   (void) WriteBlobString(image,buffer);
225   (void) FormatLocaleString(buffer,MaxTextExtent,"<Height>%.20g</Height>\n",
226     (double) image->rows);
227   (void) WriteBlobString(image,buffer);
228   (void) FormatLocaleString(buffer,MaxTextExtent,"<Depth>2</Depth>\n");
229   (void) WriteBlobString(image,buffer);
230   (void) WriteBlobString(image,"<Data>");
231   if (image->colorspace != RGBColorspace)
232     (void) TransformImageColorspace(image,RGBColorspace);
233   for (y=0; y < (ssize_t) image->rows; y++)
234   {
235     p=GetVirtualPixels(image,0,y,image->columns,1,&image->exception);
236     if (p == (const Quantum *) NULL)
237       break;
238     for (x=0; x < ((ssize_t) image->columns-3); x+=4)
239     {
240       byte=(unsigned char)
241         ((((size_t) (4*GetPixelIntensity(image,p+3)/QuantumRange) & 0x03) << 6) |
242          (((size_t) (4*GetPixelIntensity(image,p+2)/QuantumRange) & 0x03) << 4) |
243          (((size_t) (4*GetPixelIntensity(image,p+1)/QuantumRange) & 0x03) << 2) |
244          (((size_t) (4*GetPixelIntensity(image,p+0)/QuantumRange) & 0x03) << 0));
245       (void) FormatLocaleString(buffer,MaxTextExtent,"%02x",byte);
246       (void) WriteBlobString(image,buffer);
247       p+=4;
248     }
249     if ((image->columns % 4) != 0)
250       {
251         i=(ssize_t) image->columns % 4;
252         byte=(unsigned char)
253           ((((size_t) (4*GetPixelIntensity(image,p+MagickMin(i,3))/QuantumRange) & 0x03) << 6) |
254            (((size_t) (4*GetPixelIntensity(image,p+MagickMin(i,2))/QuantumRange) & 0x03) << 4) |
255            (((size_t) (4*GetPixelIntensity(image,p+MagickMin(i,1))/QuantumRange) & 0x03) << 2) |
256            (((size_t) (4*GetPixelIntensity(image,p+MagickMin(i,0))/QuantumRange) & 0x03) << 0));
257         (void) FormatLocaleString(buffer,MaxTextExtent,"%02x",~byte);
258         (void) WriteBlobString(image,buffer);
259       }
260     status=SetImageProgress(image,SaveImageTag,(MagickOffsetType) y,
261       image->rows);
262     if (status == MagickFalse)
263       break;
264   }
265   (void) WriteBlobString(image,"</Data>\n");
266   (void) WriteBlobString(image,"</CiscoIPPhoneImage>\n");
267   (void) CloseBlob(image);
268   return(MagickTrue);
269 }