blob: 2d3fa83705d188820984138562ef30ef164c07ab [file] [log] [blame]
cristy3ed852e2009-09-05 21:47:34 +00001/*
2%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
3% %
4% %
5% %
6% U U IIIII L %
7% U U I L %
8% U U I L %
9% U U I L %
10% UUU IIIII LLLLL %
11% %
12% %
13% Write X-Motif UIL Table. %
14% %
15% Software Design %
16% John Cristy %
17% July 1992 %
18% %
19% %
cristy1454be72011-12-19 01:52:48 +000020% Copyright 1999-2012 ImageMagick Studio LLC, a non-profit organization %
cristy3ed852e2009-09-05 21:47:34 +000021% dedicated to making software imaging solutions freely available. %
22% %
23% You may not use this file except in compliance with the License. You may %
24% obtain a copy of the License at %
25% %
26% http://www.imagemagick.org/script/license.php %
27% %
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. %
33% %
34%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
35%
36%
37*/
38
39/*
40 Include declarations.
41*/
cristy4c08aed2011-07-01 19:47:50 +000042#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"
cristy510d06a2011-07-06 23:43:54 +000049#include "MagickCore/colorspace-private.h"
cristy4c08aed2011-07-01 19:47:50 +000050#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"
cristy3ed852e2009-09-05 21:47:34 +000063
64/*
65 Forward declarations.
66*/
67static MagickBooleanType
cristy3a37efd2011-08-28 20:31:03 +000068 WriteUILImage(const ImageInfo *,Image *,ExceptionInfo *);
cristy3ed852e2009-09-05 21:47:34 +000069
70/*
71%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
72% %
73% %
74% %
75% R e g i s t e r U I L I m a g e %
76% %
77% %
78% %
79%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
80%
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.
87%
88% The format of the RegisterUILImage method is:
89%
cristybb503372010-05-27 20:51:26 +000090% size_t RegisterUILImage(void)
cristy3ed852e2009-09-05 21:47:34 +000091%
92*/
cristybb503372010-05-27 20:51:26 +000093ModuleExport size_t RegisterUILImage(void)
cristy3ed852e2009-09-05 21:47:34 +000094{
95 MagickInfo
96 *entry;
97
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);
105}
106
107/*
108%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
109% %
110% %
111% %
112% U n r e g i s t e r U I L I m a g e %
113% %
114% %
115% %
116%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
117%
118% UnregisterUILImage() removes format registrations made by the
119% UIL module from the list of supported formats.
120%
121% The format of the UnregisterUILImage method is:
122%
123% UnregisterUILImage(void)
124%
125*/
126ModuleExport void UnregisterUILImage(void)
127{
128 (void) UnregisterMagickInfo("UIL");
129}
130
131/*
132%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
133% %
134% %
135% %
136% W r i t e U I L I m a g e %
137% %
138% %
139% %
140%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
141%
142% Procedure WriteUILImage() writes an image to a file in the X-Motif UIL table
143% format.
144%
145% The format of the WriteUILImage method is:
146%
cristy3a37efd2011-08-28 20:31:03 +0000147% MagickBooleanType WriteUILImage(const ImageInfo *image_info,
148% Image *image,ExceptionInfo *exception)
cristy3ed852e2009-09-05 21:47:34 +0000149%
150% A description of each parameter follows.
151%
152% o image_info: the image info.
153%
154% o image: The image.
155%
cristy3a37efd2011-08-28 20:31:03 +0000156% o exception: return any errors or warnings in this structure.
157%
cristy3ed852e2009-09-05 21:47:34 +0000158*/
cristy3a37efd2011-08-28 20:31:03 +0000159static MagickBooleanType WriteUILImage(const ImageInfo *image_info,Image *image,
160 ExceptionInfo *exception)
cristy3ed852e2009-09-05 21:47:34 +0000161{
162#define MaxCixels 92
163
164 char
165 basename[MaxTextExtent],
166 buffer[MaxTextExtent],
167 name[MaxTextExtent],
cristybd899c22009-11-26 23:51:49 +0000168 *symbol;
cristy3ed852e2009-09-05 21:47:34 +0000169
cristy3ed852e2009-09-05 21:47:34 +0000170 int
171 j;
172
cristy3ed852e2009-09-05 21:47:34 +0000173 MagickBooleanType
174 status,
175 transparent;
176
cristy3ed852e2009-09-05 21:47:34 +0000177 MagickSizeType
178 number_pixels;
179
cristy9d8c8ce2011-10-25 16:13:52 +0000180 PixelInfo
181 pixel;
182
cristy4c08aed2011-07-01 19:47:50 +0000183 register const Quantum
cristy3ed852e2009-09-05 21:47:34 +0000184 *p;
185
cristybb503372010-05-27 20:51:26 +0000186 register ssize_t
cristy3ed852e2009-09-05 21:47:34 +0000187 i,
188 x;
189
cristybb503372010-05-27 20:51:26 +0000190 size_t
cristy3ed852e2009-09-05 21:47:34 +0000191 characters_per_pixel,
192 colors;
193
cristyc6da28e2011-04-28 01:41:35 +0000194 ssize_t
195 k,
196 y;
197
198 static const char
199 Cixel[MaxCixels+1] = " .XoO+@#$%&*=-;:>,<1234567890qwertyuipasdfghjk"
200 "lzxcvbnmMNBVCZASDFGHJKLPIUYTREWQ!~^/()_`'][{}|";
201
cristy3ed852e2009-09-05 21:47:34 +0000202 /*
203 Open output image file.
204 */
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);
cristy3a37efd2011-08-28 20:31:03 +0000211 assert(exception != (ExceptionInfo *) NULL);
212 assert(exception->signature == MagickSignature);
213 status=OpenBlob(image_info,image,WriteBinaryBlobMode,exception);
cristy3ed852e2009-09-05 21:47:34 +0000214 if (status == MagickFalse)
215 return(status);
cristy510d06a2011-07-06 23:43:54 +0000216 if (IsRGBColorspace(image->colorspace) == MagickFalse)
cristye941a752011-10-15 01:52:48 +0000217 (void) TransformImageColorspace(image,RGBColorspace,exception);
cristy3ed852e2009-09-05 21:47:34 +0000218 transparent=MagickFalse;
219 i=0;
cristy4c08aed2011-07-01 19:47:50 +0000220 p=(const Quantum *) NULL;
cristy3ed852e2009-09-05 21:47:34 +0000221 if (image->storage_class == PseudoClass)
222 colors=image->colors;
223 else
224 {
225 unsigned char
226 *matte_image;
227
228 /*
229 Convert DirectClass to PseudoClass image.
230 */
231 matte_image=(unsigned char *) NULL;
232 if (image->matte != MagickFalse)
233 {
234 /*
235 Map all the transparent pixels.
236 */
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");
cristybb503372010-05-27 20:51:26 +0000244 for (y=0; y < (ssize_t) image->rows; y++)
cristy3ed852e2009-09-05 21:47:34 +0000245 {
246 p=GetVirtualPixels(image,0,y,image->columns,1,exception);
cristy4c08aed2011-07-01 19:47:50 +0000247 if (p == (const Quantum *) NULL)
cristy3ed852e2009-09-05 21:47:34 +0000248 break;
cristybb503372010-05-27 20:51:26 +0000249 for (x=0; x < (ssize_t) image->columns; x++)
cristy3ed852e2009-09-05 21:47:34 +0000250 {
cristy4c08aed2011-07-01 19:47:50 +0000251 matte_image[i]=(unsigned char) (GetPixelAlpha(image,p) ==
252 (Quantum) TransparentAlpha ? 1 : 0);
cristy3ed852e2009-09-05 21:47:34 +0000253 if (matte_image[i] != 0)
254 transparent=MagickTrue;
255 i++;
cristyed231572011-07-14 02:18:59 +0000256 p+=GetPixelChannels(image);
cristy3ed852e2009-09-05 21:47:34 +0000257 }
258 }
259 }
cristy018f07f2011-09-04 21:15:19 +0000260 (void) SetImageType(image,PaletteType,exception);
cristy3ed852e2009-09-05 21:47:34 +0000261 colors=image->colors;
262 if (transparent != MagickFalse)
263 {
cristy4c08aed2011-07-01 19:47:50 +0000264 register Quantum
cristy3ed852e2009-09-05 21:47:34 +0000265 *q;
266
267 colors++;
cristybb503372010-05-27 20:51:26 +0000268 for (y=0; y < (ssize_t) image->rows; y++)
cristy3ed852e2009-09-05 21:47:34 +0000269 {
270 q=GetAuthenticPixels(image,0,y,image->columns,1,exception);
cristyacd2ed22011-08-30 01:44:23 +0000271 if (q == (Quantum *) NULL)
cristy3ed852e2009-09-05 21:47:34 +0000272 break;
cristybb503372010-05-27 20:51:26 +0000273 for (x=0; x < (ssize_t) image->columns; x++)
cristy3ed852e2009-09-05 21:47:34 +0000274 {
275 if (matte_image[i] != 0)
cristy4c08aed2011-07-01 19:47:50 +0000276 SetPixelIndex(image,image->colors,q);
cristy3ed852e2009-09-05 21:47:34 +0000277 p++;
cristyed231572011-07-14 02:18:59 +0000278 q+=GetPixelChannels(image);
cristy3ed852e2009-09-05 21:47:34 +0000279 }
280 }
281 }
282 if (matte_image != (unsigned char *) NULL)
283 matte_image=(unsigned char *) RelinquishMagickMemory(matte_image);
284 }
285 /*
286 Compute the character per pixel.
287 */
288 characters_per_pixel=1;
cristybb503372010-05-27 20:51:26 +0000289 for (k=MaxCixels; (ssize_t) colors > k; k*=MaxCixels)
cristy3ed852e2009-09-05 21:47:34 +0000290 characters_per_pixel++;
291 /*
292 UIL header.
293 */
cristybd899c22009-11-26 23:51:49 +0000294 symbol=AcquireString("");
cristy3ed852e2009-09-05 21:47:34 +0000295 (void) WriteBlobString(image,"/* UIL */\n");
296 GetPathComponent(image->filename,BasePath,basename);
cristyb51dff52011-05-19 16:55:47 +0000297 (void) FormatLocaleString(buffer,MaxTextExtent,
cristy3ed852e2009-09-05 21:47:34 +0000298 "value\n %s_ct : color_table(\n",basename);
299 (void) WriteBlobString(image,buffer);
cristy4c08aed2011-07-01 19:47:50 +0000300 GetPixelInfo(image,&pixel);
cristybb503372010-05-27 20:51:26 +0000301 for (i=0; i < (ssize_t) colors; i++)
cristy3ed852e2009-09-05 21:47:34 +0000302 {
303 /*
304 Define UIL color.
305 */
cristy9d8c8ce2011-10-25 16:13:52 +0000306 pixel=image->colormap[i];
cristy3ed852e2009-09-05 21:47:34 +0000307 pixel.colorspace=RGBColorspace;
308 pixel.depth=8;
cristy4c08aed2011-07-01 19:47:50 +0000309 pixel.alpha=(MagickRealType) OpaqueAlpha;
cristy3ed852e2009-09-05 21:47:34 +0000310 GetColorTuple(&pixel,MagickTrue,name);
311 if (transparent != MagickFalse)
cristybb503372010-05-27 20:51:26 +0000312 if (i == (ssize_t) (colors-1))
cristy3ed852e2009-09-05 21:47:34 +0000313 (void) CopyMagickString(name,"None",MaxTextExtent);
314 /*
315 Write UIL color.
316 */
317 k=i % MaxCixels;
318 symbol[0]=Cixel[k];
319 for (j=1; j < (int) characters_per_pixel; j++)
320 {
321 k=((i-k)/MaxCixels) % MaxCixels;
322 symbol[j]=Cixel[k];
323 }
324 symbol[j]='\0';
cristybd899c22009-11-26 23:51:49 +0000325 (void) SubstituteString(&symbol,"'","''");
cristy3ed852e2009-09-05 21:47:34 +0000326 if (LocaleCompare(name,"None") == 0)
cristyb51dff52011-05-19 16:55:47 +0000327 (void) FormatLocaleString(buffer,MaxTextExtent,
cristy3ed852e2009-09-05 21:47:34 +0000328 " background color = '%s'",symbol);
329 else
cristyb51dff52011-05-19 16:55:47 +0000330 (void) FormatLocaleString(buffer,MaxTextExtent,
cristy3ed852e2009-09-05 21:47:34 +0000331 " color('%s',%s) = '%s'",name,
cristy101ab702011-10-13 13:06:32 +0000332 GetPixelInfoIntensity(image->colormap+i) <
cristy3ed852e2009-09-05 21:47:34 +0000333 ((Quantum) QuantumRange/2) ? "background" : "foreground",symbol);
334 (void) WriteBlobString(image,buffer);
cristyb51dff52011-05-19 16:55:47 +0000335 (void) FormatLocaleString(buffer,MaxTextExtent,"%s",
cristybb503372010-05-27 20:51:26 +0000336 (i == (ssize_t) (colors-1) ? ");\n" : ",\n"));
cristy3ed852e2009-09-05 21:47:34 +0000337 (void) WriteBlobString(image,buffer);
338 }
339 /*
340 Define UIL pixels.
341 */
342 GetPathComponent(image->filename,BasePath,basename);
cristyb51dff52011-05-19 16:55:47 +0000343 (void) FormatLocaleString(buffer,MaxTextExtent,
cristy3ed852e2009-09-05 21:47:34 +0000344 " %s_icon : icon(color_table = %s_ct,\n",basename,basename);
345 (void) WriteBlobString(image,buffer);
cristybb503372010-05-27 20:51:26 +0000346 for (y=0; y < (ssize_t) image->rows; y++)
cristy3ed852e2009-09-05 21:47:34 +0000347 {
cristy3a37efd2011-08-28 20:31:03 +0000348 p=GetVirtualPixels(image,0,y,image->columns,1,exception);
cristy4c08aed2011-07-01 19:47:50 +0000349 if (p == (const Quantum *) NULL)
cristy3ed852e2009-09-05 21:47:34 +0000350 break;
cristy3ed852e2009-09-05 21:47:34 +0000351 (void) WriteBlobString(image," \"");
cristybb503372010-05-27 20:51:26 +0000352 for (x=0; x < (ssize_t) image->columns; x++)
cristy3ed852e2009-09-05 21:47:34 +0000353 {
cristy4c08aed2011-07-01 19:47:50 +0000354 k=((ssize_t) GetPixelIndex(image,p) % MaxCixels);
cristy3ed852e2009-09-05 21:47:34 +0000355 symbol[0]=Cixel[k];
356 for (j=1; j < (int) characters_per_pixel; j++)
357 {
cristy4c08aed2011-07-01 19:47:50 +0000358 k=(((int) GetPixelIndex(image,p)-k)/MaxCixels) %
359 MaxCixels;
cristy3ed852e2009-09-05 21:47:34 +0000360 symbol[j]=Cixel[k];
361 }
362 symbol[j]='\0';
363 (void) CopyMagickString(buffer,symbol,MaxTextExtent);
364 (void) WriteBlobString(image,buffer);
cristyed231572011-07-14 02:18:59 +0000365 p+=GetPixelChannels(image);
cristy3ed852e2009-09-05 21:47:34 +0000366 }
cristyb51dff52011-05-19 16:55:47 +0000367 (void) FormatLocaleString(buffer,MaxTextExtent,"\"%s\n",
cristybb503372010-05-27 20:51:26 +0000368 (y == (ssize_t) (image->rows-1) ? ");" : ","));
cristy3ed852e2009-09-05 21:47:34 +0000369 (void) WriteBlobString(image,buffer);
cristycee97112010-05-28 00:44:52 +0000370 status=SetImageProgress(image,SaveImageTag,(MagickOffsetType) y,
cristyc6da28e2011-04-28 01:41:35 +0000371 image->rows);
cristy3ed852e2009-09-05 21:47:34 +0000372 if (status == MagickFalse)
373 break;
374 }
cristybd899c22009-11-26 23:51:49 +0000375 symbol=DestroyString(symbol);
cristy3ed852e2009-09-05 21:47:34 +0000376 (void) CloseBlob(image);
377 return(MagickTrue);
378}