File: ora.c

package info (click to toggle)
graphicsmagick 1.4%2Breally1.3.45%2Bhg17696-1
  • links: PTS
  • area: main
  • in suites: forky, sid, trixie
  • size: 49,092 kB
  • sloc: ansic: 249,519; cpp: 18,805; sh: 8,000; perl: 3,634; makefile: 2,567; tcl: 2,209; python: 1,250
file content (250 lines) | stat: -rw-r--r-- 8,905 bytes parent folder | download | duplicates (2)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
/*
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
%                                                                             %
%                                                                             %
%                                                                             %
%                              OOO   RRRR    AAA                              %
%                             O   O  R   R  A   A                             %
%                             O   O  RRRR   AAAAA                             %
%                             O   O  R  R   A   A                             %
%                              OOO   R   R  A   A                             %
%                                                                             %
%                                                                             %
%                       Read OpenRaster (.ora) files                          %
%                                                                             %
%                             OpenRaster spec:                                %
%         https://www.freedesktop.org/wiki/Specifications/OpenRaster/         %
%                                                                             %
%                              Software Design                                %
%                              Jaroslav Fojtik                                %
%                                   2024                                      %
%                                                                             %
%                                                                             %
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
%
%
*/


/*
  Include declarations.
*/
#include "magick/studio.h"
#include "magick/blob.h"
#include "magick/colormap.h"
#include "magick/constitute.h"
#include "magick/image.h"
#include "magick/log.h"
#include "magick/magick.h"
#include "magick/monitor.h"
#include "magick/pixel_cache.h"
#include "magick/tempfile.h"
#include "magick/utility.h"


#if defined(HasPNG) && defined(HasZLIB) && (defined(HasUNZIP) || defined(HasLIBZIP))

#if defined(HasUNZIP)
 #include "contrib/minizip/unzip.h"
#else
 #include <zip.h>
#endif


static const char MERGED_IMAGE_PATH[] = "mergedimage.png";
#define BUFFER_SIZE 8196


