2 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
13 % Write X-Motif UIL Table. %
20 % Copyright 1999-2012 ImageMagick Studio LLC, a non-profit organization %
21 % dedicated to making software imaging solutions freely available. %
23 % You may not use this file except in compliance with the License. You may %
24 % obtain a copy of the License at %
26 % http://www.imagemagick.org/script/license.php %
28 % Unless required by applicable law or agreed to in writing, software %
29 % distributed under the License is distributed on an "AS IS" BASIS, %
30 % WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. %
31 % See the License for the specific language governing permissions and %
32 % limitations under the License. %
34 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
42 #include "MagickCore/studio.h"
43 #include "MagickCore/blob.h"
44 #include "MagickCore/blob-private.h"
45 #include "MagickCore/cache.h"
46 #include "MagickCore/color.h"
47 #include "MagickCore/color-private.h"
48 #include "MagickCore/colorspace.h"
49 #include "MagickCore/colorspace-private.h"
50 #include "MagickCore/exception.h"
51 #include "MagickCore/exception-private.h"
52 #include "MagickCore/image-private.h"
53 #include "MagickCore/magick.h"
54 #include "MagickCore/memory_.h"
55 #include "MagickCore/monitor.h"
56 #include "MagickCore/monitor-private.h"
57 #include "MagickCore/pixel-accessor.h"
58 #include "MagickCore/quantum-private.h"
59 #include "MagickCore/static.h"
60 #include "MagickCore/string_.h"
61 #include "MagickCore/module.h"
62 #include "MagickCore/utility.h"
67 static MagickBooleanType
68 WriteUILImage(const ImageInfo *,Image *,ExceptionInfo *);
71 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
75 % R e g i s t e r U I L I m a g e %
79 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
81 % RegisterUILImage() adds attributes for the UIL image format to
82 % the list of supported formats. The attributes 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.
88 % The format of the RegisterUILImage method is:
90 % size_t RegisterUILImage(void)
93 ModuleExport size_t RegisterUILImage(void)
98 entry=SetMagickInfo("UIL");
99 entry->encoder=(EncodeImageHandler *) WriteUILImage;
100 entry->adjoin=MagickFalse;
101 entry->description=ConstantString("X-Motif UIL table");
102 entry->module=ConstantString("UIL");
103 (void) RegisterMagickInfo(entry);
104 return(MagickImageCoderSignature);
108 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
112 % U n r e g i s t e r U I L I m a g e %
116 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
118 % UnregisterUILImage() removes format registrations made by the
119 % UIL module from the list of supported formats.
121 % The format of the UnregisterUILImage method is:
123 % UnregisterUILImage(void)
126 ModuleExport void UnregisterUILImage(void)
128 (void) UnregisterMagickInfo("UIL");
132 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
136 % W r i t e U I L I m a g e %
140 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
142 % Procedure WriteUILImage() writes an image to a file in the X-Motif UIL table
145 % The format of the WriteUILImage method is:
147 % MagickBooleanType WriteUILImage(const ImageInfo *image_info,
148 % Image *image,ExceptionInfo *exception)
150 % A description of each parameter follows.
152 % o image_info: the image info.
154 % o image: The image.
156 % o exception: return any errors or warnings in this structure.
159 static MagickBooleanType WriteUILImage(const ImageInfo *image_info,Image *image,
160 ExceptionInfo *exception)
165 basename[MaxTextExtent],
166 buffer[MaxTextExtent],
183 register const Quantum
191 characters_per_pixel,
199 Cixel[MaxCixels+1] = " .XoO+@#$%&*=-;:>,<1234567890qwertyuipasdfghjk"
200 "lzxcvbnmMNBVCZASDFGHJKLPIUYTREWQ!~^/()_`'][{}|";
203 Open output image file.
205 assert(image_info != (const ImageInfo *) NULL);
206 assert(image_info->signature == MagickSignature);
207 assert(image != (Image *) NULL);
208 assert(image->signature == MagickSignature);
209 if (image->debug != MagickFalse)
210 (void) LogMagickEvent(TraceEvent,GetMagickModule(),"%s",image->filename);
211 assert(exception != (ExceptionInfo *) NULL);
212 assert(exception->signature == MagickSignature);
213 status=OpenBlob(image_info,image,WriteBinaryBlobMode,exception);
214 if (status == MagickFalse)
216 if (IsRGBColorspace(image->colorspace) == MagickFalse)
217 (void) TransformImageColorspace(image,RGBColorspace,exception);
218 transparent=MagickFalse;
220 p=(const Quantum *) NULL;
221 if (image->storage_class == PseudoClass)
222 colors=image->colors;
229 Convert DirectClass to PseudoClass image.
231 matte_image=(unsigned char *) NULL;
232 if (image->matte != MagickFalse)
235 Map all the transparent pixels.
237 number_pixels=(MagickSizeType) image->columns*image->rows;
238 if (number_pixels != ((MagickSizeType) (size_t) number_pixels))
239 ThrowWriterException(ResourceLimitError,"MemoryAllocationFailed");
240 matte_image=(unsigned char *) AcquireQuantumMemory(image->columns,
241 image->rows*sizeof(*matte_image));
242 if (matte_image == (unsigned char *) NULL)
243 ThrowWriterException(ResourceLimitError,"MemoryAllocationFailed");
244 for (y=0; y < (ssize_t) image->rows; y++)
246 p=GetVirtualPixels(image,0,y,image->columns,1,exception);
247 if (p == (const Quantum *) NULL)
249 for (x=0; x < (ssize_t) image->columns; x++)
251 matte_image[i]=(unsigned char) (GetPixelAlpha(image,p) ==
252 (Quantum) TransparentAlpha ? 1 : 0);
253 if (matte_image[i] != 0)
254 transparent=MagickTrue;
256 p+=GetPixelChannels(image);
260 (void) SetImageType(image,PaletteType,exception);
261 colors=image->colors;
262 if (transparent != MagickFalse)
268 for (y=0; y < (ssize_t) image->rows; y++)
270 q=GetAuthenticPixels(image,0,y,image->columns,1,exception);
271 if (q == (Quantum *) NULL)
273 for (x=0; x < (ssize_t) image->columns; x++)
275 if (matte_image[i] != 0)
276 SetPixelIndex(image,image->colors,q);
278 q+=GetPixelChannels(image);
282 if (matte_image != (unsigned char *) NULL)
283 matte_image=(unsigned char *) RelinquishMagickMemory(matte_image);
286 Compute the character per pixel.
288 characters_per_pixel=1;
289 for (k=MaxCixels; (ssize_t) colors > k; k*=MaxCixels)
290 characters_per_pixel++;
294 symbol=AcquireString("");
295 (void) WriteBlobString(image,"/* UIL */\n");
296 GetPathComponent(image->filename,BasePath,basename);
297 (void) FormatLocaleString(buffer,MaxTextExtent,
298 "value\n %s_ct : color_table(\n",basename);
299 (void) WriteBlobString(image,buffer);
300 GetPixelInfo(image,&pixel);
301 for (i=0; i < (ssize_t) colors; i++)
306 pixel=image->colormap[i];
307 pixel.colorspace=RGBColorspace;
309 pixel.alpha=(MagickRealType) OpaqueAlpha;
310 GetColorTuple(&pixel,MagickTrue,name);
311 if (transparent != MagickFalse)
312 if (i == (ssize_t) (colors-1))
313 (void) CopyMagickString(name,"None",MaxTextExtent);
319 for (j=1; j < (int) characters_per_pixel; j++)
321 k=((i-k)/MaxCixels) % MaxCixels;
325 (void) SubstituteString(&symbol,"'","''");
326 if (LocaleCompare(name,"None") == 0)
327 (void) FormatLocaleString(buffer,MaxTextExtent,
328 " background color = '%s'",symbol);
330 (void) FormatLocaleString(buffer,MaxTextExtent,
331 " color('%s',%s) = '%s'",name,
332 GetPixelInfoIntensity(image->colormap+i) <
333 ((Quantum) QuantumRange/2) ? "background" : "foreground",symbol);
334 (void) WriteBlobString(image,buffer);
335 (void) FormatLocaleString(buffer,MaxTextExtent,"%s",
336 (i == (ssize_t) (colors-1) ? ");\n" : ",\n"));
337 (void) WriteBlobString(image,buffer);
342 GetPathComponent(image->filename,BasePath,basename);
343 (void) FormatLocaleString(buffer,MaxTextExtent,
344 " %s_icon : icon(color_table = %s_ct,\n",basename,basename);
345 (void) WriteBlobString(image,buffer);
346 for (y=0; y < (ssize_t) image->rows; y++)
348 p=GetVirtualPixels(image,0,y,image->columns,1,exception);
349 if (p == (const Quantum *) NULL)
351 (void) WriteBlobString(image," \"");
352 for (x=0; x < (ssize_t) image->columns; x++)
354 k=((ssize_t) GetPixelIndex(image,p) % MaxCixels);
356 for (j=1; j < (int) characters_per_pixel; j++)
358 k=(((int) GetPixelIndex(image,p)-k)/MaxCixels) %
363 (void) CopyMagickString(buffer,symbol,MaxTextExtent);
364 (void) WriteBlobString(image,buffer);
365 p+=GetPixelChannels(image);
367 (void) FormatLocaleString(buffer,MaxTextExtent,"\"%s\n",
368 (y == (ssize_t) (image->rows-1) ? ");" : ","));
369 (void) WriteBlobString(image,buffer);
370 status=SetImageProgress(image,SaveImageTag,(MagickOffsetType) y,
372 if (status == MagickFalse)
375 symbol=DestroyString(symbol);
376 (void) CloseBlob(image);