]> granicus.if.org Git - imagemagick/blob - coders/uil.c
(no commit message)
[imagemagick] / coders / uil.c
1 /*
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 %                                                                             %
20 %  Copyright 1999-2011 ImageMagick Studio LLC, a non-profit organization      %
21 %  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 \f
39 /*
40   Include declarations.
41 */
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/exception.h"
50 #include "MagickCore/exception-private.h"
51 #include "MagickCore/image-private.h"
52 #include "MagickCore/magick.h"
53 #include "MagickCore/memory_.h"
54 #include "MagickCore/monitor.h"
55 #include "MagickCore/monitor-private.h"
56 #include "MagickCore/pixel-accessor.h"
57 #include "MagickCore/quantum-private.h"
58 #include "MagickCore/static.h"
59 #include "MagickCore/string_.h"
60 #include "MagickCore/module.h"
61 #include "MagickCore/utility.h"
62 \f
63 /*
64   Forward declarations.
65 */
66 static MagickBooleanType
67   WriteUILImage(const ImageInfo *,Image *);
68 \f
69 /*
70 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
71 %                                                                             %
72 %                                                                             %
73 %                                                                             %
74 %   R e g i s t e r U I L I m a g e                                           %
75 %                                                                             %
76 %                                                                             %
77 %                                                                             %
78 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
79 %
80 %  RegisterUILImage() adds attributes for the UIL image format to
81 %  the list of supported formats.  The attributes include the image format
82 %  tag, a method to read and/or write the format, whether the format
83 %  supports the saving of more than one frame to the same file or blob,
84 %  whether the format supports native in-memory I/O, and a brief
85 %  description of the format.
86 %
87 %  The format of the RegisterUILImage method is:
88 %
89 %      size_t RegisterUILImage(void)
90 %
91 */
92 ModuleExport size_t RegisterUILImage(void)
93 {
94   MagickInfo
95     *entry;
96
97   entry=SetMagickInfo("UIL");
98   entry->encoder=(EncodeImageHandler *) WriteUILImage;
99   entry->adjoin=MagickFalse;
100   entry->description=ConstantString("X-Motif UIL table");
101   entry->module=ConstantString("UIL");
102   (void) RegisterMagickInfo(entry);
103   return(MagickImageCoderSignature);
104 }
105 \f
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 */
125 ModuleExport void UnregisterUILImage(void)
126 {
127   (void) UnregisterMagickInfo("UIL");
128 }
129 \f
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 %
146 %      MagickBooleanType WriteUILImage(const ImageInfo *image_info,Image *image)
147 %
148 %  A description of each parameter follows.
149 %
150 %    o image_info: the image info.
151 %
152 %    o image:  The image.
153 %
154 */
155 static MagickBooleanType WriteUILImage(const ImageInfo *image_info,Image *image)
156 {
157 #define MaxCixels  92
158
159   char
160     basename[MaxTextExtent],
161     buffer[MaxTextExtent],
162     name[MaxTextExtent],
163     *symbol;
164
165   ExceptionInfo
166     *exception;
167
168   int
169     j;
170
171   MagickBooleanType
172     status,
173     transparent;
174
175   PixelInfo
176     pixel;
177
178   MagickSizeType
179     number_pixels;
180
181   register const Quantum
182     *p;
183
184   register ssize_t
185     i,
186     x;
187
188   size_t
189     characters_per_pixel,
190     colors;
191
192   ssize_t
193     k,
194     y;
195
196   static const char
197     Cixel[MaxCixels+1] = " .XoO+@#$%&*=-;:>,<1234567890qwertyuipasdfghjk"
198                          "lzxcvbnmMNBVCZASDFGHJKLPIUYTREWQ!~^/()_`'][{}|";
199
200   /*
201     Open output image file.
202   */
203   assert(image_info != (const ImageInfo *) NULL);
204   assert(image_info->signature == MagickSignature);
205   assert(image != (Image *) NULL);
206   assert(image->signature == MagickSignature);
207   if (image->debug != MagickFalse)
208     (void) LogMagickEvent(TraceEvent,GetMagickModule(),"%s",image->filename);
209   status=OpenBlob(image_info,image,WriteBinaryBlobMode,&image->exception);
210   if (status == MagickFalse)
211     return(status);
212   if (image->colorspace != RGBColorspace)
213     (void) TransformImageColorspace(image,RGBColorspace);
214   exception=(&image->exception);
215   transparent=MagickFalse;
216   i=0;
217   p=(const Quantum *) NULL;
218   if (image->storage_class == PseudoClass)
219     colors=image->colors;
220   else
221     {
222       unsigned char
223         *matte_image;
224
225       /*
226         Convert DirectClass to PseudoClass image.
227       */
228       matte_image=(unsigned char *) NULL;
229       if (image->matte != MagickFalse)
230         {
231           /*
232             Map all the transparent pixels.
233           */
234           number_pixels=(MagickSizeType) image->columns*image->rows;
235           if (number_pixels != ((MagickSizeType) (size_t) number_pixels))
236             ThrowWriterException(ResourceLimitError,"MemoryAllocationFailed");
237           matte_image=(unsigned char *) AcquireQuantumMemory(image->columns,
238             image->rows*sizeof(*matte_image));
239           if (matte_image == (unsigned char *) NULL)
240             ThrowWriterException(ResourceLimitError,"MemoryAllocationFailed");
241           for (y=0; y < (ssize_t) image->rows; y++)
242           {
243             p=GetVirtualPixels(image,0,y,image->columns,1,exception);
244             if (p == (const Quantum *) NULL)
245               break;
246             for (x=0; x < (ssize_t) image->columns; x++)
247             {
248               matte_image[i]=(unsigned char) (GetPixelAlpha(image,p) ==
249                 (Quantum) TransparentAlpha ? 1 : 0);
250               if (matte_image[i] != 0)
251                 transparent=MagickTrue;
252               i++;
253               p+=GetPixelChannels(image);
254             }
255           }
256         }
257       (void) SetImageType(image,PaletteType);
258       colors=image->colors;
259       if (transparent != MagickFalse)
260         {
261           register Quantum
262             *q;
263
264           colors++;
265           for (y=0; y < (ssize_t) image->rows; y++)
266           {
267             q=GetAuthenticPixels(image,0,y,image->columns,1,exception);
268             if (q == (const Quantum *) NULL)
269               break;
270             for (x=0; x < (ssize_t) image->columns; x++)
271             {
272               if (matte_image[i] != 0)
273                 SetPixelIndex(image,image->colors,q);
274               p++;
275               q+=GetPixelChannels(image);
276             }
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;
286   for (k=MaxCixels; (ssize_t) colors > k; k*=MaxCixels)
287     characters_per_pixel++;
288   /*
289     UIL header.
290   */
291   symbol=AcquireString("");
292   (void) WriteBlobString(image,"/* UIL */\n");
293   GetPathComponent(image->filename,BasePath,basename);
294   (void) FormatLocaleString(buffer,MaxTextExtent,
295     "value\n  %s_ct : color_table(\n",basename);
296   (void) WriteBlobString(image,buffer);
297   GetPixelInfo(image,&pixel);
298   for (i=0; i < (ssize_t) colors; i++)
299   {
300     /*
301       Define UIL color.
302     */
303     SetPixelInfoPacket(image,image->colormap+i,&pixel);
304     pixel.colorspace=RGBColorspace;
305     pixel.depth=8;
306     pixel.alpha=(MagickRealType) OpaqueAlpha;
307     GetColorTuple(&pixel,MagickTrue,name);
308     if (transparent != MagickFalse)
309       if (i == (ssize_t) (colors-1))
310         (void) CopyMagickString(name,"None",MaxTextExtent);
311     /*
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';
322     (void) SubstituteString(&symbol,"'","''");
323     if (LocaleCompare(name,"None") == 0)
324       (void) FormatLocaleString(buffer,MaxTextExtent,
325         "    background color = '%s'",symbol);
326     else
327       (void) FormatLocaleString(buffer,MaxTextExtent,
328         "    color('%s',%s) = '%s'",name,
329         GetPixelPacketIntensity(image->colormap+i) <
330         ((Quantum) QuantumRange/2) ? "background" : "foreground",symbol);
331     (void) WriteBlobString(image,buffer);
332     (void) FormatLocaleString(buffer,MaxTextExtent,"%s",
333       (i == (ssize_t) (colors-1) ? ");\n" : ",\n"));
334     (void) WriteBlobString(image,buffer);
335   }
336   /*
337     Define UIL pixels.
338   */
339   GetPathComponent(image->filename,BasePath,basename);
340   (void) FormatLocaleString(buffer,MaxTextExtent,
341     "  %s_icon : icon(color_table = %s_ct,\n",basename,basename);
342   (void) WriteBlobString(image,buffer);
343   for (y=0; y < (ssize_t) image->rows; y++)
344   {
345     p=GetVirtualPixels(image,0,y,image->columns,1,&image->exception);
346     if (p == (const Quantum *) NULL)
347       break;
348     (void) WriteBlobString(image,"    \"");
349     for (x=0; x < (ssize_t) image->columns; x++)
350     {
351       k=((ssize_t) GetPixelIndex(image,p) % MaxCixels);
352       symbol[0]=Cixel[k];
353       for (j=1; j < (int) characters_per_pixel; j++)
354       {
355         k=(((int) GetPixelIndex(image,p)-k)/MaxCixels) %
356           MaxCixels;
357         symbol[j]=Cixel[k];
358       }
359       symbol[j]='\0';
360       (void) CopyMagickString(buffer,symbol,MaxTextExtent);
361       (void) WriteBlobString(image,buffer);
362       p+=GetPixelChannels(image);
363     }
364     (void) FormatLocaleString(buffer,MaxTextExtent,"\"%s\n",
365       (y == (ssize_t) (image->rows-1) ? ");" : ","));
366     (void) WriteBlobString(image,buffer);
367     status=SetImageProgress(image,SaveImageTag,(MagickOffsetType) y,
368       image->rows);
369     if (status == MagickFalse)
370       break;
371   }
372   symbol=DestroyString(symbol);
373   (void) CloseBlob(image);
374   return(MagickTrue);
375 }