/*
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
%                                                                             %
%                                                                             %
%                                                                             %
%   R e a d O R A I m a g e                                                   %
%                                                                             %
%                                                                             %
%                                                                             %
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
%
%  ReadORAImage reads an ORA file in the most basic way possible: by
%  reading it as a ZIP File and extracting the mergedimage.png file from it,
%  which is then passed to ReadPNGImage.
%  https://www.freedesktop.org/wiki/Specifications/OpenRaster/Draft/FileLayout/
%
%  The format of the ReadORAImage method is:
%
%      Image *ReadORAImage(const ImageInfo *image_info,ExceptionInfo *exception)
%
%  A description of each parameter follows:
%
%    o image:  Method ReadORAImage returns a pointer to the image after
%      reading. A null image is returned if there is a memory shortage or if
%      the image cannot be read.
%
%    o image_info: Specifies a pointer to a ImageInfo structure.
%
%    o exception: return any errors or warnings in this structure.
*/
static Image *ReadORAImage(const ImageInfo *image_info, ExceptionInfo *exception)
{
#if defined(HasUNZIP)
  unzFile zip_archive;
#else
  zip_t *zip_archive;
  zip_file_t *merged_image_file;
#endif

  Image *image;
  int res;
  void *buf;
  FILE *PngFile;
  ImageInfo *clone_info;

  assert(image_info != (const ImageInfo *) NULL);
  assert(image_info->signature == MagickSignature);
  assert(exception != (ExceptionInfo *) NULL);
  assert(exception->signature == MagickSignature);

  image = AllocateImage(image_info);

  buf = MagickAllocateResourceLimitedArray(void*,BUFFER_SIZE,1);
  if(buf == NULL)
        ThrowReaderException(ResourceLimitError,MemoryAllocationFailed,image);

#if defined(HasUNZIP)
  zip_archive = unzOpen(image_info->filename);
#else
  zip_archive = zip_open(image_info->filename,ZIP_RDONLY,&res);
#endif
  if(zip_archive == NULL)
  {
    MagickFreeResourceLimitedMemory(buf);
    ThrowReaderException(FileOpenError,UnableToOpenFile,image);
  }

#if defined(HasUNZIP)
  if(UNZ_OK != unzLocateFile(zip_archive,MERGED_IMAGE_PATH,0))
  {
    unzClose(zip_archive);
#else
  merged_image_file = zip_fopen(zip_archive,MERGED_IMAGE_PATH,ZIP_FL_UNCHANGED);
  if (merged_image_file == NULL)
  {
    zip_discard(zip_archive);
#endif
    MagickFreeResourceLimitedMemory(buf);
    ThrowReaderException(FileOpenError,UnableToOpenFile,image);
  }

  clone_info = CloneImageInfo(image_info);
  memcpy(clone_info->filename,"PNG:",4);
  PngFile = AcquireTemporaryFileStream(clone_info->filename+4,BinaryFileIOMode);
  if(!PngFile)
  {
    DestroyImageInfo(clone_info);
    clone_info = (ImageInfo *) NULL;
#if defined(HasUNZIP)
    unzClose(zip_archive);
#else
    zip_discard(zip_archive);
#endif
    MagickFreeResourceLimitedMemory(buf);
    ThrowReaderException(FileOpenError,UnableToOpenFile,image);
  }

#if defined(HasUNZIP)
  unzOpenCurrentFile(zip_archive);
  while((res=unzReadCurrentFile(zip_archive,buf,BUFFER_SIZE)) > 0)
  {
    fwrite(buf,res,1,PngFile);
  }
  unzCloseCurrentFile(zip_archive);
#else
  while((res=zip_fread(merged_image_file,buf,BUFFER_SIZE)) > 0)
  {
    fwrite(buf,res,1,PngFile);
  }
  (void)zip_fclose(merged_image_file);
  (void)zip_discard(zip_archive);
#endif

  fclose(PngFile);

  MagickFreeResourceLimitedMemory(buf);
#if defined(HasUNZIP)
  unzClose(zip_archive);
#endif

  DestroyImage(image);

  (void)strlcpy(clone_info->magick, "PNG", sizeof(clone_info->magick));
  image = ReadImage(clone_info,exception);

  DestroyImageInfo(clone_info);
  clone_info = (ImageInfo*)NULL;

  return(image);
}



/*
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
%                                                                             %
%                                                                             %
%                                                                             %
%   R e g i s t e r O R A I m a g e                                           %
%                                                                             %
%                                                                             %
%                                                                             %
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
%
%  Method RegisterORAImage adds attributes for the ORA image format to
%  the list of supported formats.  The attributes include the image format
%  tag, a method to read and/or write the format, whether the format
%  supports the saving of more than one frame to the same file or blob,
%  whether the format supports native in-memory I/O, and a brief
%  description of the format.
%
%  The format of the RegisterORAImage method is:
%
%      RegisterORAImage(void)
%
*/
ModuleExport void RegisterORAImage(void)
{
  MagickInfo *entry;

  entry=SetMagickInfo("ORA");
  entry->decoder = (DecoderHandler)ReadORAImage;
  entry->description="Open Raster";
  entry->module="ORA";
  entry->adjoin=False;
  (void) RegisterMagickInfo(entry);
}

/*
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
%                                                                             %
%                                                                             %
%                                                                             %
%   U n r e g i s t e r O R A I m a g e                                       %
%                                                                             %
%                                                                             %
%                                                                             %
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
%
%  Method UnregisterORAImage removes format registrations made by the
%  ORA module from the list of supported formats.
%
%  The format of the UnregisterORAImage method is:
%
%      UnregisterORAImage(void)
%
*/
ModuleExport void UnregisterORAImage(void)
{
  (void) UnregisterMagickInfo("ORA");
}

#endif