cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 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 % |
cristy | de984cd | 2013-12-01 14:49:27 +0000 | [diff] [blame] | 15 | % Cristy % |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 16 | % April 2004 % |
| 17 | % % |
| 18 | % % |
Cristy | 7ce65e7 | 2015-12-12 18:03:16 -0500 | [diff] [blame] | 19 | % Copyright 1999-2016 ImageMagick Studio LLC, a non-profit organization % |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 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 | */ |
cristy | 4c08aed | 2011-07-01 19:47:50 +0000 | [diff] [blame] | 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" |
cristy | 510d06a | 2011-07-06 23:43:54 +0000 | [diff] [blame] | 47 | #include "MagickCore/colorspace-private.h" |
cristy | 4c08aed | 2011-07-01 19:47:50 +0000 | [diff] [blame] | 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" |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 66 | |
| 67 | /* |
| 68 | Forward declarations. |
| 69 | */ |
| 70 | static MagickBooleanType |
cristy | 1e178e7 | 2011-08-28 19:44:34 +0000 | [diff] [blame] | 71 | WriteCIPImage(const ImageInfo *,Image *,ExceptionInfo *); |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 72 | |
| 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 | % |
cristy | bb50337 | 2010-05-27 20:51:26 +0000 | [diff] [blame] | 93 | % size_t RegisterCIPImage(void) |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 94 | % |
| 95 | */ |
cristy | bb50337 | 2010-05-27 20:51:26 +0000 | [diff] [blame] | 96 | ModuleExport size_t RegisterCIPImage(void) |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 97 | { |
| 98 | MagickInfo |
| 99 | *entry; |
| 100 | |
dirk | 06b627a | 2015-04-06 18:59:17 +0000 | [diff] [blame] | 101 | entry=AcquireMagickInfo("CIP","CIP","Cisco IP phone image format"); |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 102 | entry->encoder=(EncodeImageHandler *) WriteCIPImage; |
dirk | 08e9a11 | 2015-02-22 01:51:41 +0000 | [diff] [blame] | 103 | entry->flags^=CoderAdjoinFlag; |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 104 | (void) RegisterMagickInfo(entry); |
| 105 | return(MagickImageCoderSignature); |
| 106 | } |
| 107 | |
| 108 | /* |
| 109 | %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% |
| 110 | % % |
| 111 | % % |
| 112 | % % |
| 113 | % U n r e g i s t e r C I P I m a g e % |
| 114 | % % |
| 115 | % % |
| 116 | % % |
| 117 | %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% |
| 118 | % |
| 119 | % UnregisterCIPImage() removes format registrations made by the |
| 120 | % CIP module from the list of supported formats. |
| 121 | % |
| 122 | % The format of the UnregisterCIPImage method is: |
| 123 | % |
| 124 | % UnregisterCIPImage(void) |
| 125 | % |
| 126 | */ |
| 127 | ModuleExport void UnregisterCIPImage(void) |
| 128 | { |
| 129 | (void) UnregisterMagickInfo("CIP"); |
| 130 | } |
| 131 | |
| 132 | /* |
| 133 | %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% |
| 134 | % % |
| 135 | % % |
| 136 | % % |
| 137 | % W r i t e C I P I m a g e % |
| 138 | % % |
| 139 | % % |
| 140 | % % |
| 141 | %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% |
| 142 | % |
| 143 | % Procedure WriteCIPImage() writes an image to a file in the Cisco IP phone |
| 144 | % image format. |
| 145 | % |
| 146 | % The format of the WriteCIPImage method is: |
| 147 | % |
cristy | 1e178e7 | 2011-08-28 19:44:34 +0000 | [diff] [blame] | 148 | % MagickBooleanType WriteCIPImage(const ImageInfo *image_info, |
| 149 | % Image *image,ExceptionInfo *exception) |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 150 | % |
| 151 | % A description of each parameter follows. |
| 152 | % |
| 153 | % o image_info: the image info. |
| 154 | % |
| 155 | % o image: The image. |
| 156 | % |
cristy | 1e178e7 | 2011-08-28 19:44:34 +0000 | [diff] [blame] | 157 | % o exception: return any errors or warnings in this structure. |
| 158 | % |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 159 | */ |
cristy | 1e178e7 | 2011-08-28 19:44:34 +0000 | [diff] [blame] | 160 | static MagickBooleanType WriteCIPImage(const ImageInfo *image_info,Image *image, |
| 161 | ExceptionInfo *exception) |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 162 | { |
| 163 | char |
cristy | 151b66d | 2015-04-15 10:50:31 +0000 | [diff] [blame] | 164 | buffer[MagickPathExtent]; |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 165 | |
| 166 | const char |
| 167 | *value; |
| 168 | |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 169 | MagickBooleanType |
| 170 | status; |
| 171 | |
cristy | 4c08aed | 2011-07-01 19:47:50 +0000 | [diff] [blame] | 172 | register const Quantum |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 173 | *p; |
| 174 | |
cristy | bb50337 | 2010-05-27 20:51:26 +0000 | [diff] [blame] | 175 | register ssize_t |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 176 | i, |
| 177 | x; |
| 178 | |
cristy | 4e82e51 | 2011-04-24 01:33:42 +0000 | [diff] [blame] | 179 | ssize_t |
| 180 | y; |
| 181 | |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 182 | unsigned char |
| 183 | byte; |
| 184 | |
| 185 | /* |
| 186 | Open output image file. |
| 187 | */ |
| 188 | assert(image_info != (const ImageInfo *) NULL); |
cristy | e1c94d9 | 2015-06-28 12:16:33 +0000 | [diff] [blame] | 189 | assert(image_info->signature == MagickCoreSignature); |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 190 | assert(image != (Image *) NULL); |
cristy | e1c94d9 | 2015-06-28 12:16:33 +0000 | [diff] [blame] | 191 | assert(image->signature == MagickCoreSignature); |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 192 | if (image->debug != MagickFalse) |
| 193 | (void) LogMagickEvent(TraceEvent,GetMagickModule(),"%s",image->filename); |
cristy | 3a37efd | 2011-08-28 20:31:03 +0000 | [diff] [blame] | 194 | assert(exception != (ExceptionInfo *) NULL); |
cristy | e1c94d9 | 2015-06-28 12:16:33 +0000 | [diff] [blame] | 195 | assert(exception->signature == MagickCoreSignature); |
cristy | 1e178e7 | 2011-08-28 19:44:34 +0000 | [diff] [blame] | 196 | status=OpenBlob(image_info,image,WriteBinaryBlobMode,exception); |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 197 | if (status == MagickFalse) |
| 198 | return(status); |
| 199 | (void) WriteBlobString(image,"<CiscoIPPhoneImage>\n"); |
cristy | d15e659 | 2011-10-15 00:13:06 +0000 | [diff] [blame] | 200 | value=GetImageProperty(image,"label",exception); |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 201 | if (value != (const char *) NULL) |
cristy | 151b66d | 2015-04-15 10:50:31 +0000 | [diff] [blame] | 202 | (void) FormatLocaleString(buffer,MagickPathExtent,"<Title>%s</Title>\n",value); |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 203 | else |
| 204 | { |
| 205 | char |
cristy | 151b66d | 2015-04-15 10:50:31 +0000 | [diff] [blame] | 206 | basename[MagickPathExtent]; |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 207 | |
| 208 | GetPathComponent(image->filename,BasePath,basename); |
cristy | 151b66d | 2015-04-15 10:50:31 +0000 | [diff] [blame] | 209 | (void) FormatLocaleString(buffer,MagickPathExtent,"<Title>%s</Title>\n", |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 210 | basename); |
| 211 | } |
| 212 | (void) WriteBlobString(image,buffer); |
cristy | 151b66d | 2015-04-15 10:50:31 +0000 | [diff] [blame] | 213 | (void) FormatLocaleString(buffer,MagickPathExtent, |
cristy | e8c25f9 | 2010-06-03 00:53:06 +0000 | [diff] [blame] | 214 | "<LocationX>%.20g</LocationX>\n",(double) image->page.x); |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 215 | (void) WriteBlobString(image,buffer); |
cristy | 151b66d | 2015-04-15 10:50:31 +0000 | [diff] [blame] | 216 | (void) FormatLocaleString(buffer,MagickPathExtent, |
cristy | e8c25f9 | 2010-06-03 00:53:06 +0000 | [diff] [blame] | 217 | "<LocationY>%.20g</LocationY>\n",(double) image->page.y); |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 218 | (void) WriteBlobString(image,buffer); |
cristy | 151b66d | 2015-04-15 10:50:31 +0000 | [diff] [blame] | 219 | (void) FormatLocaleString(buffer,MagickPathExtent,"<Width>%.20g</Width>\n", |
cristy | e8c25f9 | 2010-06-03 00:53:06 +0000 | [diff] [blame] | 220 | (double) (image->columns+(image->columns % 2))); |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 221 | (void) WriteBlobString(image,buffer); |
cristy | 151b66d | 2015-04-15 10:50:31 +0000 | [diff] [blame] | 222 | (void) FormatLocaleString(buffer,MagickPathExtent,"<Height>%.20g</Height>\n", |
cristy | e8c25f9 | 2010-06-03 00:53:06 +0000 | [diff] [blame] | 223 | (double) image->rows); |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 224 | (void) WriteBlobString(image,buffer); |
cristy | 151b66d | 2015-04-15 10:50:31 +0000 | [diff] [blame] | 225 | (void) FormatLocaleString(buffer,MagickPathExtent,"<Depth>2</Depth>\n"); |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 226 | (void) WriteBlobString(image,buffer); |
| 227 | (void) WriteBlobString(image,"<Data>"); |
cristy | af8d391 | 2014-02-21 14:50:33 +0000 | [diff] [blame] | 228 | (void) TransformImageColorspace(image,sRGBColorspace,exception); |
cristy | bb50337 | 2010-05-27 20:51:26 +0000 | [diff] [blame] | 229 | for (y=0; y < (ssize_t) image->rows; y++) |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 230 | { |
cristy | 1e178e7 | 2011-08-28 19:44:34 +0000 | [diff] [blame] | 231 | p=GetVirtualPixels(image,0,y,image->columns,1,exception); |
cristy | 4c08aed | 2011-07-01 19:47:50 +0000 | [diff] [blame] | 232 | if (p == (const Quantum *) NULL) |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 233 | break; |
cristy | bb50337 | 2010-05-27 20:51:26 +0000 | [diff] [blame] | 234 | for (x=0; x < ((ssize_t) image->columns-3); x+=4) |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 235 | { |
| 236 | byte=(unsigned char) |
cristy | 9160742 | 2013-07-03 00:14:54 +0000 | [diff] [blame] | 237 | ((((size_t) (3*ClampToQuantum(GetPixelLuma(image,p+3))/QuantumRange) & 0x03) << 6) | |
| 238 | (((size_t) (3*ClampToQuantum(GetPixelLuma(image,p+2))/QuantumRange) & 0x03) << 4) | |
| 239 | (((size_t) (3*ClampToQuantum(GetPixelLuma(image,p+1))/QuantumRange) & 0x03) << 2) | |
| 240 | (((size_t) (3*ClampToQuantum(GetPixelLuma(image,p+0))/QuantumRange) & 0x03) << 0)); |
cristy | 151b66d | 2015-04-15 10:50:31 +0000 | [diff] [blame] | 241 | (void) FormatLocaleString(buffer,MagickPathExtent,"%02x",byte); |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 242 | (void) WriteBlobString(image,buffer); |
| 243 | p+=4; |
| 244 | } |
| 245 | if ((image->columns % 4) != 0) |
| 246 | { |
cristy | bb50337 | 2010-05-27 20:51:26 +0000 | [diff] [blame] | 247 | i=(ssize_t) image->columns % 4; |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 248 | byte=(unsigned char) |
cristy | 9160742 | 2013-07-03 00:14:54 +0000 | [diff] [blame] | 249 | ((((size_t) (3*ClampToQuantum(GetPixelLuma(image,p+MagickMin(i,3)))/QuantumRange) & 0x03) << 6) | |
| 250 | (((size_t) (3*ClampToQuantum(GetPixelLuma(image,p+MagickMin(i,2)))/QuantumRange) & 0x03) << 4) | |
| 251 | (((size_t) (3*ClampToQuantum(GetPixelLuma(image,p+MagickMin(i,1)))/QuantumRange) & 0x03) << 2) | |
| 252 | (((size_t) (3*ClampToQuantum(GetPixelLuma(image,p+MagickMin(i,0)))/QuantumRange) & 0x03) << 0)); |
cristy | 151b66d | 2015-04-15 10:50:31 +0000 | [diff] [blame] | 253 | (void) FormatLocaleString(buffer,MagickPathExtent,"%02x",~byte); |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 254 | (void) WriteBlobString(image,buffer); |
| 255 | } |
cristy | cee9711 | 2010-05-28 00:44:52 +0000 | [diff] [blame] | 256 | status=SetImageProgress(image,SaveImageTag,(MagickOffsetType) y, |
cristy | 4e82e51 | 2011-04-24 01:33:42 +0000 | [diff] [blame] | 257 | image->rows); |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 258 | if (status == MagickFalse) |
| 259 | break; |
| 260 | } |
| 261 | (void) WriteBlobString(image,"</Data>\n"); |
| 262 | (void) WriteBlobString(image,"</CiscoIPPhoneImage>\n"); |
| 263 | (void) CloseBlob(image); |
| 264 | return(MagickTrue); |
| 265 | } |