Coverage Report

Created: 2026-05-16 07:22

next uncovered line (L), next uncovered region (R), next uncovered branch (B)
/src/imagemagick/coders/pix.c
Line
Count
Source
1
/*
2
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
3
%                                                                             %
4
%                                                                             %
5
%                                                                             %
6
%                            PPPP   IIIII  X   X                              %
7
%                            P   P    I     X X                               %
8
%                            PPPP     I      X                                %
9
%                            P        I     X X                               %
10
%                            P      IIIII  X   X                              %
11
%                                                                             %
12
%                                                                             %
13
%                    Read Alias/Wavefront RLE Image 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/license/                                         %
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/blob.h"
44
#include "MagickCore/blob-private.h"
45
#include "MagickCore/cache.h"
46
#include "MagickCore/colormap.h"
47
#include "MagickCore/exception.h"
48
#include "MagickCore/exception-private.h"
49
#include "MagickCore/image.h"
50
#include "MagickCore/image-private.h"
51
#include "MagickCore/list.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

62
/*
63
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
64
%                                                                             %
65
%                                                                             %
66
%                                                                             %
67
%   R e a d P I X I m a g e                                                   %
68
%                                                                             %
69
%                                                                             %
70
%                                                                             %
71
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
72
%
73
%  ReadPIXImage() reads a Alias/Wavefront RLE image file and returns it.
74
%  It allocates the memory necessary for the new Image structure and returns a
75
%  pointer to the new image.
76
%
77
%  The format of the ReadPIXImage method is:
78
%
79
%      Image *ReadPIXImage(const ImageInfo *image_info,ExceptionInfo *exception)
80
%
81
%  A description of each parameter follows:
82
%
83
%    o image_info: the image info.
84
%
85
%    o exception: return any errors or warnings in this structure.
86
%
87
%
88
*/
89
static Image *ReadPIXImage(const ImageInfo *image_info,ExceptionInfo *exception)
90
429
{
91
429
  Image
92
429
    *image;
93
94
429
  MagickBooleanType
95
429
    status;
96
97
429
  Quantum
98
429
    blue,
99
429
    green,
100
429
    index,
101
429
    red;
102
103
429
  ssize_t
104
429
    x;
105
106
429
  Quantum
107
429
    *q;
108
109
429
  size_t
110
429
    bits_per_pixel,
111
429
    height,
112
429
    length,
113
429
    width;
114
115
429
  ssize_t
116
429
    y;
117
118
  /*
119
    Open image file.
120
  */
121
429
  assert(image_info != (const ImageInfo *) NULL);
122
429
  assert(image_info->signature == MagickCoreSignature);
123
429
  assert(exception != (ExceptionInfo *) NULL);
124
429
  assert(exception->signature == MagickCoreSignature);
125
429
  if (IsEventLogging() != MagickFalse)
126
0
    (void) LogMagickEvent(TraceEvent,GetMagickModule(),"%s",
127
0
      image_info->filename);
128
429
  image=AcquireImage(image_info,exception);
129
429
  status=OpenBlob(image_info,image,ReadBinaryBlobMode,exception);
130
429
  if (status == MagickFalse)
131
1
    {
132
1
      image=DestroyImageList(image);
133
1
      return((Image *) NULL);
134
1
    }
135
  /*
136
    Read PIX image.
137
  */
138
428
  width=ReadBlobMSBShort(image);
139
428
  height=ReadBlobMSBShort(image);
140
428
  (void) ReadBlobMSBShort(image);  /* x-offset */
141
428
  (void) ReadBlobMSBShort(image);  /* y-offset */
142
428
  bits_per_pixel=ReadBlobMSBShort(image);
143
428
  if ((width == 0UL) || (height == 0UL) || ((bits_per_pixel != 8) &&
144
171
      (bits_per_pixel != 24)))
145
351
    ThrowReaderException(CorruptImageError,"ImproperImageHeader");
146
351
  do
147
368
  {
148
    /*
149
      Initialize image structure.
150
    */
151
368
    image->columns=width;
152
368
    image->rows=height;
153
368
    if (bits_per_pixel == 8)
154
242
      if (AcquireImageColormap(image,256,exception) == MagickFalse)
155
368
        ThrowReaderException(ResourceLimitError,"MemoryAllocationFailed");
156
368
    if ((image_info->ping != MagickFalse) && (image_info->number_scenes != 0))
157
0
      if (image->scene >= (image_info->scene+image_info->number_scenes-1))
158
0
        break;
159
368
    status=SetImageExtent(image,image->columns,image->rows,exception);
160
368
    if (status == MagickFalse)
161
59
      return(DestroyImageList(image));
162
309
    status=ResetImagePixels(image,exception);
163
309
    if (status == MagickFalse)
164
0
      return(DestroyImageList(image));
165
    /*
166
      Convert PIX raster image to pixel packets.
167
    */
168
309
    red=(Quantum) 0;
169
309
    green=(Quantum) 0;
170
309
    blue=(Quantum) 0;
171
309
    index=0;
172
309
    length=0;
173
8.21k
    for (y=0; y < (ssize_t) image->rows; y++)
174
8.19k
    {
175
8.19k
      q=QueueAuthenticPixels(image,0,y,image->columns,1,exception);
176
8.19k
      if (q == (Quantum *) NULL)
177
0
        break;
178
1.92M
      for (x=0; x < (ssize_t) image->columns; x++)
179
1.92M
      {
180
1.92M
        if (length == 0)
181
14.1k
          {
182
14.1k
            int
183
14.1k
              c;
184
185
14.1k
            c=ReadBlobByte(image);
186
14.1k
            if ((c == 0) || (c == EOF))
187
295
              break;
188
13.8k
            length=(size_t) c;
189
13.8k
            if (bits_per_pixel == 8)
190
824
              index=ScaleCharToQuantum((unsigned char) ReadBlobByte(image));
191
13.0k
            else
192
13.0k
              {
193
13.0k
                blue=ScaleCharToQuantum((unsigned char) ReadBlobByte(image));
194
13.0k
                green=ScaleCharToQuantum((unsigned char) ReadBlobByte(image));
195
13.0k
                red=ScaleCharToQuantum((unsigned char) ReadBlobByte(image));
196
13.0k
              }
197
13.8k
          }
198
1.91M
        if (image->storage_class == PseudoClass)
199
140k
          SetPixelIndex(image,index,q);
200
1.91M
        SetPixelBlue(image,blue,q);
201
1.91M
        SetPixelGreen(image,green,q);
202
1.91M
        SetPixelRed(image,red,q);
203
1.91M
        length--;
204
1.91M
        q+=(ptrdiff_t) GetPixelChannels(image);
205
1.91M
      }
206
8.19k
      if (x < (ssize_t) image->columns)
207
295
        break;
208
7.90k
      if (SyncAuthenticPixels(image,exception) == MagickFalse)
209
0
        break;
210
7.90k
      if (image->previous == (Image *) NULL)
211
7.90k
        {
212
7.90k
          status=SetImageProgress(image,LoadImageTag,(MagickOffsetType) y,
213
7.90k
            image->rows);
214
7.90k
          if (status == MagickFalse)
215
0
            break;
216
7.90k
        }
217
7.90k
    }
218
309
    if (image->storage_class == PseudoClass)
219
198
      (void) SyncImage(image,exception);
220
309
    if (EOFBlob(image) != MagickFalse)
221
149
      {
222
149
        ThrowFileException(exception,CorruptImageError,"UnexpectedEndOfFile",
223
149
          image->filename);
224
149
        break;
225
149
      }
226
    /*
227
      Proceed to next image.
228
    */
229
160
    if (image_info->number_scenes != 0)
230
0
      if (image->scene >= (image_info->scene+image_info->number_scenes-1))
231
0
        break;
232
160
    width=ReadBlobMSBLong(image);
233
160
    height=ReadBlobMSBLong(image);
234
160
    (void) ReadBlobMSBShort(image);
235
160
    (void) ReadBlobMSBShort(image);
236
160
    bits_per_pixel=ReadBlobMSBShort(image);
237
160
    status=(width != 0UL) && (height == 0UL) && ((bits_per_pixel == 8) ||
238
143
      (bits_per_pixel == 24)) ? MagickTrue : MagickFalse;
239
160
    if (status != MagickFalse)
240
17
      {
241
        /*
242
          Allocate next image structure.
243
        */
244
17
        AcquireNextImage(image_info,image,exception);
245
17
        if (GetNextImageInList(image) == (Image *) NULL)
246
0
          {
247
0
            status=MagickFalse;
248
0
            break;
249
0
          }
250
17
        image=SyncNextImageInList(image);
251
17
        status=SetImageProgress(image,LoadImagesTag,TellBlob(image),
252
17
          GetBlobSize(image));
253
17
        if (status == MagickFalse)
254
0
          break;
255
17
      }
256
160
  } while (status != MagickFalse);
257
292
  if (CloseBlob(image) == MagickFalse)
258
0
    status=MagickFalse;
259
292
  if (status == MagickFalse)
260
143
    return(DestroyImageList(image));
261
149
  return(GetFirstImageInList(image));
262
292
}
263

