Powered by Pair ImageMagick logo
Image Magick
Main Page | Namespace List | Class Hierarchy | Alphabetical List | Data Structures | File List | Namespace Members | Data Fields | Globals | Related Pages

ImageMagick-6.1.1/coders/map.c

Go to the documentation of this file.
00001 /* 00002 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% 00003 % % 00004 % % 00005 % % 00006 % M M AAA PPPP % 00007 % MM MM A A P P % 00008 % M M M AAAAA PPPP % 00009 % M M A A P % 00010 % M M A A P % 00011 % % 00012 % % 00013 % Read/Write Image Colormaps As An Image File. % 00014 % % 00015 % Software Design % 00016 % John Cristy % 00017 % July 1992 % 00018 % % 00019 % % 00020 % Copyright 1999-2004 ImageMagick Studio LLC, a non-profit organization % 00021 % dedicated to making software imaging solutions freely available. % 00022 % % 00023 % You may not use this file except in compliance with the License. You may % 00024 % obtain a copy of the License at % 00025 % % 00026 % http://www.imagemagick.org/www/Copyright.html % 00027 % % 00028 % Unless required by applicable law or agreed to in writing, software % 00029 % distributed under the License is distributed on an "AS IS" BASIS, % 00030 % WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. % 00031 % See the License for the specific language governing permissions and % 00032 % limitations under the License. % 00033 % % 00034 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% 00035 % 00036 % 00037 */ 00038 00039 /* 00040 Include declarations. 00041 */ 00042 #include "magick/studio.h" 00043 #include "magick/blob.h" 00044 #include "magick/blob_private.h" 00045 #include "magick/color.h" 00046 #include "magick/color_private.h" 00047 #include "magick/colorspace.h" 00048 #include "magick/error.h" 00049 #include "magick/error_private.h" 00050 #include "magick/image.h" 00051 #include "magick/image_private.h" 00052 #include "magick/list.h" 00053 #include "magick/magick.h" 00054 #include "magick/memory_.h" 00055 #include "magick/static.h" 00056 #include "magick/string_.h" 00057 00058 /* 00059 Forward declarations. 00060 */ 00061 static MagickBooleanType 00062 WriteMAPImage(const ImageInfo *,Image *); 00063 00064 /* 00065 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% 00066 % % 00067 % % 00068 % % 00069 % R e a d M A P I m a g e % 00070 % % 00071 % % 00072 % % 00073 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% 00074 % 00075 % ReadMAPImage() reads an image of raw RGB colormap and colormap index 00076 % bytes and returns it. It allocates the memory necessary for the new Image 00077 % structure and returns a pointer to the new image. 00078 % 00079 % The format of the ReadMAPImage method is: 00080 % 00081 % Image *ReadMAPImage(const ImageInfo *image_info,ExceptionInfo *exception) 00082 % 00083 % A description of each parameter follows: 00084 % 00085 % o image_info: The image info. 00086 % 00087 % o exception: return any errors or warnings in this structure. 00088 % 00089 % 00090 */ 00091 static Image *ReadMAPImage(const ImageInfo *image_info,ExceptionInfo *exception) 00092 { 00093 Image 00094 *image; 00095 00096 IndexPacket 00097 index; 00098 00099 long 00100 y; 00101 00102 MagickBooleanType 00103 status; 00104 00105 register IndexPacket 00106 *indexes; 00107 00108 register long 00109 x; 00110 00111 register PixelPacket 00112 *q; 00113 00114 register long 00115 i; 00116 00117 register unsigned char 00118 *p; 00119 00120 size_t 00121 packet_size; 00122 00123 unsigned char 00124 *colormap, 00125 *pixels; 00126 00127 unsigned long 00128 depth; 00129 00130 /* 00131 Open image file. 00132 */ 00133 assert(image_info != (const ImageInfo *) NULL); 00134 assert(image_info->signature == MagickSignature); 00135 if (image_info->debug != MagickFalse) 00136 (void) LogMagickEvent(TraceEvent,GetMagickModule(),image_info->filename); 00137 assert(exception != (ExceptionInfo *) NULL); 00138 assert(exception->signature == MagickSignature); 00139 image=AllocateImage(image_info); 00140 if ((image->columns == 0) || (image->rows == 0)) 00141 ThrowReaderException(OptionError,"MustSpecifyImageSize"); 00142 status=OpenBlob(image_info,image,ReadBinaryBlobMode,exception); 00143 if (status == MagickFalse) 00144 { 00145 DestroyImageList(image); 00146 return((Image *) NULL); 00147 } 00148 /* 00149 Initialize image structure. 00150 */ 00151 image->storage_class=PseudoClass; 00152 status=AllocateImageColormap(image,(unsigned long) 00153 (image->offset != 0 ? image->offset : 256)); 00154 if (status == MagickFalse) 00155 ThrowReaderException(ResourceLimitError,"MemoryAllocationFailed"); 00156 depth=GetImageQuantumDepth(image,MagickTrue); 00157 packet_size=(size_t) (depth/8); 00158 pixels=(unsigned char *) AcquireMagickMemory(packet_size*image->columns); 00159 packet_size=(size_t) (image->colors > 256 ? 6 : 3); 00160 colormap=(unsigned char *) AcquireMagickMemory(packet_size*image->colors); 00161 if ((pixels == (unsigned char *) NULL) || 00162 (colormap == (unsigned char *) NULL)) 00163 ThrowReaderException(ResourceLimitError,"MemoryAllocationFailed"); 00164 /* 00165 Read image colormap. 00166 */ 00167 (void) ReadBlob(image,packet_size*image->colors,colormap); 00168 p=colormap; 00169 if (image->depth <= 8) 00170 for (i=0; i < (long) image->colors; i++) 00171 { 00172 image->colormap[i].red=ScaleCharToQuantum(*p++); 00173 image->colormap[i].green=ScaleCharToQuantum(*p++); 00174 image->colormap[i].blue=ScaleCharToQuantum(*p++); 00175 } 00176 else 00177 for (i=0; i < (long) image->colors; i++) 00178 { 00179 image->colormap[i].red=(Quantum) (*p++ << 8); 00180 image->colormap[i].red|=(*p++); 00181 image->colormap[i].green=(Quantum) (*p++ << 8); 00182 image->colormap[i].green|=(*p++); 00183 image->colormap[i].blue=(Quantum) (*p++ << 8); 00184 image->colormap[i].blue|=(*p++); 00185 } 00186 colormap=(unsigned char *) RelinquishMagickMemory(colormap); 00187 if (image_info->ping != MagickFalse) 00188 { 00189 CloseBlob(image); 00190 return(GetFirstImageInList(image)); 00191 } 00192 /* 00193 Read image pixels. 00194 */ 00195 packet_size=(size_t) (depth/8); 00196 for (y=0; y < (long) image->rows; y++) 00197 { 00198 p=pixels; 00199 q=SetImagePixels(image,0,y,image->columns,1); 00200 if (q == (PixelPacket *) NULL) 00201 break; 00202 indexes=GetIndexes(image); 00203 (void) ReadBlob(image,(size_t) packet_size*image->columns,pixels); 00204 for (x=0; x < (long) image->columns; x++) 00205 { 00206 index=ConstrainColormapIndex(image,*p); 00207 p++; 00208 if (image->colors > 256) 00209 { 00210 index=ConstrainColormapIndex(image,((int) (index << 8)+(int) (*p))); 00211 p++; 00212 } 00213 indexes[x]=(IndexPacket) index; 00214 *q++=image->colormap[index]; 00215 } 00216 if (SyncImagePixels(image) == MagickFalse) 00217 break; 00218 } 00219 pixels=(unsigned char *) RelinquishMagickMemory(pixels); 00220 if (EOFBlob(image) != MagickFalse) 00221 ThrowFileException(exception,CorruptImageError,"UnexpectedEndOfFile", 00222 image->filename); 00223 CloseBlob(image); 00224 return(GetFirstImageInList(image)); 00225 } 00226 00227 /* 00228 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% 00229 % % 00230 % % 00231 % % 00232 % R e g i s t e r M A P I m a g e % 00233 % % 00234 % % 00235 % % 00236 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% 00237 % 00238 % RegisterMAPImage() adds attributes for the MAP image format to 00239 % the list of supported formats. The attributes include the image format 00240 % tag, a method to read and/or write the format, whether the format 00241 % supports the saving of more than one frame to the same file or blob, 00242 % whether the format supports native in-memory I/O, and a brief 00243 % description of the format. 00244 % 00245 % The format of the RegisterMAPImage method is: 00246 % 00247 % RegisterMAPImage(void) 00248 % 00249 */ 00250 ModuleExport void RegisterMAPImage(void) 00251 { 00252 MagickInfo 00253 *entry; 00254 00255 entry=SetMagickInfo("MAP"); 00256 entry->decoder=(DecoderHandler *) ReadMAPImage; 00257 entry->encoder=(EncoderHandler *) WriteMAPImage; 00258 entry->adjoin=MagickFalse; 00259 entry->raw=MagickTrue; 00260 entry->endian_support=MagickTrue; 00261 entry->description=AcquireString("Colormap intensities and indices"); 00262 entry->module=AcquireString("MAP"); 00263 (void) RegisterMagickInfo(entry); 00264 } 00265 00266 /* 00267 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% 00268 % % 00269 % % 00270 % % 00271 % U n r e g i s t e r M A P I m a g e % 00272 % % 00273 % % 00274 % % 00275 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% 00276 % 00277 % UnregisterMAPImage() removes format registrations made by the 00278 % MAP module from the list of supported formats. 00279 % 00280 % The format of the UnregisterMAPImage method is: 00281 % 00282 % UnregisterMAPImage(void) 00283 % 00284 */ 00285 ModuleExport void UnregisterMAPImage(void) 00286 { 00287 (void) UnregisterMagickInfo("MAP"); 00288 } 00289 00290 /* 00291 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% 00292 % % 00293 % % 00294 % % 00295 % W r i t e M A P I m a g e % 00296 % % 00297 % % 00298 % % 00299 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% 00300 % 00301 % WriteMAPImage() writes an image to a file as red, green, and blue 00302 % colormap bytes followed by the colormap indexes. 00303 % 00304 % The format of the WriteMAPImage method is: 00305 % 00306 % MagickBooleanType WriteMAPImage(const ImageInfo *image_info,Image *image) 00307 % 00308 % A description of each parameter follows. 00309 % 00310 % o image_info: The image info. 00311 % 00312 % o image: The image. 00313 % 00314 % 00315 */ 00316 static MagickBooleanType WriteMAPImage(const ImageInfo *image_info,Image *image) 00317 { 00318 long 00319 y; 00320 00321 MagickBooleanType 00322 status; 00323 00324 register IndexPacket 00325 *indexes; 00326 00327 register const PixelPacket 00328 *p; 00329 00330 register long 00331 i, 00332 x; 00333 00334 register unsigned char 00335 *q; 00336 00337 size_t 00338 packet_size; 00339 00340 unsigned char 00341 *colormap, 00342 *pixels; 00343 00344 unsigned long 00345 depth; 00346 00347 /* 00348 Open output image file. 00349 */ 00350 assert(image_info != (const ImageInfo *) NULL); 00351 assert(image_info->signature == MagickSignature); 00352 assert(image != (Image *) NULL); 00353 assert(image->signature == MagickSignature); 00354 if (image->debug != MagickFalse) 00355 (void) LogMagickEvent(TraceEvent,GetMagickModule(),image->filename); 00356 status=OpenBlob(image_info,image,WriteBinaryBlobMode,&image->exception); 00357 if (status == MagickFalse) 00358 return(status); 00359 (void) SetImageColorspace(image,RGBColorspace); 00360 /* 00361 Allocate colormap. 00362 */ 00363 if (IsPaletteImage(image,&image->exception) == MagickFalse) 00364 (void) SetImageType(image,PaletteType); 00365 depth=GetImageQuantumDepth(image,MagickTrue); 00366 packet_size=(size_t) (depth/8); 00367 pixels=(unsigned char *) AcquireMagickMemory(packet_size*image->columns); 00368 packet_size=(size_t) (image->colors > 256 ? 6 : 3); 00369 colormap=(unsigned char *) AcquireMagickMemory(packet_size*image->colors); 00370 if ((pixels == (unsigned char *) NULL) || 00371 (colormap == (unsigned char *) NULL)) 00372 ThrowWriterException(ResourceLimitError,"MemoryAllocationFailed"); 00373 /* 00374 Write colormap to file. 00375 */ 00376 q=colormap; 00377 if (image->depth <= 8) 00378 for (i=0; i < (long) image->colors; i++) 00379 { 00380 *q++=(unsigned char) image->colormap[i].red; 00381 *q++=(unsigned char) image->colormap[i].green; 00382 *q++=(unsigned char) image->colormap[i].blue; 00383 } 00384 else 00385 for (i=0; i < (long) image->colors; i++) 00386 { 00387 *q++=(unsigned char) (image->colormap[i].red >> 8); 00388 *q++=(unsigned char) image->colormap[i].red; 00389 *q++=(unsigned char) (image->colormap[i].green >> 8); 00390 *q++=(unsigned char) image->colormap[i].green; 00391 *q++=(unsigned char) (image->colormap[i].blue >> 8); 00392 *q++=(unsigned char) image->colormap[i].blue; 00393 } 00394 (void) WriteBlob(image,packet_size*image->colors,colormap); 00395 colormap=(unsigned char *) RelinquishMagickMemory(colormap); 00396 /* 00397 Write image pixels to file. 00398 */ 00399 for (y=0; y < (long) image->rows; y++) 00400 { 00401 p=AcquireImagePixels(image,0,y,image->columns,1,&image->exception); 00402 if (p == (const PixelPacket *) NULL) 00403 break; 00404 indexes=GetIndexes(image); 00405 q=pixels; 00406 for (x=0; x < (long) image->columns; x++) 00407 { 00408 if (image->colors > 256) 00409 *q++=(unsigned char) (indexes[x] >> 8); 00410 *q++=(unsigned char) indexes[x]; 00411 } 00412 (void) WriteBlob(image,(size_t) (q-pixels),pixels); 00413 } 00414 pixels=(unsigned char *) RelinquishMagickMemory(pixels); 00415 CloseBlob(image); 00416 return(status); 00417 }

Generated on Mon Oct 25 13:40:38 2004 for ImageMagick by doxygen 1.3.7
ImageMagick Copyright © 2004, ImageMagick Studio LLC