blob: d705eb7e337b7eacc290b218c712505b8defc2e3 [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 %
cristyde984cd2013-12-01 14:49:27 +000016% Cristy %
cristy3ed852e2009-09-05 21:47:34 +000017% July 1992 %
18% %
19% %
Cristy7ce65e72015-12-12 18:03:16 -050020% Copyright 1999-2016 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"
cristy8941c702012-06-21 01:30:15 +000043#include "MagickCore/attribute.h"
cristy4c08aed2011-07-01 19:47:50 +000044#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"
cristy510d06a2011-07-06 23:43:54 +000050#include "MagickCore/colorspace-private.h"
cristy4c08aed2011-07-01 19:47:50 +000051#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"
cristy3ed852e2009-09-05 21:47:34 +000064
65/*
66 Forward declarations.
67*/
68static MagickBooleanType
cristy3a37efd2011-08-28 20:31:03 +000069 WriteUILImage(const ImageInfo *,Image *,ExceptionInfo *);
cristy3ed852e2009-09-05 21:47:34 +000070
71/*
72%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
73% %
74% %
75% %
76% R e g i s t e r U I L I m a g e %
77% %
78% %
79% %
80%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
81%
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.
88%
89% The format of the RegisterUILImage method is:
90%
cristybb503372010-05-27 20:51:26 +000091% size_t RegisterUILImage(void)
cristy3ed852e2009-09-05 21:47:34 +000092%
93*/
cristybb503372010-05-27 20:51:26 +000094ModuleExport size_t RegisterUILImage(void)
cristy3ed852e2009-09-05 21:47:34 +000095{
96 MagickInfo
97 *entry;
98
dirk06b627a2015-04-06 18:59:17 +000099 entry=AcquireMagickInfo("UIL","UIL","X-Motif UIL table");
cristy3ed852e2009-09-05 21:47:34 +0000100 entry->encoder=(EncodeImageHandler *) WriteUILImage;
dirk08e9a112015-02-22 01:51:41 +0000101 entry->flags^=CoderAdjoinFlag;
cristy3ed852e2009-09-05 21:47:34 +0000102 (void) RegisterMagickInfo(entry);
103 return(MagickImageCoderSignature);
104}
105
106/*
107%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
108% %
109% %
110% %
111% U n r e g i s t e r U I L I m a g e %
112% %
113% %
114% %
115%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
116%
117% UnregisterUILImage() removes format registrations made by the
118% UIL module from the list of supported formats.
119%
120% The format of the UnregisterUILImage method is:
121%
122% UnregisterUILImage(void)
123%
124*/
125ModuleExport void UnregisterUILImage(void)
126{
127 (void) UnregisterMagickInfo("UIL");
128}
129
130/*
131%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
132% %
133% %
134% %
135% W r i t e U I L I m a g e %
136% %
137% %
138% %
139%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
140%
141% Procedure WriteUILImage() writes an image to a file in the X-Motif UIL table
142% format.
143%
144% The format of the WriteUILImage method is:
145%
cristy3a37efd2011-08-28 20:31:03 +0000146% MagickBooleanType WriteUILImage(const ImageInfo *image_info,
147% Image *image,ExceptionInfo *exception)
cristy3ed852e2009-09-05 21:47:34 +0000148%
149% A description of each parameter follows.
150%
151% o image_info: the image info.
152%
153% o image: The image.
154%
cristy3a37efd2011-08-28 20:31:03 +0000155% o exception: return any errors or warnings in this structure.
156%
cristy3ed852e2009-09-05 21:47:34 +0000157*/
cristy3a37efd2011-08-28 20:31:03 +0000158static MagickBooleanType WriteUILImage(const ImageInfo *image_info,Image *image,
159 ExceptionInfo *exception)
cristy3ed852e2009-09-05 21:47:34 +0000160{
161#define MaxCixels 92
162
163 char
cristy151b66d2015-04-15 10:50:31 +0000164 basename[MagickPathExtent],
165 buffer[MagickPathExtent],
166 name[MagickPathExtent],
cristybd899c22009-11-26 23:51:49 +0000167 *symbol;
cristy3ed852e2009-09-05 21:47:34 +0000168
cristy3ed852e2009-09-05 21:47:34 +0000169 int
170 j;
171
cristy3ed852e2009-09-05 21:47:34 +0000172 MagickBooleanType
173 status,
174 transparent;
175
cristy3ed852e2009-09-05 21:47:34 +0000176 MagickSizeType
177 number_pixels;
178
cristy9d8c8ce2011-10-25 16:13:52 +0000179 PixelInfo
180 pixel;
181
cristy4c08aed2011-07-01 19:47:50 +0000182 register const Quantum
cristy3ed852e2009-09-05 21:47:34 +0000183 *p;
184
cristybb503372010-05-27 20:51:26 +0000185 register ssize_t
cristy3ed852e2009-09-05 21:47:34 +0000186 i,
187 x;
188
cristybb503372010-05-27 20:51:26 +0000189 size_t
cristy3ed852e2009-09-05 21:47:34 +0000190 characters_per_pixel,
191 colors;
192
cristyc6da28e2011-04-28 01:41:35 +0000193 ssize_t
194 k,
195 y;
196
197 static const char
198 Cixel[MaxCixels+1] = " .XoO+@#$%&*=-;:>,<1234567890qwertyuipasdfghjk"
199 "lzxcvbnmMNBVCZASDFGHJKLPIUYTREWQ!~^/()_`'][{}|";
200
cristy3ed852e2009-09-05 21:47:34 +0000201 /*
202 Open output image file.
203 */
204 assert(image_info != (const ImageInfo *) NULL);
cristye1c94d92015-06-28 12:16:33 +0000205 assert(image_info->signature == MagickCoreSignature);
cristy3ed852e2009-09-05 21:47:34 +0000206 assert(image != (Image *) NULL);
cristye1c94d92015-06-28 12:16:33 +0000207 assert(image->signature == MagickCoreSignature);
cristy3ed852e2009-09-05 21:47:34 +0000208 if (image->debug != MagickFalse)
209 (void) LogMagickEvent(TraceEvent,GetMagickModule(),"%s",image->filename);
cristy3a37efd2011-08-28 20:31:03 +0000210 assert(exception != (ExceptionInfo *) NULL);
cristye1c94d92015-06-28 12:16:33 +0000211 assert(exception->signature == MagickCoreSignature);
cristy3a37efd2011-08-28 20:31:03 +0000212 status=OpenBlob(image_info,image,WriteBinaryBlobMode,exception);
cristy3ed852e2009-09-05 21:47:34 +0000213 if (status == MagickFalse)
214 return(status);
cristyaf8d3912014-02-21 14:50:33 +0000215 (void) TransformImageColorspace(image,sRGBColorspace,exception);
cristy3ed852e2009-09-05 21:47:34 +0000216 transparent=MagickFalse;
217 i=0;
cristy4c08aed2011-07-01 19:47:50 +0000218 p=(const Quantum *) NULL;
cristy3ed852e2009-09-05 21:47:34 +0000219 if (image->storage_class == PseudoClass)
220 colors=image->colors;
221 else
222 {
223 unsigned char
224 *matte_image;
225
226 /*
227 Convert DirectClass to PseudoClass image.
228 */
229 matte_image=(unsigned char *) NULL;
cristy17f11b02014-12-20 19:37:04 +0000230 if (image->alpha_trait != UndefinedPixelTrait)
cristy3ed852e2009-09-05 21:47:34 +0000231 {
232 /*
233 Map all the transparent pixels.
234 */
235 number_pixels=(MagickSizeType) image->columns*image->rows;
236 if (number_pixels != ((MagickSizeType) (size_t) number_pixels))
237 ThrowWriterException(ResourceLimitError,"MemoryAllocationFailed");
238 matte_image=(unsigned char *) AcquireQuantumMemory(image->columns,
239 image->rows*sizeof(*matte_image));
240 if (matte_image == (unsigned char *) NULL)
241 ThrowWriterException(ResourceLimitError,"MemoryAllocationFailed");
cristybb503372010-05-27 20:51:26 +0000242 for (y=0; y < (ssize_t) image->rows; y++)
cristy3ed852e2009-09-05 21:47:34 +0000243 {
244 p=GetVirtualPixels(image,0,y,image->columns,1,exception);
cristy4c08aed2011-07-01 19:47:50 +0000245 if (p == (const Quantum *) NULL)
cristy3ed852e2009-09-05 21:47:34 +0000246 break;
cristybb503372010-05-27 20:51:26 +0000247 for (x=0; x < (ssize_t) image->columns; x++)
cristy3ed852e2009-09-05 21:47:34 +0000248 {
cristy4c08aed2011-07-01 19:47:50 +0000249 matte_image[i]=(unsigned char) (GetPixelAlpha(image,p) ==
250 (Quantum) TransparentAlpha ? 1 : 0);
cristy3ed852e2009-09-05 21:47:34 +0000251 if (matte_image[i] != 0)
252 transparent=MagickTrue;
253 i++;
cristyed231572011-07-14 02:18:59 +0000254 p+=GetPixelChannels(image);
cristy3ed852e2009-09-05 21:47:34 +0000255 }
256 }
257 }
cristy018f07f2011-09-04 21:15:19 +0000258 (void) SetImageType(image,PaletteType,exception);
cristy3ed852e2009-09-05 21:47:34 +0000259 colors=image->colors;
260 if (transparent != MagickFalse)
261 {
cristy4c08aed2011-07-01 19:47:50 +0000262 register Quantum
cristy3ed852e2009-09-05 21:47:34 +0000263 *q;
264
265 colors++;
cristybb503372010-05-27 20:51:26 +0000266 for (y=0; y < (ssize_t) image->rows; y++)
cristy3ed852e2009-09-05 21:47:34 +0000267 {
268 q=GetAuthenticPixels(image,0,y,image->columns,1,exception);
cristyacd2ed22011-08-30 01:44:23 +0000269 if (q == (Quantum *) NULL)
cristy3ed852e2009-09-05 21:47:34 +0000270 break;
cristybb503372010-05-27 20:51:26 +0000271 for (x=0; x < (ssize_t) image->columns; x++)
cristy3ed852e2009-09-05 21:47:34 +0000272 {
273 if (matte_image[i] != 0)
dirk1678aec2015-08-30 12:07:29 +0200274 SetPixelIndex(image,(Quantum) image->colors,q);
cristyed231572011-07-14 02:18:59 +0000275 q+=GetPixelChannels(image);
cristy3ed852e2009-09-05 21:47:34 +0000276 }
277 }
278 }
279 if (matte_image != (unsigned char *) NULL)
280 matte_image=(unsigned char *) RelinquishMagickMemory(matte_image);
281 }
282 /*
283 Compute the character per pixel.
284 */
285 characters_per_pixel=1;
cristybb503372010-05-27 20:51:26 +0000286 for (k=MaxCixels; (ssize_t) colors > k; k*=MaxCixels)
cristy3ed852e2009-09-05 21:47:34 +0000287 characters_per_pixel++;
288 /*
289 UIL header.
290 */
cristybd899c22009-11-26 23:51:49 +0000291 symbol=AcquireString("");
cristy3ed852e2009-09-05 21:47:34 +0000292 (void) WriteBlobString(image,"/* UIL */\n");
293 GetPathComponent(image->filename,BasePath,basename);
cristy151b66d2015-04-15 10:50:31 +0000294 (void) FormatLocaleString(buffer,MagickPathExtent,
cristy3ed852e2009-09-05 21:47:34 +0000295 "value\n %s_ct : color_table(\n",basename);
296 (void) WriteBlobString(image,buffer);
cristy4c08aed2011-07-01 19:47:50 +0000297 GetPixelInfo(image,&pixel);
cristybb503372010-05-27 20:51:26 +0000298 for (i=0; i < (ssize_t) colors; i++)
cristy3ed852e2009-09-05 21:47:34 +0000299 {
300 /*
301 Define UIL color.
302 */
cristy9d8c8ce2011-10-25 16:13:52 +0000303 pixel=image->colormap[i];
cristy7020ae62012-04-18 12:58:34 +0000304 pixel.colorspace=sRGBColorspace;
cristy3ed852e2009-09-05 21:47:34 +0000305 pixel.depth=8;
cristya19f1d72012-08-07 18:24:38 +0000306 pixel.alpha=(double) OpaqueAlpha;
cristy3ed852e2009-09-05 21:47:34 +0000307 GetColorTuple(&pixel,MagickTrue,name);
308 if (transparent != MagickFalse)
cristybb503372010-05-27 20:51:26 +0000309 if (i == (ssize_t) (colors-1))
cristy151b66d2015-04-15 10:50:31 +0000310 (void) CopyMagickString(name,"None",MagickPathExtent);
cristy3ed852e2009-09-05 21:47:34 +0000311 /*
312 Write UIL color.
313 */
314 k=i % MaxCixels;
315 symbol[0]=Cixel[k];
316 for (j=1; j < (int) characters_per_pixel; j++)
317 {
318 k=((i-k)/MaxCixels) % MaxCixels;
319 symbol[j]=Cixel[k];
320 }
321 symbol[j]='\0';
cristybd899c22009-11-26 23:51:49 +0000322 (void) SubstituteString(&symbol,"'","''");
cristy3ed852e2009-09-05 21:47:34 +0000323 if (LocaleCompare(name,"None") == 0)
cristy151b66d2015-04-15 10:50:31 +0000324 (void) FormatLocaleString(buffer,MagickPathExtent,
cristy3ed852e2009-09-05 21:47:34 +0000325 " background color = '%s'",symbol);
326 else
cristy151b66d2015-04-15 10:50:31 +0000327 (void) FormatLocaleString(buffer,MagickPathExtent,
cristy3ed852e2009-09-05 21:47:34 +0000328 " color('%s',%s) = '%s'",name,
cristy11a06d32015-01-04 12:03:27 +0000329 GetPixelInfoIntensity(image,image->colormap+i) <
cristyc3306092015-02-28 22:57:28 +0000330 (QuantumRange/2.0) ? "background" : "foreground",symbol);
cristy3ed852e2009-09-05 21:47:34 +0000331 (void) WriteBlobString(image,buffer);
cristy151b66d2015-04-15 10:50:31 +0000332 (void) FormatLocaleString(buffer,MagickPathExtent,"%s",
cristybb503372010-05-27 20:51:26 +0000333 (i == (ssize_t) (colors-1) ? ");\n" : ",\n"));
cristy3ed852e2009-09-05 21:47:34 +0000334 (void) WriteBlobString(image,buffer);
335 }
336 /*
337 Define UIL pixels.
338 */
339 GetPathComponent(image->filename,BasePath,basename);
cristy151b66d2015-04-15 10:50:31 +0000340 (void) FormatLocaleString(buffer,MagickPathExtent,
cristy3ed852e2009-09-05 21:47:34 +0000341 " %s_icon : icon(color_table = %s_ct,\n",basename,basename);
342 (void) WriteBlobString(image,buffer);
cristybb503372010-05-27 20:51:26 +0000343 for (y=0; y < (ssize_t) image->rows; y++)
cristy3ed852e2009-09-05 21:47:34 +0000344 {
cristy3a37efd2011-08-28 20:31:03 +0000345 p=GetVirtualPixels(image,0,y,image->columns,1,exception);
cristy4c08aed2011-07-01 19:47:50 +0000346 if (p == (const Quantum *) NULL)
cristy3ed852e2009-09-05 21:47:34 +0000347 break;
cristy3ed852e2009-09-05 21:47:34 +0000348 (void) WriteBlobString(image," \"");
cristybb503372010-05-27 20:51:26 +0000349 for (x=0; x < (ssize_t) image->columns; x++)
cristy3ed852e2009-09-05 21:47:34 +0000350 {
cristy4c08aed2011-07-01 19:47:50 +0000351 k=((ssize_t) GetPixelIndex(image,p) % MaxCixels);
cristy3ed852e2009-09-05 21:47:34 +0000352 symbol[0]=Cixel[k];
353 for (j=1; j < (int) characters_per_pixel; j++)
354 {
cristy4c08aed2011-07-01 19:47:50 +0000355 k=(((int) GetPixelIndex(image,p)-k)/MaxCixels) %
356 MaxCixels;
cristy3ed852e2009-09-05 21:47:34 +0000357 symbol[j]=Cixel[k];
358 }
359 symbol[j]='\0';
cristy151b66d2015-04-15 10:50:31 +0000360 (void) CopyMagickString(buffer,symbol,MagickPathExtent);
cristy3ed852e2009-09-05 21:47:34 +0000361 (void) WriteBlobString(image,buffer);
cristyed231572011-07-14 02:18:59 +0000362 p+=GetPixelChannels(image);
cristy3ed852e2009-09-05 21:47:34 +0000363 }
cristy151b66d2015-04-15 10:50:31 +0000364 (void) FormatLocaleString(buffer,MagickPathExtent,"\"%s\n",
cristybb503372010-05-27 20:51:26 +0000365 (y == (ssize_t) (image->rows-1) ? ");" : ","));
cristy3ed852e2009-09-05 21:47:34 +0000366 (void) WriteBlobString(image,buffer);
cristycee97112010-05-28 00:44:52 +0000367 status=SetImageProgress(image,SaveImageTag,(MagickOffsetType) y,
cristyc6da28e2011-04-28 01:41:35 +0000368 image->rows);
cristy3ed852e2009-09-05 21:47:34 +0000369 if (status == MagickFalse)
370 break;
371 }
cristybd899c22009-11-26 23:51:49 +0000372 symbol=DestroyString(symbol);
cristy3ed852e2009-09-05 21:47:34 +0000373 (void) CloseBlob(image);
374 return(MagickTrue);
375}