264
/*
265
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
266
%                                                                             %
267
%                                                                             %
268
%                                                                             %
269
%   R e g i s t e r P I X I m a g e                                           %
270
%                                                                             %
271
%                                                                             %
272
%                                                                             %
273
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
274
%
275
%  RegisterPIXImage() adds attributes for the PIX image format to
276
%  the list of supported formats.  The attributes include the image format
277
%  tag, a method to read and/or write the format, whether the format
278
%  supports the saving of more than one frame to the same file or blob,
279
%  whether the format supports native in-memory I/O, and a brief
280
%  description of the format.
281
%
282
%  The format of the RegisterPIXImage method is:
283
%
284
%      size_t RegisterPIXImage(void)
285
%
286
*/
287
ModuleExport size_t RegisterPIXImage(void)
288
8
{
289
8
  MagickInfo
290
8
    *entry;
291
292
8
  entry=AcquireMagickInfo("PIX","PIX","Alias/Wavefront RLE image format");
293
8
  entry->decoder=(DecodeImageHandler *) ReadPIXImage;
294
8
  (void) RegisterMagickInfo(entry);
295
8
  return(MagickImageCoderSignature);
296
8
}
297

298
/*
299
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
300
%                                                                             %
301
%                                                                             %
302
%                                                                             %
303
%   U n r e g i s t e r P I X I m a g e                                       %
304
%                                                                             %
305
%                                                                             %
306
%                                                                             %
307
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
308
%
309
%  UnregisterPIXImage() removes format registrations made by the
310
%  PIX module from the list of supported formats.
311
%
312
%  The format of the UnregisterPIXImage method is:
313
%
314
%      UnregisterPIXImage(void)
315
%
316
*/
317
ModuleExport void UnregisterPIXImage(void)
318
0
{
319
0
  (void) UnregisterMagickInfo("PIX");
320
0
}