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/attribute.h"
44 #include "MagickCore/blob.h"
45 #include "MagickCore/blob-private.h"
46 #include "MagickCore/cache.h"
47 #include "MagickCore/color.h"
48 #include "MagickCore/color-private.h"
49 #include "MagickCore/colorspace.h"
50 #include "MagickCore/colorspace-private.h"
51 #include "MagickCore/exception.h"
52 #include "MagickCore/exception-private.h"
53 #include "MagickCore/image-private.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/quantum-private.h"
60 #include "MagickCore/static.h"
61 #include "MagickCore/string_.h"
62 #include "MagickCore/module.h"
63 #include "MagickCore/utility.h"
68 static MagickBooleanType
69 WriteUILImage(const ImageInfo *,Image *,ExceptionInfo *);
72 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
76 % R e g i s t e r U I L I m a g e %
80 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
82 % RegisterUILImage() adds attributes for the UIL image format to
83 % the list of supported formats. The attributes include the image format
84 % tag, a method to read and/or write the format, whether the format
85 % supports the saving of more than one frame to the same file or blob,
86 % whether the format supports native in-memory I/O, and a brief
87 % description of the format.
89 % The format of the RegisterUILImage method is:
91 % size_t RegisterUILImage(void)
94 ModuleExport size_t RegisterUILImage(void)
99 entry=SetMagickInfo("UIL");
100 entry->encoder=(EncodeImageHandler *) WriteUILImage;
101 entry->adjoin=MagickFalse;
102 entry->description=ConstantString("X-Motif UIL table");
103 entry->module=ConstantString("UIL");
104 (void) RegisterMagickInfo(entry);
105 return(MagickImageCoderSignature);
109 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
113 % U n r e g i s t e r U I L I m a g e %
117 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
119 % UnregisterUILImage() removes format registrations made by the
120 % UIL module from the list of supported formats.
122 % The format of the UnregisterUILImage method is:
124 % UnregisterUILImage(void)
127 ModuleExport void UnregisterUILImage(void)
129 (void) UnregisterMagickInfo("UIL");
133 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
137 % W r i t e U I L I m a g e %
141 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
143 % Procedure WriteUILImage() writes an image to a file in the X-Motif UIL table
146 % The format of the WriteUILImage method is:
148 % MagickBooleanType WriteUILImage(const ImageInfo *image_info,
149 % Image *image,ExceptionInfo *exception)
151 % A description of each parameter follows.
153 % o image_info: the image info.
155 % o image: The image.
157 % o exception: return any errors or warnings in this structure.
160 static MagickBooleanType WriteUILImage(const ImageInfo *image_info,Image *image,
161 ExceptionInfo *exception)
166 basename[MaxTextExtent],
167 buffer[MaxTextExtent],
184 register const Quantum
192 characters_per_pixel,
200 Cixel[MaxCixels+1] = " .XoO+@#$%&*=-;:>,<1234567890qwertyuipasdfghjk"
201 "lzxcvbnmMNBVCZASDFGHJKLPIUYTREWQ!~^/()_`'][{}|";
204 Open output image file.
206 assert(image_info != (const ImageInfo *) NULL);
207 assert(image_info->signature == MagickSignature);
208 assert(image != (Image *) NULL);
209 assert(image->signature == MagickSignature);
210 if (image->debug != MagickFalse)
211 (void) LogMagickEvent(TraceEvent,GetMagickModule(),"%s",image->filename);
212 assert(exception != (ExceptionInfo *) NULL);
213 assert(exception->signature == MagickSignature);
214 status=OpenBlob(image_info,image,WriteBinaryBlobMode,exception);
215 if (status == MagickFalse)
217 if (IssRGBColorspace(image->colorspace) == MagickFalse)
218 (void) TransformImageColorspace(image,sRGBColorspace,exception);
219 transparent=MagickFalse;
221 p=(const Quantum *) NULL;
222 if (image->storage_class == PseudoClass)
223 colors=image->colors;
230 Convert DirectClass to PseudoClass image.
232 matte_image=(unsigned char *) NULL;
233 if (image->matte != MagickFalse)
236 Map all the transparent pixels.
238 number_pixels=(MagickSizeType) image->columns*image->rows;
239 if (number_pixels != ((MagickSizeType) (size_t) number_pixels))
240 ThrowWriterException(ResourceLimitError,"MemoryAllocationFailed");
241 matte_image=(unsigned char *) AcquireQuantumMemory(image->columns,
242 image->rows*sizeof(*matte_image));
243 if (matte_image == (unsigned char *) NULL)
244 ThrowWriterException(ResourceLimitError,"MemoryAllocationFailed");
245 for (y=0; y < (ssize_t) image->rows; y++)
247 p=GetVirtualPixels(image,0,y,image->columns,1,exception);
248 if (p == (const Quantum *) NULL)
250 for (x=0; x < (ssize_t) image->columns; x++)
252 matte_image[i]=(unsigned char) (GetPixelAlpha(image,p) ==
253 (Quantum) TransparentAlpha ? 1 : 0);
254 if (matte_image[i] != 0)
255 transparent=MagickTrue;
257 p+=GetPixelChannels(image);
261 (void) SetImageType(image,PaletteType,exception);
262 colors=image->colors;
263 if (transparent != MagickFalse)
269 for (y=0; y < (ssize_t) image->rows; y++)
271 q=GetAuthenticPixels(image,0,y,image->columns,1,exception);
272 if (q == (Quantum *) NULL)
274 for (x=0; x < (ssize_t) image->columns; x++)
276 if (matte_image[i] != 0)
277 SetPixelIndex(image,image->colors,q);
279 q+=GetPixelChannels(image);
283 if (matte_image != (unsigned char *) NULL)
284 matte_image=(unsigned char *) RelinquishMagickMemory(matte_image);
287 Compute the character per pixel.
289 characters_per_pixel=1;
290 for (k=MaxCixels; (ssize_t) colors > k; k*=MaxCixels)
291 characters_per_pixel++;
295 symbol=AcquireString("");
296 (void) WriteBlobString(image,"/* UIL */\n");
297 GetPathComponent(image->filename,BasePath,basename);
298 (void) FormatLocaleString(buffer,MaxTextExtent,
299 "value\n %s_ct : color_table(\n",basename);
300 (void) WriteBlobString(image,buffer);
301 GetPixelInfo(image,&pixel);
302 for (i=0; i < (ssize_t) colors; i++)
307 pixel=image->colormap[i];
308 pixel.colorspace=sRGBColorspace;
310 pixel.alpha=(MagickRealType) OpaqueAlpha;
311 GetColorTuple(&pixel,MagickTrue,name);
312 if (transparent != MagickFalse)
313 if (i == (ssize_t) (colors-1))
314 (void) CopyMagickString(name,"None",MaxTextExtent);
320 for (j=1; j < (int) characters_per_pixel; j++)
322 k=((i-k)/MaxCixels) % MaxCixels;
326 (void) SubstituteString(&symbol,"'","''");
327 if (LocaleCompare(name,"None") == 0)
328 (void) FormatLocaleString(buffer,MaxTextExtent,
329 " background color = '%s'",symbol);
331 (void) FormatLocaleString(buffer,MaxTextExtent,
332 " color('%s',%s) = '%s'",name,
333 GetPixelInfoIntensity(image->colormap+i) <
334 (QuantumRange/2) ? "background" : "foreground",symbol);
335 (void) WriteBlobString(image,buffer);
336 (void) FormatLocaleString(buffer,MaxTextExtent,"%s",
337 (i == (ssize_t) (colors-1) ? ");\n" : ",\n"));
338 (void) WriteBlobString(image,buffer);
343 GetPathComponent(image->filename,BasePath,basename);
344 (void) FormatLocaleString(buffer,MaxTextExtent,
345 " %s_icon : icon(color_table = %s_ct,\n",basename,basename);
346 (void) WriteBlobString(image,buffer);
347 for (y=0; y < (ssize_t) image->rows; y++)
349 p=GetVirtualPixels(image,0,y,image->columns,1,exception);
350 if (p == (const Quantum *) NULL)
352 (void) WriteBlobString(image," \"");
353 for (x=0; x < (ssize_t) image->columns; x++)
355 k=((ssize_t) GetPixelIndex(image,p) % MaxCixels);
357 for (j=1; j < (int) characters_per_pixel; j++)
359 k=(((int) GetPixelIndex(image,p)-k)/MaxCixels) %
364 (void) CopyMagickString(buffer,symbol,MaxTextExtent);
365 (void) WriteBlobString(image,buffer);
366 p+=GetPixelChannels(image);
368 (void) FormatLocaleString(buffer,MaxTextExtent,"\"%s\n",
369 (y == (ssize_t) (image->rows-1) ? ");" : ","));
370 (void) WriteBlobString(image,buffer);
371 status=SetImageProgress(image,SaveImageTag,(MagickOffsetType) y,
373 if (status == MagickFalse)
376 symbol=DestroyString(symbol);
377 (void) CloseBlob(image);