]> granicus.if.org Git - imagemagick/blob - coders/cip.c
3f847cb44df9b04731d642c93db93ccc3ddc38b7
[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-2012 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/colorspace-private.h"
48 #include "MagickCore/constitute.h"
49 #include "MagickCore/exception.h"
50 #include "MagickCore/exception-private.h"
51 #include "MagickCore/image.h"
52 #include "MagickCore/image-private.h"
53 #include "MagickCore/list.h"
54 #include "MagickCore/magick.h"
55 #include "MagickCore/memory_.h"
56 #include "MagickCore/monitor.h"
57 #include "MagickCore/monitor-private.h"
58 #include "MagickCore/pixel-accessor.h"
59 #include "MagickCore/property.h"
60 #include "MagickCore/quantize.h"
61 #include "MagickCore/quantum-private.h"
62 #include "MagickCore/static.h"
63 #include "MagickCore/string_.h"
64 #include "MagickCore/module.h"
65 #include "MagickCore/utility.h"
66 \f
67 /*
68   Forward declarations.
69 */
70 static MagickBooleanType
71   WriteCIPImage(const ImageInfo *,Image *,ExceptionInfo *);
72 \f
73 /*
74 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
75 %                                                                             %
76 %                                                                             %
77 %                                                                             %
78 %   R e g i s t e r C I P I m a g e                                           %
79 %                                                                             %
80 %                                                                             %
81 %                                                                             %
82 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
83 %
84 %  RegisterCIPImage() adds properties for the CIP IP phone image format to
85 %  the list of supported formats.  The properties include the image format
86 %  tag, a method to read and/or write the format, whether the format
87 %  supports the saving of more than one frame to the same file or blob,
88 %  whether the format supports native in-memory I/O, and a brief
89 %  description of the format.
90 %
91 %  The format of the RegisterCIPImage method is:
92 %
93 %      size_t RegisterCIPImage(void)
94 %
95 */
96 ModuleExport size_t RegisterCIPImage(void)
97 {
98   MagickInfo
99     *entry;
100
101   entry=SetMagickInfo("CIP");
102   entry->encoder=(EncodeImageHandler *) WriteCIPImage;
103   entry->adjoin=MagickFalse;
104   entry->description=ConstantString("Cisco IP phone image format");
105   entry->module=ConstantString("CIP");
106   (void) RegisterMagickInfo(entry);
107   return(MagickImageCoderSignature);
108 }
109 \f
110 /*
111 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
112 %                                                                             %
113 %                                                                             %
114 %                                                                             %
115 %   U n r e g i s t e r C I P I m a g e                                       %
116 %                                                                             %
117 %                                                                             %
118 %                                                                             %
119 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
120 %
121 %  UnregisterCIPImage() removes format registrations made by the
122 %  CIP module from the list of supported formats.
123 %
124 %  The format of the UnregisterCIPImage method is:
125 %
126 %      UnregisterCIPImage(void)
127 %
128 */
129 ModuleExport void UnregisterCIPImage(void)
130 {
131   (void) UnregisterMagickInfo("CIP");
132 }
133 \f
134 /*
135 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
136 %                                                                             %
137 %                                                                             %
138 %                                                                             %
139 %   W r i t e C I P I m a g e                                                 %
140 %                                                                             %
141 %                                                                             %
142 %                                                                             %
143 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
144 %
145 %  Procedure WriteCIPImage() writes an image to a file in the Cisco IP phone
146 %  image format.
147 %
148 %  The format of the WriteCIPImage method is:
149 %
150 %      MagickBooleanType WriteCIPImage(const ImageInfo *image_info,
151 %        Image *image,ExceptionInfo *exception)
152 %
153 %  A description of each parameter follows.
154 %
155 %    o image_info: the image info.
156 %
157 %    o image:  The image.
158 %
159 %    o exception: return any errors or warnings in this structure.
160 %
161 */
162
163 static inline ssize_t MagickMin(const ssize_t x,const ssize_t y)
164 {
165   if (x < y)
166     return(x);
167   return(y);
168 }
169
170 static MagickBooleanType WriteCIPImage(const ImageInfo *image_info,Image *image,
171   ExceptionInfo *exception)
172 {
173   char
174     buffer[MaxTextExtent];
175
176   const char
177     *value;
178
179   MagickBooleanType
180     status;
181
182   register const Quantum
183     *p;
184
185   register ssize_t
186     i,
187     x;
188
189   ssize_t
190     y;
191
192   unsigned char
193     byte;
194
195   /*
196     Open output image file.
197   */
198   assert(image_info != (const ImageInfo *) NULL);
199   assert(image_info->signature == MagickSignature);
200   assert(image != (Image *) NULL);
201   assert(image->signature == MagickSignature);
202   if (image->debug != MagickFalse)
203     (void) LogMagickEvent(TraceEvent,GetMagickModule(),"%s",image->filename);
204   assert(exception != (ExceptionInfo *) NULL);
205   assert(exception->signature == MagickSignature);
206   status=OpenBlob(image_info,image,WriteBinaryBlobMode,exception);
207   if (status == MagickFalse)
208     return(status);
209   (void) WriteBlobString(image,"<CiscoIPPhoneImage>\n");
210   value=GetImageProperty(image,"label",exception);
211   if (value != (const char *) NULL)
212     (void) FormatLocaleString(buffer,MaxTextExtent,"<Title>%s</Title>\n",value);
213   else
214     {
215       char
216         basename[MaxTextExtent];
217
218       GetPathComponent(image->filename,BasePath,basename);
219       (void) FormatLocaleString(buffer,MaxTextExtent,"<Title>%s</Title>\n",
220         basename);
221     }
222   (void) WriteBlobString(image,buffer);
223   (void) FormatLocaleString(buffer,MaxTextExtent,
224     "<LocationX>%.20g</LocationX>\n",(double) image->page.x);
225   (void) WriteBlobString(image,buffer);
226   (void) FormatLocaleString(buffer,MaxTextExtent,
227     "<LocationY>%.20g</LocationY>\n",(double) image->page.y);
228   (void) WriteBlobString(image,buffer);
229   (void) FormatLocaleString(buffer,MaxTextExtent,"<Width>%.20g</Width>\n",
230     (double) (image->columns+(image->columns % 2)));
231   (void) WriteBlobString(image,buffer);
232   (void) FormatLocaleString(buffer,MaxTextExtent,"<Height>%.20g</Height>\n",
233     (double) image->rows);
234   (void) WriteBlobString(image,buffer);
235   (void) FormatLocaleString(buffer,MaxTextExtent,"<Depth>2</Depth>\n");
236   (void) WriteBlobString(image,buffer);
237   (void) WriteBlobString(image,"<Data>");
238   if (IssRGBCompatibleColorspace(image->colorspace) == MagickFalse)
239     (void) TransformImageColorspace(image,sRGBColorspace,exception);
240   for (y=0; y < (ssize_t) image->rows; y++)
241   {
242     p=GetVirtualPixels(image,0,y,image->columns,1,exception);
243     if (p == (const Quantum *) NULL)
244       break;
245     for (x=0; x < ((ssize_t) image->columns-3); x+=4)
246     {
247       byte=(unsigned char)
248         ((((size_t) (4*GetPixelIntensity(image,p+3)/QuantumRange) & 0x03) << 6) |
249          (((size_t) (4*GetPixelIntensity(image,p+2)/QuantumRange) & 0x03) << 4) |
250          (((size_t) (4*GetPixelIntensity(image,p+1)/QuantumRange) & 0x03) << 2) |
251          (((size_t) (4*GetPixelIntensity(image,p+0)/QuantumRange) & 0x03) << 0));
252       (void) FormatLocaleString(buffer,MaxTextExtent,"%02x",byte);
253       (void) WriteBlobString(image,buffer);
254       p+=4;
255     }
256     if ((image->columns % 4) != 0)
257       {
258         i=(ssize_t) image->columns % 4;
259         byte=(unsigned char)
260           ((((size_t) (4*GetPixelIntensity(image,p+MagickMin(i,3))/QuantumRange) & 0x03) << 6) |
261            (((size_t) (4*GetPixelIntensity(image,p+MagickMin(i,2))/QuantumRange) & 0x03) << 4) |
262            (((size_t) (4*GetPixelIntensity(image,p+MagickMin(i,1))/QuantumRange) & 0x03) << 2) |
263            (((size_t) (4*GetPixelIntensity(image,p+MagickMin(i,0))/QuantumRange) & 0x03) << 0));
264         (void) FormatLocaleString(buffer,MaxTextExtent,"%02x",~byte);
265         (void) WriteBlobString(image,buffer);
266       }
267     status=SetImageProgress(image,SaveImageTag,(MagickOffsetType) y,
268       image->rows);
269     if (status == MagickFalse)
270       break;
271   }
272   (void) WriteBlobString(image,"</Data>\n");
273   (void) WriteBlobString(image,"</CiscoIPPhoneImage>\n");
274   (void) CloseBlob(image);
275   return(MagickTrue);
276 }