root / ImageMagick / trunk / coders / cip.c

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