]> granicus.if.org Git - imagemagick/blob - coders/uil.c
8496adee348bdea15b6ec41d4a43e51299c949d1
[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 %                                   Cristy                                    %
17 %                                 July 1992                                   %
18 %                                                                             %
19 %                                                                             %
20 %  Copyright 1999-2014 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/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"
64 \f
65 /*
66   Forward declarations.
67 */
68 static MagickBooleanType
69   WriteUILImage(const ImageInfo *,Image *,ExceptionInfo *);
70 \f
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 %
91 %      size_t RegisterUILImage(void)
92 %
93 */
94 ModuleExport size_t RegisterUILImage(void)
95 {
96   MagickInfo
97     *entry;
98
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);
106 }
107 \f
108 /*
109 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
110 %                                                                             %
111 %                                                                             %
112 %                                                                             %
113 %   U n r e g i s t e r U I L I m a g e                                       %
114 %                                                                             %
115 %                                                                             %
116 %                                                                             %
117 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
118 %
119 %  UnregisterUILImage() removes format registrations made by the
120 %  UIL module from the list of supported formats.
121 %
122 %  The format of the UnregisterUILImage method is:
123 %
124 %      UnregisterUILImage(void)
125 %
126 */
127 ModuleExport void UnregisterUILImage(void)
128 {
129   (void) UnregisterMagickInfo("UIL");
130 }
131 \f
132 /*
133 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
134 %                                                                             %
135 %                                                                             %
136 %                                                                             %
137 %   W r i t e U I L I m a g e                                                 %
138 %                                                                             %
139 %                                                                             %
140 %                                                                             %
141 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
142 %
143 %  Procedure WriteUILImage() writes an image to a file in the X-Motif UIL table
144 %  format.
145 %
146 %  The format of the WriteUILImage method is:
147 %
148 %      MagickBooleanType WriteUILImage(const ImageInfo *image_info,
149 %        Image *image,ExceptionInfo *exception)
150 %
151 %  A description of each parameter follows.
152 %
153 %    o image_info: the image info.
154 %
155 %    o image:  The image.
156 %
157 %    o exception: return any errors or warnings in this structure.
158 %
159 */
160 static MagickBooleanType WriteUILImage(const ImageInfo *image_info,Image *image,
161   ExceptionInfo *exception)
162 {
163 #define MaxCixels  92
164
165   char
166     basename[MaxTextExtent],
167     buffer[MaxTextExtent],
168     name[MaxTextExtent],
169     *symbol;
170
171   int
172     j;
173
174   MagickBooleanType
175     status,
176     transparent;
177
178   MagickSizeType
179     number_pixels;
180
181   PixelInfo
182     pixel;
183
184   register const Quantum
185     *p;
186
187   register ssize_t
188     i,
189     x;
190
191   size_t
192     characters_per_pixel,
193     colors;
194
195   ssize_t
196     k,
197     y;
198
199   static const char
200     Cixel[MaxCixels+1] = " .XoO+@#$%&*=-;:>,<1234567890qwertyuipasdfghjk"
201                          "lzxcvbnmMNBVCZASDFGHJKLPIUYTREWQ!~^/()_`'][{}|";
202
203   /*
204     Open output image file.
205   */
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)
216     return(status);
217   (void) TransformImageColorspace(image,sRGBColorspace,exception);
218   transparent=MagickFalse;
219   i=0;
220   p=(const Quantum *) NULL;
221   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->alpha_trait == BlendPixelTrait)
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");
244           for (y=0; y < (ssize_t) image->rows; y++)
245           {
246             p=GetVirtualPixels(image,0,y,image->columns,1,exception);
247             if (p == (const Quantum *) NULL)
248               break;
249             for (x=0; x < (ssize_t) image->columns; x++)
250             {
251               matte_image[i]=(unsigned char) (GetPixelAlpha(image,p) ==
252                 (Quantum) TransparentAlpha ? 1 : 0);
253               if (matte_image[i] != 0)
254                 transparent=MagickTrue;
255               i++;
256               p+=GetPixelChannels(image);
257             }
258           }
259         }
260       (void) SetImageType(image,PaletteType,exception);
261       colors=image->colors;
262       if (transparent != MagickFalse)
263         {
264           register Quantum
265             *q;
266
267           colors++;
268           for (y=0; y < (ssize_t) image->rows; y++)
269           {
270             q=GetAuthenticPixels(image,0,y,image->columns,1,exception);
271             if (q == (Quantum *) NULL)
272               break;
273             for (x=0; x < (ssize_t) image->columns; x++)
274             {
275               if (matte_image[i] != 0)
276                 SetPixelIndex(image,image->colors,q);
277               q+=GetPixelChannels(image);
278             }
279           }
280         }
281       if (matte_image != (unsigned char *) NULL)
282         matte_image=(unsigned char *) RelinquishMagickMemory(matte_image);
283     }
284   /*
285     Compute the character per pixel.
286   */
287   characters_per_pixel=1;
288   for (k=MaxCixels; (ssize_t) colors > k; k*=MaxCixels)
289     characters_per_pixel++;
290   /*
291     UIL header.
292   */
293   symbol=AcquireString("");
294   (void) WriteBlobString(image,"/* UIL */\n");
295   GetPathComponent(image->filename,BasePath,basename);
296   (void) FormatLocaleString(buffer,MaxTextExtent,
297     "value\n  %s_ct : color_table(\n",basename);
298   (void) WriteBlobString(image,buffer);
299   GetPixelInfo(image,&pixel);
300   for (i=0; i < (ssize_t) colors; i++)
301   {
302     /*
303       Define UIL color.
304     */
305     pixel=image->colormap[i];
306     pixel.colorspace=sRGBColorspace;
307     pixel.depth=8;
308     pixel.alpha=(double) OpaqueAlpha;
309     GetColorTuple(&pixel,MagickTrue,name);
310     if (transparent != MagickFalse)
311       if (i == (ssize_t) (colors-1))
312         (void) CopyMagickString(name,"None",MaxTextExtent);
313     /*
314       Write UIL color.
315     */
316     k=i % MaxCixels;
317     symbol[0]=Cixel[k];
318     for (j=1; j < (int) characters_per_pixel; j++)
319     {
320       k=((i-k)/MaxCixels) % MaxCixels;
321       symbol[j]=Cixel[k];
322     }
323     symbol[j]='\0';
324     (void) SubstituteString(&symbol,"'","''");
325     if (LocaleCompare(name,"None") == 0)
326       (void) FormatLocaleString(buffer,MaxTextExtent,
327         "    background color = '%s'",symbol);
328     else
329       (void) FormatLocaleString(buffer,MaxTextExtent,
330         "    color('%s',%s) = '%s'",name,
331         GetPixelInfoIntensity(image->colormap+i) <
332         (QuantumRange/2) ? "background" : "foreground",symbol);
333     (void) WriteBlobString(image,buffer);
334     (void) FormatLocaleString(buffer,MaxTextExtent,"%s",
335       (i == (ssize_t) (colors-1) ? ");\n" : ",\n"));
336     (void) WriteBlobString(image,buffer);
337   }
338   /*
339     Define UIL pixels.
340   */
341   GetPathComponent(image->filename,BasePath,basename);
342   (void) FormatLocaleString(buffer,MaxTextExtent,
343     "  %s_icon : icon(color_table = %s_ct,\n",basename,basename);
344   (void) WriteBlobString(image,buffer);
345   for (y=0; y < (ssize_t) image->rows; y++)
346   {
347     p=GetVirtualPixels(image,0,y,image->columns,1,exception);
348     if (p == (const Quantum *) NULL)
349       break;
350     (void) WriteBlobString(image,"    \"");
351     for (x=0; x < (ssize_t) image->columns; x++)
352     {
353       k=((ssize_t) GetPixelIndex(image,p) % MaxCixels);
354       symbol[0]=Cixel[k];
355       for (j=1; j < (int) characters_per_pixel; j++)
356       {
357         k=(((int) GetPixelIndex(image,p)-k)/MaxCixels) %
358           MaxCixels;
359         symbol[j]=Cixel[k];
360       }
361       symbol[j]='\0';
362       (void) CopyMagickString(buffer,symbol,MaxTextExtent);
363       (void) WriteBlobString(image,buffer);
364       p+=GetPixelChannels(image);
365     }
366     (void) FormatLocaleString(buffer,MaxTextExtent,"\"%s\n",
367       (y == (ssize_t) (image->rows-1) ? ");" : ","));
368     (void) WriteBlobString(image,buffer);
369     status=SetImageProgress(image,SaveImageTag,(MagickOffsetType) y,
370       image->rows);
371     if (status == MagickFalse)
372       break;
373   }
374   symbol=DestroyString(symbol);
375   (void) CloseBlob(image);
376   return(MagickTrue);
377 }