Coverage Report

Created: 2025-06-16 07:00

/src/imagemagick/coders/art.c
Line
Count
Source (jump to first uncovered line)
1
/*
2
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
3
%                                                                             %
4
%                                                                             %
5
%                                                                             %
6
%                             AAA   RRRR   TTTTT                              %
7
%                            A   A  R   R    T                                %
8
%                            AAAAA  RRRR     T                                %
9
%                            A   A  R R      T                                %
10
%                            A   A  R  R     T                                %
11
%                                                                             %
12
%                                                                             %
13
%                 Support PFS: 1st Publisher Clip Art Format                  %
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-private.h"
48
#include "MagickCore/colormap.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.h"
54
#include "MagickCore/image-private.h"
55
#include "MagickCore/list.h"
56
#include "MagickCore/magick.h"
57
#include "MagickCore/memory_.h"
58
#include "MagickCore/monitor.h"
59
#include "MagickCore/monitor-private.h"
60
#include "MagickCore/quantum-private.h"
61
#include "MagickCore/static.h"
62
#include "MagickCore/string_.h"
63
#include "MagickCore/module.h"
64

65
/*
66
  Forward declarations.
67
*/
68
static MagickBooleanType
69
  WriteARTImage(const ImageInfo *,Image *,ExceptionInfo *);
70

71
/*
72
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
73
%                                                                             %
74
%                                                                             %
75
%                                                                             %
76
%   R e a d A R T I m a g e                                                   %
77
%                                                                             %
78
%                                                                             %
79
%                                                                             %
80
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
81
%
82
%  ReadARTImage() reads an image of raw bits in LSB order and returns it.
83
%  It allocates the memory necessary for the new Image structure and returns
84
%  a pointer to the new image.
85
%
86
%  The format of the ReadARTImage method is:
87
%
88
%      Image *ReadARTImage(const ImageInfo *image_info,
89
%        ExceptionInfo *exception)
90
%
91
%  A description of each parameter follows:
92
%
93
%    o image_info: the image info.
94
%
95
%    o exception: return any errors or warnings in this structure.
96
%
97
*/
98
static Image *ReadARTImage(const ImageInfo *image_info,ExceptionInfo *exception)
99
574
{
100
574
  Image
101
574
    *image;
102
103
574
  QuantumInfo
104
574
    *quantum_info;
105
106
574
  MagickBooleanType
107
574
    status;
108
109
574
  size_t
110
574
    length;
111
112
574
  ssize_t
113
574
    count,
114
574
    y;
115
116
574
  unsigned char
117
574
    *pixels;
118
119
  /*
120
    Open image file.
121
  */
122
574
  assert(image_info != (const ImageInfo *) NULL);
123
574
  assert(image_info->signature == MagickCoreSignature);
124
574
  assert(exception != (ExceptionInfo *) NULL);
125
574
  assert(exception->signature == MagickCoreSignature);
126
574
  if (IsEventLogging() != MagickFalse)
127
0
    (void) LogMagickEvent(TraceEvent,GetMagickModule(),"%s",
128
0
      image_info->filename);
129
574
  image=AcquireImage(image_info,exception);
130
574
  status=OpenBlob(image_info,image,ReadBinaryBlobMode,exception);
131
574
  if (status == MagickFalse)
132
14
    {
133
14
      image=DestroyImageList(image);
134
14
      return((Image *) NULL);
135
14
    }
136
560
  image->depth=1;
137
560
  image->endian=MSBEndian;
138
560
  (void) ReadBlobLSBShort(image);
139
560
  image->columns=(size_t) ReadBlobLSBShort(image);
140
560
  (void) ReadBlobLSBShort(image);
141
560
  image->rows=(size_t) ReadBlobLSBShort(image);
142
560
  if ((image->columns == 0) || (image->rows == 0))
143
522
    ThrowReaderException(CorruptImageError,"ImproperImageHeader");
144
522
  if (image_info->ping != MagickFalse)
145
0
    {
146
0
      (void) CloseBlob(image);
147
0
      return(GetFirstImageInList(image));
148
0
    }
149
522
  status=SetImageExtent(image,image->columns,image->rows,exception);
150
522
  if (status == MagickFalse)
151
45
    return(DestroyImageList(image));
152
  /*
153
    Convert bi-level image to pixel packets.
154
  */
155
477
  SetImageColorspace(image,GRAYColorspace,exception);
156
477
  quantum_info=AcquireQuantumInfo(image_info,image);
157
477
  if (quantum_info == (QuantumInfo *) NULL)
158
477
    ThrowReaderException(ResourceLimitError,"MemoryAllocationFailed");
159
477
  length=GetQuantumExtent(image,quantum_info,GrayQuantum);
160
477
  pixels=GetQuantumPixels(quantum_info);
161
44.4k
  for (y=0; y < (ssize_t) image->rows; y++)
162
44.1k
  {
163
44.1k
    const void
164
44.1k
      *stream;
165
166
44.1k
    Quantum
167
44.1k
      *magick_restrict q;
168
169
44.1k
    q=QueueAuthenticPixels(image,0,y,image->columns,1,exception);
170
44.1k
    if (q == (Quantum *) NULL)
171
0
      break;
172
44.1k
    stream=ReadBlobStream(image,length,pixels,&count);
173
44.1k
    if (count != (ssize_t) length)
174
211
      break;
175
43.9k
    (void) ImportQuantumPixels(image,(CacheView *) NULL,quantum_info,
176
43.9k
      GrayQuantum,(unsigned char *) stream,exception);
177
43.9k
    stream=ReadBlobStream(image,(size_t) (-(ssize_t) length) & 0x01,pixels,
178
43.9k
      &count);
179
43.9k
    if (SyncAuthenticPixels(image,exception) == MagickFalse)
180
0
      break;
181
43.9k
    if (SetImageProgress(image,LoadImageTag,y,image->rows) == MagickFalse)
182
0
      break;
183
43.9k
  }
184
477
  SetQuantumImageType(image,GrayQuantum);
185
477
  quantum_info=DestroyQuantumInfo(quantum_info);
186
477
  if (y < (ssize_t) image->rows)
187
266
    ThrowReaderException(CorruptImageError,"UnableToReadImageData");
188
266
  if (EOFBlob(image) != MagickFalse)
189
20
    ThrowFileException(exception,CorruptImageError,"UnexpectedEndOfFile",
190
266
      image->filename);
191
266
  if (CloseBlob(image) == MagickFalse)
192
0
    status=MagickFalse;
193
266
  if (status == MagickFalse)
194
0
    return(DestroyImageList(image));
195
266
  return(GetFirstImageInList(image));
196
266
}
197

