Coverage Report

Created: 2025-06-16 07:00

/src/imagemagick/coders/uil.c
Line
Count
Source (jump to first uncovered line)
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 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
%    https://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
*/
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

65
/*
66
  Forward declarations.
67
*/
68
static MagickBooleanType
69
  WriteUILImage(const ImageInfo *,Image *,ExceptionInfo *);
70

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
8
{
96
8
  MagickInfo
97
8
    *entry;
98
99
8
  entry=AcquireMagickInfo("UIL","UIL","X-Motif UIL table");
100
8
  entry->encoder=(EncodeImageHandler *) WriteUILImage;
101
8
  entry->flags^=CoderAdjoinFlag;
102
8
  (void) RegisterMagickInfo(entry);
103
8
  return(MagickImageCoderSignature);
104
8
}
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
*/
125
ModuleExport void UnregisterUILImage(void)
126
0
{
127
0
  (void) UnregisterMagickInfo("UIL");
128
0
}
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
%
146
%      MagickBooleanType WriteUILImage(const ImageInfo *image_info,
147
%        Image *image,ExceptionInfo *exception)
148
%
149
%  A description of each parameter follows.
150
%
151
%    o image_info: the image info.
152
%
153
%    o image:  The image.
154
%
155
%    o exception: return any errors or warnings in this structure.
156
%
157
*/
158
static MagickBooleanType WriteUILImage(const ImageInfo *image_info,Image *image,
159
  ExceptionInfo *exception)
