blob: 86a3427a065304f12cdc5d85de56e282873cc0c2 [file] [log] [blame]
cristy3ed852e2009-09-05 21:47:34 +00001/*
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 %
cristyde984cd2013-12-01 14:49:27 +000015% Cristy %
cristy3ed852e2009-09-05 21:47:34 +000016% April 2004 %
17% %
18% %
cristyb56bb242014-11-25 17:12:48 +000019% Copyright 1999-2015 ImageMagick Studio LLC, a non-profit organization %
cristy3ed852e2009-09-05 21:47:34 +000020% 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
38/*
39 Include declarations.
40*/
cristy4c08aed2011-07-01 19:47:50 +000041#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"
cristy510d06a2011-07-06 23:43:54 +000047#include "MagickCore/colorspace-private.h"
cristy4c08aed2011-07-01 19:47:50 +000048#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"
cristy3ed852e2009-09-05 21:47:34 +000066
67/*
68 Forward declarations.
69*/
70static MagickBooleanType
cristy1e178e72011-08-28 19:44:34 +000071 WriteCIPImage(const ImageInfo *,Image *,ExceptionInfo *);
cristy3ed852e2009-09-05 21:47:34 +000072
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%
cristybb503372010-05-27 20:51:26 +000093% size_t RegisterCIPImage(void)
cristy3ed852e2009-09-05 21:47:34 +000094%
95*/
cristybb503372010-05-27 20:51:26 +000096ModuleExport size_t RegisterCIPImage(void)
cristy3ed852e2009-09-05 21:47:34 +000097{
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
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*/
129ModuleExport void UnregisterCIPImage(void)
130{
131 (void) UnregisterMagickInfo("CIP");
132}
133
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%
cristy1e178e72011-08-28 19:44:34 +0000150% MagickBooleanType WriteCIPImage(const ImageInfo *image_info,
151% Image *image,ExceptionInfo *exception)
cristy3ed852e2009-09-05 21:47:34 +0000152%
153% A description of each parameter follows.
154%
155% o image_info: the image info.
156%
157% o image: The image.
158%
cristy1e178e72011-08-28 19:44:34 +0000159% o exception: return any errors or warnings in this structure.
160%
cristy3ed852e2009-09-05 21:47:34 +0000161*/
cristy1e178e72011-08-28 19:44:34 +0000162static MagickBooleanType WriteCIPImage(const ImageInfo *image_info,Image *image,
163 ExceptionInfo *exception)
cristy3ed852e2009-09-05 21:47:34 +0000164{
165 char
166 buffer[MaxTextExtent];
167
168 const char
169 *value;
170
cristy3ed852e2009-09-05 21:47:34 +0000171 MagickBooleanType
172 status;
173
cristy4c08aed2011-07-01 19:47:50 +0000174 register const Quantum
cristy3ed852e2009-09-05 21:47:34 +0000175 *p;
176
cristybb503372010-05-27 20:51:26 +0000177 register ssize_t
cristy3ed852e2009-09-05 21:47:34 +0000178 i,
179 x;
180
cristy4e82e512011-04-24 01:33:42 +0000181 ssize_t
182 y;
183
cristy3ed852e2009-09-05 21:47:34 +0000184 unsigned char
185 byte;
186
187 /*
188 Open output image file.
189 */
190 assert(image_info != (const ImageInfo *) NULL);
191 assert(image_info->signature == MagickSignature);
192 assert(image != (Image *) NULL);
193 assert(image->signature == MagickSignature);
194 if (image->debug != MagickFalse)
195 (void) LogMagickEvent(TraceEvent,GetMagickModule(),"%s",image->filename);
cristy3a37efd2011-08-28 20:31:03 +0000196 assert(exception != (ExceptionInfo *) NULL);
197 assert(exception->signature == MagickSignature);
cristy1e178e72011-08-28 19:44:34 +0000198 status=OpenBlob(image_info,image,WriteBinaryBlobMode,exception);
cristy3ed852e2009-09-05 21:47:34 +0000199 if (status == MagickFalse)
200 return(status);
201 (void) WriteBlobString(image,"<CiscoIPPhoneImage>\n");
cristyd15e6592011-10-15 00:13:06 +0000202 value=GetImageProperty(image,"label",exception);
cristy3ed852e2009-09-05 21:47:34 +0000203 if (value != (const char *) NULL)
cristyb51dff52011-05-19 16:55:47 +0000204 (void) FormatLocaleString(buffer,MaxTextExtent,"<Title>%s</Title>\n",value);
cristy3ed852e2009-09-05 21:47:34 +0000205 else
206 {
207 char
208 basename[MaxTextExtent];
209
210 GetPathComponent(image->filename,BasePath,basename);
cristyb51dff52011-05-19 16:55:47 +0000211 (void) FormatLocaleString(buffer,MaxTextExtent,"<Title>%s</Title>\n",
cristy3ed852e2009-09-05 21:47:34 +0000212 basename);
213 }
214 (void) WriteBlobString(image,buffer);
cristyb51dff52011-05-19 16:55:47 +0000215 (void) FormatLocaleString(buffer,MaxTextExtent,
cristye8c25f92010-06-03 00:53:06 +0000216 "<LocationX>%.20g</LocationX>\n",(double) image->page.x);
cristy3ed852e2009-09-05 21:47:34 +0000217 (void) WriteBlobString(image,buffer);
cristyb51dff52011-05-19 16:55:47 +0000218 (void) FormatLocaleString(buffer,MaxTextExtent,
cristye8c25f92010-06-03 00:53:06 +0000219 "<LocationY>%.20g</LocationY>\n",(double) image->page.y);
cristy3ed852e2009-09-05 21:47:34 +0000220 (void) WriteBlobString(image,buffer);
cristyb51dff52011-05-19 16:55:47 +0000221 (void) FormatLocaleString(buffer,MaxTextExtent,"<Width>%.20g</Width>\n",
cristye8c25f92010-06-03 00:53:06 +0000222 (double) (image->columns+(image->columns % 2)));
cristy3ed852e2009-09-05 21:47:34 +0000223 (void) WriteBlobString(image,buffer);
cristyb51dff52011-05-19 16:55:47 +0000224 (void) FormatLocaleString(buffer,MaxTextExtent,"<Height>%.20g</Height>\n",
cristye8c25f92010-06-03 00:53:06 +0000225 (double) image->rows);
cristy3ed852e2009-09-05 21:47:34 +0000226 (void) WriteBlobString(image,buffer);
cristyb51dff52011-05-19 16:55:47 +0000227 (void) FormatLocaleString(buffer,MaxTextExtent,"<Depth>2</Depth>\n");
cristy3ed852e2009-09-05 21:47:34 +0000228 (void) WriteBlobString(image,buffer);
229 (void) WriteBlobString(image,"<Data>");
cristyaf8d3912014-02-21 14:50:33 +0000230 (void) TransformImageColorspace(image,sRGBColorspace,exception);
cristybb503372010-05-27 20:51:26 +0000231 for (y=0; y < (ssize_t) image->rows; y++)
cristy3ed852e2009-09-05 21:47:34 +0000232 {
cristy1e178e72011-08-28 19:44:34 +0000233 p=GetVirtualPixels(image,0,y,image->columns,1,exception);
cristy4c08aed2011-07-01 19:47:50 +0000234 if (p == (const Quantum *) NULL)
cristy3ed852e2009-09-05 21:47:34 +0000235 break;
cristybb503372010-05-27 20:51:26 +0000236 for (x=0; x < ((ssize_t) image->columns-3); x+=4)
cristy3ed852e2009-09-05 21:47:34 +0000237 {
238 byte=(unsigned char)
cristy91607422013-07-03 00:14:54 +0000239 ((((size_t) (3*ClampToQuantum(GetPixelLuma(image,p+3))/QuantumRange) & 0x03) << 6) |
240 (((size_t) (3*ClampToQuantum(GetPixelLuma(image,p+2))/QuantumRange) & 0x03) << 4) |
241 (((size_t) (3*ClampToQuantum(GetPixelLuma(image,p+1))/QuantumRange) & 0x03) << 2) |
242 (((size_t) (3*ClampToQuantum(GetPixelLuma(image,p+0))/QuantumRange) & 0x03) << 0));
cristyb51dff52011-05-19 16:55:47 +0000243 (void) FormatLocaleString(buffer,MaxTextExtent,"%02x",byte);
cristy3ed852e2009-09-05 21:47:34 +0000244 (void) WriteBlobString(image,buffer);
245 p+=4;
246 }
247 if ((image->columns % 4) != 0)
248 {
cristybb503372010-05-27 20:51:26 +0000249 i=(ssize_t) image->columns % 4;
cristy3ed852e2009-09-05 21:47:34 +0000250 byte=(unsigned char)
cristy91607422013-07-03 00:14:54 +0000251 ((((size_t) (3*ClampToQuantum(GetPixelLuma(image,p+MagickMin(i,3)))/QuantumRange) & 0x03) << 6) |
252 (((size_t) (3*ClampToQuantum(GetPixelLuma(image,p+MagickMin(i,2)))/QuantumRange) & 0x03) << 4) |
253 (((size_t) (3*ClampToQuantum(GetPixelLuma(image,p+MagickMin(i,1)))/QuantumRange) & 0x03) << 2) |
254 (((size_t) (3*ClampToQuantum(GetPixelLuma(image,p+MagickMin(i,0)))/QuantumRange) & 0x03) << 0));
cristyb51dff52011-05-19 16:55:47 +0000255 (void) FormatLocaleString(buffer,MaxTextExtent,"%02x",~byte);
cristy3ed852e2009-09-05 21:47:34 +0000256 (void) WriteBlobString(image,buffer);
257 }
cristycee97112010-05-28 00:44:52 +0000258 status=SetImageProgress(image,SaveImageTag,(MagickOffsetType) y,
cristy4e82e512011-04-24 01:33:42 +0000259 image->rows);
cristy3ed852e2009-09-05 21:47:34 +0000260 if (status == MagickFalse)
261 break;
262 }
263 (void) WriteBlobString(image,"</Data>\n");
264 (void) WriteBlobString(image,"</CiscoIPPhoneImage>\n");
265 (void) CloseBlob(image);
266 return(MagickTrue);
267}