198
/*
199
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
200
%                                                                             %
201
%                                                                             %
202
%                                                                             %
203
%   R e g i s t e r A R T I m a g e                                           %
204
%                                                                             %
205
%                                                                             %
206
%                                                                             %
207
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
208
%
209
%  RegisterARTImage() adds attributes for the ART image format to
210
%  the list of supported formats.  The attributes include the image format
211
%  tag, a method to read and/or write the format, whether the format
212
%  supports the saving of more than one frame to the same file or blob,
213
%  whether the format supports native in-memory I/O, and a brief
214
%  description of the format.
215
%
216
%  The format of the RegisterARTImage method is:
217
%
218
%      size_t RegisterARTImage(void)
219
%
220
*/
221
ModuleExport size_t RegisterARTImage(void)
222
9
{
223
9
  MagickInfo
224
9
    *entry;
225
226
9
  entry=AcquireMagickInfo("ART","ART","PFS: 1st Publisher Clip Art");
227
9
  entry->decoder=(DecodeImageHandler *) ReadARTImage;
228
9
  entry->encoder=(EncodeImageHandler *) WriteARTImage;
229
9
  entry->flags|=CoderRawSupportFlag;
230
9
  entry->flags^=CoderAdjoinFlag;
231
9
  (void) RegisterMagickInfo(entry);
232
9
  return(MagickImageCoderSignature);
233
9
}
234

235
/*
236
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
237
%                                                                             %
238
%                                                                             %
239
%                                                                             %
240
%   U n r e g i s t e r A R T I m a g e                                       %
241
%                                                                             %
242
%                                                                             %
243
%                                                                             %
244
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
245
%
246
%  UnregisterARTImage() removes format registrations made by the
247
%  ART module from the list of supported formats.
248
%
249
%  The format of the UnregisterARTImage method is:
250
%
251
%      UnregisterARTImage(void)
252
%
253
*/
254
ModuleExport void UnregisterARTImage(void)
255
0
{
256
0
  (void) UnregisterMagickInfo("ART");
257
0
}
258