160
0
{
161
0
#define MaxCixels  92
162
163
0
  char
164
0
    basename[MagickPathExtent],
165
0
    buffer[MagickPathExtent],
166
0
    name[MagickPathExtent],
167
0
    *symbol;
168
169
0
  int
170
0
    j;
171
172
0
  MagickBooleanType
173
0
    status,
174
0
    transparent;
175
176
0
  MagickSizeType
177
0
    number_pixels;
178
179
0
  PixelInfo
180
0
    pixel;
181
182
0
  const Quantum
183
0
    *p;
184
185
0
  ssize_t
186
0
    i,
187
0
    x;
188
189
0
  size_t
190
0
    characters_per_pixel,
191
0
    colors;
192
193
0
  ssize_t
194
0
    k,
195
0
    y;
196
197
0
  static const char
198
0
    Cixel[MaxCixels+1] = " .XoO+@#$%&*=-;:>,<1234567890qwertyuipasdfghjk"
199
0
                         "lzxcvbnmMNBVCZASDFGHJKLPIUYTREWQ!~^/()_`'][{}|";
200
201
  /*
202
    Open output image file.
203
  */
204
0
  assert(image_info != (const ImageInfo *) NULL);
205
0
  assert(image_info->signature == MagickCoreSignature);
206
0
  assert(image != (Image *) NULL);
207
0
  assert(image->signature == MagickCoreSignature);
208
0
  assert(exception != (ExceptionInfo *) NULL);
209
0
  assert(exception->signature == MagickCoreSignature);
210
0
  if (IsEventLogging() != MagickFalse)
211
0
    (void) LogMagickEvent(TraceEvent,GetMagickModule(),"%s",image->filename);
212
0
  status=OpenBlob(image_info,image,WriteBinaryBlobMode,exception);
213
0
  if (status == MagickFalse)
214
0
    return(status);
215
0
  if (IssRGBCompatibleColorspace(image->colorspace) == MagickFalse)
216
0
    (void) TransformImageColorspace(image,sRGBColorspace,exception);
217
0
  transparent=MagickFalse;
218
0
  i=0;
219
0
  p=(const Quantum *) NULL;
220
0
  if (image->storage_class == PseudoClass)
221
0
    colors=image->colors;
222
0
  else
223
0
    {
224
0
      unsigned char
225
0
        *matte_image;
226
227
      /*
228
        Convert DirectClass to PseudoClass image.
229
      */
230
0
      matte_image=(unsigned char *) NULL;
231
0
      if (image->alpha_trait != UndefinedPixelTrait)
232
0
        {
233
          /*
234
            Map all the transparent pixels.
235
          */
236
0
          number_pixels=(MagickSizeType) image->columns*image->rows;
237
0
          if (number_pixels != ((MagickSizeType) (size_t) number_pixels))
238
0
            ThrowWriterException(ResourceLimitError,"MemoryAllocationFailed");
239
0
          matte_image=(unsigned char *) AcquireQuantumMemory(image->columns,
240
0
            image->rows*sizeof(*matte_image));
241
0
          if (matte_image == (unsigned char *) NULL)
242
0
            ThrowWriterException(ResourceLimitError,"MemoryAllocationFailed");
243
0
          for (y=0; y < (ssize_t) image->rows; y++)
244
0
          {
245
0
            p=GetVirtualPixels(image,0,y,image->columns,1,exception);
246
0
            if (p == (const Quantum *) NULL)
247
0
              break;
248
0
            for (x=0; x < (ssize_t) image->columns; x++)
249
0
            {
250
0
              matte_image[i]=(unsigned char) (GetPixelAlpha(image,p) ==
251
0
                (Quantum) TransparentAlpha ? 1 : 0);
252
0
              if (matte_image[i] != 0)
253
0
                transparent=MagickTrue;
254
0
              i++;
255
0
              p+=(ptrdiff_t) GetPixelChannels(image);
256
0
            }
257
0
          }
258
0
        }
259
0
      (void) SetImageType(image,PaletteType,exception);
260
0
      colors=image->colors;
261
0
      if (transparent != MagickFalse)
262
0
        {
263
0
          Quantum
264
0
            *q;
265
266
0
          i=0;
267
0
          colors++;
268
0
          for (y=0; y < (ssize_t) image->rows; y++)
269
0
          {
270
0
            q=GetAuthenticPixels(image,0,y,image->columns,1,exception);
271
0
            if (q == (Quantum *) NULL)
272
0
              break;
273
0
            for (x=0; x < (ssize_t) image->columns; x++)
274
0
            {
275
0
              if (matte_image[i] != 0)
276
0
                SetPixelIndex(image,(Quantum) image->colors,q);
277
0
              i++;
278
0
              q+=(ptrdiff_t) GetPixelChannels(image);
279
0
            }
280
0
          }
281
0
        }
282
0
      if (matte_image != (unsigned char *) NULL)
283
0
        matte_image=(unsigned char *) RelinquishMagickMemory(matte_image);
284
0
    }
285
  /*
286
    Compute the character per pixel.
287
  */
288
0
  characters_per_pixel=1;
289
0
  for (k=MaxCixels; (ssize_t) colors > k; k*=MaxCixels)
290
0
    characters_per_pixel++;
291
  /*
292
    UIL header.
293
  */
294
0
  symbol=AcquireString("");
295
0
  (void) WriteBlobString(image,"/* UIL */\n");
296
0
  GetPathComponent(image->filename,BasePath,basename);
297
0
  (void) FormatLocaleString(buffer,MagickPathExtent,
298
0
    "value\n  %s_ct : color_table(\n",basename);
299
0
  (void) WriteBlobString(image,buffer);
300
0
  GetPixelInfo(image,&pixel);
301
0
  for (i=0; i < (ssize_t) colors; i++)
302
0
  {
303
    /*
304
      Define UIL color.
305
    */
306
0
    pixel=image->colormap[i];
307
0
    pixel.colorspace=sRGBColorspace;
308
0
    pixel.depth=8;
309
0
    pixel.alpha=(double) OpaqueAlpha;
310
0
    GetColorTuple(&pixel,MagickTrue,name);
311
0
    if (transparent != MagickFalse)
312
0
      if (i == (ssize_t) (colors-1))
313
0
        (void) CopyMagickString(name,"None",MagickPathExtent);
314
    /*
315
      Write UIL color.
316
    */
317
0
    k=i % MaxCixels;
318
0
    symbol[0]=Cixel[k];
319
0
    for (j=1; j < (int) characters_per_pixel; j++)
320
0
    {
321
0
      k=((i-k)/MaxCixels) % MaxCixels;
322
0
      symbol[j]=Cixel[k];
323
0
    }
324
0
    symbol[j]='\0';
325
0
    (void) SubstituteString(&symbol,"'","''");
326
0
    if (LocaleCompare(name,"None") == 0)
327
0
      (void) FormatLocaleString(buffer,MagickPathExtent,
328
0
        "    background color = '%s'",symbol);
329
0
    else
330
0
      (void) FormatLocaleString(buffer,MagickPathExtent,
331
0
        "    color('%s',%s) = '%s'",name,
332
0
        GetPixelInfoIntensity(image,image->colormap+i) <
333
0
        ((double) QuantumRange/2.0) ? "background" : "foreground",symbol);
334
0
    (void) WriteBlobString(image,buffer);
335
0
    (void) FormatLocaleString(buffer,MagickPathExtent,"%s",
336
0
      (i == (ssize_t) (colors-1) ? ");\n" : ",\n"));
337
0
    (void) WriteBlobString(image,buffer);
338
0
  }
339
  /*
340
    Define UIL pixels.
341
  */
342
0
  GetPathComponent(image->filename,BasePath,basename);
343
0
  (void) FormatLocaleString(buffer,MagickPathExtent,
344
0
    "  %s_icon : icon(color_table = %s_ct,\n",basename,basename);
345
0
  (void) WriteBlobString(image,buffer);
346
0
  for (y=0; y < (ssize_t) image->rows; y++)
347
0
  {
348
0
    p=GetVirtualPixels(image,0,y,image->columns,1,exception);
349
0
    if (p == (const Quantum *) NULL)
350
0
      break;
351
0
    (void) WriteBlobString(image,"    \"");
352
0
    for (x=0; x < (ssize_t) image->columns; x++)
353
0
    {
354
0
      k=((ssize_t) GetPixelIndex(image,p) % MaxCixels);
355
0
      symbol[0]=Cixel[k];
356
0
      for (j=1; j < (int) characters_per_pixel; j++)
357
0
      {
358
0
        k=(((int) GetPixelIndex(image,p)-k)/MaxCixels) %
359
0
          MaxCixels;
360
0
        symbol[j]=Cixel[k];
361
0
      }
362
0
      symbol[j]='\0';
363
0
      (void) CopyMagickString(buffer,symbol,MagickPathExtent);
364
0
      (void) WriteBlobString(image,buffer);
365
0
      p+=(ptrdiff_t) GetPixelChannels(image);
366
0
    }
367
0
    (void) FormatLocaleString(buffer,MagickPathExtent,"\"%s\n",
368
0
      (y == (ssize_t) (image->rows-1) ? ");" : ","));
369
0
    (void) WriteBlobString(image,buffer);
370
0
    status=SetImageProgress(image,SaveImageTag,(MagickOffsetType) y,
371
0
      image->rows);
372
0
    if (status == MagickFalse)
373
0
      break;
374
0
  }
375
0
  symbol=DestroyString(symbol);
376
0
  if (CloseBlob(image) == MagickFalse)
377
0
    status=MagickFalse;
378
0
  return(status);
379
0
}