1 /*
2 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
3 % %
4 % %
5 % %
6 % %
7 % FFFFF L IIIII FFFFF %
8 % F L I F %
9 % FFF L I FFF %
10 % F L I F %
11 % F LLLLL IIIII F %
12 % %
13 % %
14 % Support FilmLight Image Format %
15 % %
16 % Software Design %
17 % Cristy %
18 % November 2020 %
19 % %
20 % %
21 % Copyright 1999-2021 ImageMagick Studio LLC, a non-profit organization %
22 % dedicated to making software imaging solutions freely available. %
23 % %
24 % You may not use this file except in compliance with the License. You may %
25 % obtain a copy of the License at %
26 % %
27 % https://imagemagick.org/script/license.php %
28 % %
29 % Unless required by applicable law or agreed to in writing, software %
30 % distributed under the License is distributed on an "AS IS" BASIS, %
31 % WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. %
32 % See the License for the specific language governing permissions and %
33 % limitations under the License. %
34 % %
35 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
36 %
37 %
38 */
39
40 /*
41 Include declarations.
42 */
43 #include "MagickCore/studio.h"
44 #include "MagickCore/attribute.h"
45 #include "MagickCore/blob.h"
46 #include "MagickCore/blob-private.h"
47 #include "MagickCore/cache.h"
48 #include "MagickCore/color-private.h"
49 #include "MagickCore/colormap.h"
50 #include "MagickCore/colorspace.h"
51 #include "MagickCore/colorspace-private.h"
52 #include "MagickCore/exception.h"
53 #include "MagickCore/exception-private.h"
54 #include "MagickCore/image.h"
55 #include "MagickCore/image-private.h"
56 #include "MagickCore/list.h"
57 #include "MagickCore/magick.h"
58 #include "MagickCore/memory_.h"
59 #include "MagickCore/monitor.h"
60 #include "MagickCore/monitor-private.h"
61 #include "MagickCore/quantum-private.h"
62 #include "MagickCore/static.h"
63 #include "MagickCore/string_.h"
64 #include "MagickCore/module.h"
65
66 /*
67 Forward declarations.
68 */
69 static MagickBooleanType
70 WriteFL32Image(const ImageInfo *,Image *,ExceptionInfo *);
71
72 /*
73 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
74 % %
75 % %
76 % %
77 % I s F L 3 2 %
78 % %
79 % %
80 % %
81 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
82 %
83 % IsFL32() returns MagickTrue if the image format type, identified by the
84 % magick string, is FL32.
85 %
86 % The format of the IsFL32 method is:
87 %
88 % MagickBooleanType IsFL32(const unsigned char *magick,const size_t extent)
89 %
90 % A description of each parameter follows:
91 %
92 % o magick: compare image format pattern against these bytes.
93 %
94 % o extent: Specifies the extent of the magick string.
95 %
96 */
IsFL32(const unsigned char * magick,const size_t extent)97 static MagickBooleanType IsFL32(const unsigned char *magick,const size_t extent)
98 {
99 if (extent < 4)
100 return(MagickFalse);
101 if (LocaleNCompare((char *) magick,"FL32",4) == 0)
102 return(MagickTrue);
103 return(MagickFalse);
104 }
105
106 /*
107 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
108 % %
109 % %
110 % %
111 % R e a d F L 3 2 I m a g e %
112 % %
113 % %
114 % %
115 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
116 %
117 % ReadFL32Image() reads an image of raw bits in LSB order and returns it.
118 % It allocates the memory necessary for the new Image structure and returns
119 % a pointer to the new image.
120 %
121 % The format of the ReadFL32Image method is:
122 %
123 % Image *ReadFL32Image(const ImageInfo *image_info,
124 % ExceptionInfo *exception)
125 %
126 % A description of each parameter follows:
127 %
128 % o image_info: the image info.
129 %
130 % o exception: return any errors or warnings in this structure.
131 %
132 */
ReadFL32Image(const ImageInfo * image_info,ExceptionInfo * exception)133 static Image *ReadFL32Image(const ImageInfo *image_info,
134 ExceptionInfo *exception)
135 {
136 Image
137 *image;
138
139 QuantumInfo
140 *quantum_info;
141
142 QuantumType
143 quantum_type;
144
145 MagickBooleanType
146 status;
147
148 size_t
149 extent;
150
151 ssize_t
152 count,
153 y;
154
155 unsigned char
156 *pixels;
157
158 unsigned int
159 magic;
160
161 /*
162 Open image file.
163 */
164 assert(image_info != (const ImageInfo *) NULL);
165 assert(image_info->signature == MagickCoreSignature);
166 if (image_info->debug != MagickFalse)
167 (void) LogMagickEvent(TraceEvent,GetMagickModule(),"%s",
168 image_info->filename);
169 assert(exception != (ExceptionInfo *) NULL);
170 assert(exception->signature == MagickCoreSignature);
171 image=AcquireImage(image_info,exception);
172 status=OpenBlob(image_info,image,ReadBinaryBlobMode,exception);
173 if (status == MagickFalse)
174 {
175 image=DestroyImageList(image);
176 return((Image *) NULL);
177 }
178 magic=ReadBlobLSBLong(image);
179 if (magic != 842222662)
180 ThrowReaderException(CorruptImageError,"ImproperImageHeader");
181 image->depth=32;
182 image->endian=LSBEndian;
183 image->rows=(size_t) ReadBlobLSBLong(image);
184 image->columns=(size_t) ReadBlobLSBLong(image);
185 image->number_channels=(size_t) ReadBlobLSBLong(image);
186 if ((image->columns == 0) || (image->rows == 0) ||
187 (image->number_channels == 0) ||
188 (image->number_channels >= MaxPixelChannels))
189 ThrowReaderException(CorruptImageError,"ImproperImageHeader");
190 if (image_info->ping != MagickFalse)
191 {
192 (void) CloseBlob(image);
193 return(GetFirstImageInList(image));
194 }
195 switch (image->number_channels)
196 {
197 case 1:
198 {
199 image->colorspace=GRAYColorspace;
200 quantum_type=GrayQuantum;
201 break;
202 }
203 case 2:
204 {
205 image->colorspace=GRAYColorspace;
206 image->alpha_trait=BlendPixelTrait;
207 quantum_type=GrayAlphaQuantum;
208 break;
209 }
210 case 3:
211 {
212 quantum_type=RGBQuantum;
213 break;
214 }
215 case 4:
216 {
217 image->alpha_trait=BlendPixelTrait;
218 quantum_type=RGBAQuantum;
219 break;
220 }
221 default:
222 {
223 quantum_type=RGBQuantum;
224 image->number_meta_channels=image->number_channels-3;
225 break;
226 }
227 }
228 status=SetImageExtent(image,image->columns,image->rows,exception);
229 if (status == MagickFalse)
230 return(DestroyImageList(image));
231 /*
232 Convert FL32 image to pixel packets.
233 */
234 quantum_info=AcquireQuantumInfo(image_info,image);
235 if (quantum_info == (QuantumInfo *) NULL)
236 ThrowReaderException(ResourceLimitError,"MemoryAllocationFailed");
237 status=SetQuantumFormat(image,quantum_info,FloatingPointQuantumFormat);
238 extent=GetQuantumExtent(image,quantum_info,quantum_type);
239 pixels=GetQuantumPixels(quantum_info);
240 for (y=0; y < (ssize_t) image->rows; y++)
241 {
242 const void
243 *stream;
244
245 Quantum
246 *magick_restrict q;
247
248 q=QueueAuthenticPixels(image,0,y,image->columns,1,exception);
249 if (q == (Quantum *) NULL)
250 break;
251 stream=ReadBlobStream(image,extent,pixels,&count);
252 if (count != (ssize_t) extent)
253 break;
254 (void) ImportQuantumPixels(image,(CacheView *) NULL,quantum_info,
255 quantum_type,(unsigned char *) stream,exception);
256 if (SyncAuthenticPixels(image,exception) == MagickFalse)
257 break;
258 if (SetImageProgress(image,LoadImageTag,y,image->rows) == MagickFalse)
259 break;
260 }
261 SetQuantumImageType(image,quantum_type);
262 quantum_info=DestroyQuantumInfo(quantum_info);
263 if (y < (ssize_t) image->rows)
264 ThrowReaderException(CorruptImageError,"UnableToReadImageData");
265 if (EOFBlob(image) != MagickFalse)
266 ThrowFileException(exception,CorruptImageError,"UnexpectedEndOfFile",
267 image->filename);
268 (void) CloseBlob(image);
269 return(GetFirstImageInList(image));
270 }
271
272 /*
273 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
274 % %
275 % %
276 % %
277 % R e g i s t e r F L 3 2 I m a g e %
278 % %
279 % %
280 % %
281 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
282 %
283 % RegisterFL32Image() adds attributes for the FL32 image format to
284 % the list of supported formats. The attributes include the image format
285 % tag, a method to read and/or write the format, whether the format
286 % supports the saving of more than one frame to the same file or blob,
287 % whether the format supports native in-memory I/O, and a brief
288 % description of the format.
289 %
290 % The format of the RegisterFL32Image method is:
291 %
292 % size_t RegisterFL32Image(void)
293 %
294 */
RegisterFL32Image(void)295 ModuleExport size_t RegisterFL32Image(void)
296 {
297 MagickInfo
298 *entry;
299
300 entry=AcquireMagickInfo("FL32","FL32","FilmLight");
301 entry->decoder=(DecodeImageHandler *) ReadFL32Image;
302 entry->encoder=(EncodeImageHandler *) WriteFL32Image;
303 entry->magick=(IsImageFormatHandler *) IsFL32;
304 entry->flags|=CoderRawSupportFlag;
305 entry->flags^=CoderAdjoinFlag;
306 (void) RegisterMagickInfo(entry);
307 return(MagickImageCoderSignature);
308 }
309
310 /*
311 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
312 % %
313 % %
314 % %
315 % U n r e g i s t e r F L 3 2 I m a g e %
316 % %
317 % %
318 % %
319 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
320 %
321 % UnregisterFL32Image() removes format registrations made by the
322 % FL32 module from the list of supported formats.
323 %
324 % The format of the UnregisterFL32Image method is:
325 %
326 % UnregisterFL32Image(void)
327 %
328 */
UnregisterFL32Image(void)329 ModuleExport void UnregisterFL32Image(void)
330 {
331 (void) UnregisterMagickInfo("FL32");
332 }
333
334 /*
335 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
336 % %
337 % %
338 % %
339 % W r i t e F L 3 2 I m a g e %
340 % %
341 % %
342 % %
343 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
344 %
345 % WriteFL32Image() writes an image of raw bits in LSB order to a file.
346 %
347 % The format of the WriteFL32Image method is:
348 %
349 % MagickBooleanType WriteFL32Image(const ImageInfo *image_info,
350 % Image *image,ExceptionInfo *exception)
351 %
352 % A description of each parameter follows.
353 %
354 % o image_info: the image info.
355 %
356 % o image: The image.
357 %
358 % o exception: return any errors or warnings in this structure.
359 %
360 */
WriteFL32Image(const ImageInfo * image_info,Image * image,ExceptionInfo * exception)361 static MagickBooleanType WriteFL32Image(const ImageInfo *image_info,
362 Image *image,ExceptionInfo *exception)
363 {
364 MagickBooleanType
365 status;
366
367 QuantumInfo
368 *quantum_info;
369
370 QuantumType
371 quantum_type;
372
373 const Quantum
374 *p;
375
376 size_t
377 extent;
378
379 ssize_t
380 count,
381 y;
382
383 unsigned char
384 *pixels;
385
386 /*
387 Open output image file.
388 */
389 assert(image_info != (const ImageInfo *) NULL);
390 assert(image_info->signature == MagickCoreSignature);
391 assert(image != (Image *) NULL);
392 assert(image->signature == MagickCoreSignature);
393 if (image->debug != MagickFalse)
394 (void) LogMagickEvent(TraceEvent,GetMagickModule(),"%s",image->filename);
395 assert(exception != (ExceptionInfo *) NULL);
396 assert(exception->signature == MagickCoreSignature);
397 status=OpenBlob(image_info,image,WriteBinaryBlobMode,exception);
398 if (status == MagickFalse)
399 return(status);
400 (void) WriteBlobLSBLong(image,842222662U);
401 (void) WriteBlobLSBLong(image,(unsigned int) image->rows);
402 (void) WriteBlobLSBLong(image,(unsigned int) image->columns);
403 (void) WriteBlobLSBLong(image,(unsigned int) image->number_channels);
404 image->endian=LSBEndian;
405 image->depth=32;
406 switch (image->number_channels)
407 {
408 case 1:
409 {
410 quantum_type=GrayQuantum;
411 break;
412 }
413 case 2:
414 {
415 quantum_type=GrayAlphaQuantum;
416 break;
417 }
418 case 3:
419 {
420 quantum_type=RGBQuantum;
421 break;
422 }
423 case 4:
424 {
425 quantum_type=RGBAQuantum;
426 break;
427 }
428 default:
429 {
430 quantum_type=RGBQuantum;
431 break;
432 }
433 }
434 quantum_info=AcquireQuantumInfo(image_info,image);
435 if (quantum_info == (QuantumInfo *) NULL)
436 ThrowWriterException(ImageError,"MemoryAllocationFailed");
437 status=SetQuantumFormat(image,quantum_info,FloatingPointQuantumFormat);
438 pixels=(unsigned char *) GetQuantumPixels(quantum_info);
439 for (y=0; y < (ssize_t) image->rows; y++)
440 {
441 p=GetVirtualPixels(image,0,y,image->columns,1,exception);
442 if (p == (const Quantum *) NULL)
443 break;
444 extent=ExportQuantumPixels(image,(CacheView *) NULL,quantum_info,
445 quantum_type,pixels,exception);
446 count=WriteBlob(image,extent,pixels);
447 if (count != (ssize_t) extent)
448 break;
449 status=SetImageProgress(image,SaveImageTag,(MagickOffsetType) y,
450 image->rows);
451 if (status == MagickFalse)
452 break;
453 }
454 quantum_info=DestroyQuantumInfo(quantum_info);
455 if (y < (ssize_t) image->rows)
456 ThrowWriterException(CorruptImageError,"UnableToWriteImageData");
457 (void) CloseBlob(image);
458 return(status);
459 }
460