259
/*
260
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
261
%                                                                             %
262
%                                                                             %
263
%                                                                             %
264
%   W r i t e A R T I m a g e                                                 %
265
%                                                                             %
266
%                                                                             %
267
%                                                                             %
268
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
269
%
270
%  WriteARTImage() writes an image of raw bits in LSB order to a file.
271
%
272
%  The format of the WriteARTImage method is:
273
%
274
%      MagickBooleanType WriteARTImage(const ImageInfo *image_info,
275
%        Image *image,ExceptionInfo *exception)
276
%
277
%  A description of each parameter follows.
278
%
279
%    o image_info: the image info.
280
%
281
%    o image:  The image.
282
%
283
%    o exception: return any errors or warnings in this structure.
284
%
285
*/
286
static MagickBooleanType WriteARTImage(const ImageInfo *image_info,Image *image,
287
  ExceptionInfo *exception)
288
245
{
289
245
  MagickBooleanType
290
245
    status;
291
292
245
  QuantumInfo
293
245
    *quantum_info;
294
295
245
  const Quantum
296
245
    *p;
297
298
245
  size_t
299
245
    length;
300
301
245
  ssize_t
302
245
    count,
303
245
    y;
304
305
245
  unsigned char
306
245
    *pixels;
307
308
  /*
309
    Open output image file.
310
  */
311
245
  assert(image_info != (const ImageInfo *) NULL);
312
245
  assert(image_info->signature == MagickCoreSignature);
313
245
  assert(image != (Image *) NULL);
314
245
  assert(image->signature == MagickCoreSignature);
315
245
  assert(exception != (ExceptionInfo *) NULL);
316
245
  assert(exception->signature == MagickCoreSignature);
317
245
  if (IsEventLogging() != MagickFalse)
318
0
    (void) LogMagickEvent(TraceEvent,GetMagickModule(),"%s",image->filename);
319
245
  status=OpenBlob(image_info,image,WriteBinaryBlobMode,exception);
320
245
  if (status == MagickFalse)
321
0
    return(status);
322
245
  if ((image->columns > 65535UL) || (image->rows > 65535UL))
323
245
    ThrowWriterException(ImageError,"WidthOrHeightExceedsLimit");
324
245
  if (IssRGBCompatibleColorspace(image->colorspace) == MagickFalse)
325
0
    (void) TransformImageColorspace(image,sRGBColorspace,exception);
326
245
  (void) SetImageType(image,BilevelType,exception);
327
245
  image->endian=MSBEndian;
328
245
  image->depth=1;
329
245
  (void) WriteBlobLSBShort(image,0);
330
245
  (void) WriteBlobLSBShort(image,(unsigned short) image->columns);
331
245
  (void) WriteBlobLSBShort(image,0);
332
245
  (void) WriteBlobLSBShort(image,(unsigned short) image->rows);
333
245
  quantum_info=AcquireQuantumInfo(image_info,image);
334
245
  if (quantum_info == (QuantumInfo *) NULL)
335
245
    ThrowWriterException(ImageError,"MemoryAllocationFailed");
336
245
  pixels=(unsigned char *) GetQuantumPixels(quantum_info);
337
27.8k
  for (y=0; y < (ssize_t) image->rows; y++)
338
27.5k
  {
339
27.5k
    p=GetVirtualPixels(image,0,y,image->columns,1,exception);
340
27.5k
    if (p == (const Quantum *) NULL)
341
0
      break;
342
27.5k
    length=ExportQuantumPixels(image,(CacheView *) NULL,quantum_info,
343
27.5k
      GrayQuantum,pixels,exception);
344
27.5k
    count=WriteBlob(image,length,pixels);
345
27.5k
    if (count != (ssize_t) length)
346
0
      break;
347
27.5k
    count=WriteBlob(image,(size_t) (-(ssize_t) length) & 0x01,pixels);
348
27.5k
    status=SetImageProgress(image,SaveImageTag,(MagickOffsetType) y,
349
27.5k
      image->rows);
350
27.5k
    if (status == MagickFalse)
351
0
      break;
352
27.5k
  }
353
245
  quantum_info=DestroyQuantumInfo(quantum_info);
354
245
  if (y < (ssize_t) image->rows)
355
245
    ThrowWriterException(CorruptImageError,"UnableToWriteImageData");
356
245
  if (CloseBlob(image) == MagickFalse)
357
0
    status=MagickFalse;
358
245
  return(status);
359
245
}