]> granicus.if.org Git - imagemagick/blob - coders/dng.c
(no commit message)
[imagemagick] / coders / dng.c
1 /*
2 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
3 %                                                                             %
4 %                                                                             %
5 %                                                                             %
6 %                            DDDD   N   N   GGGG                              %
7 %                            D   D  NN  N  GS                                 %
8 %                            D   D  N N N  G  GG                              %
9 %                            D   D  N  NN  G   G                              %
10 %                            DDDD   N   N   GGGG                              %
11 %                                                                             %
12 %                                                                             %
13 %                  Read the Digital Negative Image Format                     %
14 %                                                                             %
15 %                              Software Design                                %
16 %                                John Cristy                                  %
17 %                                 July 1999                                   %
18 %                                                                             %
19 %                                                                             %
20 %  Copyright 1999-2009 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 %    http://www.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   Include declarations.
40 */
41 #include "magick/studio.h"
42 #include "magick/blob.h"
43 #include "magick/blob-private.h"
44 #include "magick/constitute.h"
45 #include "magick/delegate.h"
46 #include "magick/exception.h"
47 #include "magick/exception-private.h"
48 #include "magick/geometry.h"
49 #include "magick/image.h"
50 #include "magick/image-private.h"
51 #include "magick/layer.h"
52 #include "magick/list.h"
53 #include "magick/log.h"
54 #include "magick/magick.h"
55 #include "magick/memory_.h"
56 #include "magick/resource_.h"
57 #include "magick/quantum-private.h"
58 #include "magick/static.h"
59 #include "magick/string_.h"
60 #include "magick/module.h"
61 #include "magick/transform.h"
62 #include "magick/utility.h"
63 #include "magick/xml-tree.h"
64 \f
65 /*
66 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
67 %                                                                             %
68 %                                                                             %
69 %                                                                             %
70 %   R e a d D N G I m a g e                                                   %
71 %                                                                             %
72 %                                                                             %
73 %                                                                             %
74 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
75 %
76 %  ReadDNGImage() reads an binary file in the Digital Negative format and
77 %  returns it.  It allocates the memory necessary for the new Image structure
78 %  and returns a pointer to the new image. 
79 %
80 %  The format of the ReadDNGImage method is:
81 %
82 %      Image *ReadDNGImage(const ImageInfo *image_info,
83 %        ExceptionInfo *exception)
84 %
85 %  A description of each parameter follows:
86 %
87 %    o image_info: the image info.
88 %
89 %    o exception: return any errors or warnings in this structure.
90 %
91 */
92 static Image *ReadDNGImage(const ImageInfo *image_info,ExceptionInfo *exception)
93 {
94   ExceptionInfo
95     *sans_exception;
96
97   Image
98     *image;
99
100   ImageInfo
101     *read_info;
102
103   MagickBooleanType
104     status;
105
106   /*
107     Open image file.
108   */
109   assert(image_info != (const ImageInfo *) NULL);
110   assert(image_info->signature == MagickSignature);
111   if (image_info->debug != MagickFalse)
112     (void) LogMagickEvent(TraceEvent,GetMagickModule(),"%s",
113       image_info->filename);
114   assert(exception != (ExceptionInfo *) NULL);
115   assert(exception->signature == MagickSignature);
116   image=AcquireImage(image_info);
117   status=OpenBlob(image_info,image,ReadBinaryBlobMode,exception);
118   if (status == MagickFalse)
119     {
120       image=DestroyImageList(image);
121       return((Image *) NULL);
122     }
123   (void) CloseBlob(image);
124   (void) DestroyImageList(image);
125   /*
126     Convert DNG to PPM with delegate.
127   */
128   image=AcquireImage(image_info);
129   read_info=CloneImageInfo(image_info);
130   (void) InvokeDelegate(read_info,image,"dng:decode",(char *) NULL,exception);
131   image=DestroyImage(image);
132   (void) FormatMagickString(read_info->filename,MaxTextExtent,"%s.png",
133     read_info->unique);
134   sans_exception=AcquireExceptionInfo();
135   image=ReadImage(read_info,sans_exception);
136   sans_exception=DestroyExceptionInfo(sans_exception);
137   if (image == (Image *) NULL)
138     {
139       (void) FormatMagickString(read_info->filename,MaxTextExtent,"%s.ppm",
140         read_info->unique);
141       image=ReadImage(read_info,exception);
142     }
143   (void) RelinquishUniqueFileResource(read_info->filename);
144   if (image != (Image *) NULL)
145     {
146       char
147         filename[MaxTextExtent],
148         *xml;
149
150       ExceptionInfo
151         *sans;
152
153       (void) CopyMagickString(image->magick,read_info->magick,MaxTextExtent);
154       (void) FormatMagickString(filename,MaxTextExtent,"%s.ufraw",
155         read_info->unique);
156       sans=AcquireExceptionInfo();
157       xml=FileToString(filename,MaxTextExtent,sans);
158       (void) RelinquishUniqueFileResource(filename);
159       if (xml != (char *) NULL)
160         {
161           XMLTreeInfo
162            *ufraw;
163
164           /*
165             Inject 
166           */
167           ufraw=NewXMLTree(xml,sans);
168           if (ufraw != (XMLTreeInfo *) NULL)
169             {
170               char
171                 *content,
172                 property[MaxTextExtent];
173
174               const char
175                 *tag;
176
177               XMLTreeInfo
178                 *next;
179
180               if (image->properties == (void *) NULL)
181                 ((Image *) image)->properties=NewSplayTree(
182                   CompareSplayTreeString,RelinquishMagickMemory,
183                   RelinquishMagickMemory);
184               next=GetXMLTreeChild(ufraw,(const char *) NULL);
185               while (next != (XMLTreeInfo *) NULL)
186               {
187                 tag=GetXMLTreeTag(next);
188                 if (tag == (char *) NULL)
189                   tag="unknown";
190                 (void) FormatMagickString(property,MaxTextExtent,"dng:%s",tag);
191                 content=ConstantString(GetXMLTreeContent(next)); 
192                 StripString(content);
193                 if ((LocaleCompare(tag,"log") != 0) &&
194                     (LocaleCompare(tag,"InputFilename") != 0) &&
195                     (LocaleCompare(tag,"OutputFilename") != 0) &&
196                     (LocaleCompare(tag,"OutputType") != 0) &&
197                     (strlen(content) != 0))
198                   (void) AddValueToSplayTree((SplayTreeInfo *)
199                     ((Image *) image)->properties,ConstantString(property),
200                     content);
201                 next=GetXMLTreeSibling(next);
202               }
203               ufraw=DestroyXMLTree(ufraw);
204             }
205           xml=DestroyString(xml);
206         }
207       sans=DestroyExceptionInfo(sans);
208     }
209   read_info=DestroyImageInfo(read_info);
210   return(image);
211 }
212 \f
213 /*
214 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
215 %                                                                             %
216 %                                                                             %
217 %                                                                             %
218 %   R e g i s t e r D N G I m a g e                                           %
219 %                                                                             %
220 %                                                                             %
221 %                                                                             %
222 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
223 %
224 %  RegisterDNGImage() adds attributes for the DNG image format to
225 %  the list of supported formats.  The attributes include the image format
226 %  tag, a method to read and/or write the format, whether the format
227 %  supports the saving of more than one frame to the same file or blob,
228 %  whether the format supports native in-memory I/O, and a brief
229 %  description of the format.
230 %
231 %  The format of the RegisterDNGImage method is:
232 %
233 %      unsigned long RegisterDNGImage(void)
234 %
235 */
236 ModuleExport unsigned long RegisterDNGImage(void)
237 {
238   MagickInfo
239     *entry;
240
241   entry=SetMagickInfo("3FR");
242   entry->decoder=(DecodeImageHandler *) ReadDNGImage;
243   entry->blob_support=MagickFalse;
244   entry->seekable_stream=MagickTrue;
245   entry->format_type=ExplicitFormatType;
246   entry->description=ConstantString("Hasselblad CFV/H3D39II");
247   entry->module=ConstantString("DNG");
248   (void) RegisterMagickInfo(entry);
249   entry=SetMagickInfo("ARW");
250   entry->decoder=(DecodeImageHandler *) ReadDNGImage;
251   entry->blob_support=MagickFalse;
252   entry->seekable_stream=MagickTrue;
253   entry->format_type=ExplicitFormatType;
254   entry->description=ConstantString("Sony Alpha Raw Image Format");
255   entry->module=ConstantString("DNG");
256   (void) RegisterMagickInfo(entry);
257   entry=SetMagickInfo("DNG");
258   entry->decoder=(DecodeImageHandler *) ReadDNGImage;
259   entry->blob_support=MagickFalse;
260   entry->seekable_stream=MagickTrue;
261   entry->format_type=ExplicitFormatType;
262   entry->description=ConstantString("Digital Negative");
263   entry->module=ConstantString("DNG");
264   (void) RegisterMagickInfo(entry);
265   entry=SetMagickInfo("CR2");
266   entry->decoder=(DecodeImageHandler *) ReadDNGImage;
267   entry->blob_support=MagickFalse;
268   entry->seekable_stream=MagickTrue;
269   entry->format_type=ExplicitFormatType;
270   entry->description=ConstantString("Canon Digital Camera Raw Image Format");
271   entry->module=ConstantString("DNG");
272   (void) RegisterMagickInfo(entry);
273   entry=SetMagickInfo("CRW");
274   entry->decoder=(DecodeImageHandler *) ReadDNGImage;
275   entry->blob_support=MagickFalse;
276   entry->seekable_stream=MagickTrue;
277   entry->format_type=ExplicitFormatType;
278   entry->description=ConstantString("Canon Digital Camera Raw Image Format");
279   entry->module=ConstantString("DNG");
280   (void) RegisterMagickInfo(entry);
281   entry=SetMagickInfo("DCR");
282   entry->decoder=(DecodeImageHandler *) ReadDNGImage;
283   entry->blob_support=MagickFalse;
284   entry->seekable_stream=MagickTrue;
285   entry->format_type=ExplicitFormatType;
286   entry->description=ConstantString("Kodak Digital Camera Raw Image File");
287   entry->module=ConstantString("DNG");
288   (void) RegisterMagickInfo(entry);
289   entry=SetMagickInfo("ERF");
290   entry->decoder=(DecodeImageHandler *) ReadDNGImage;
291   entry->blob_support=MagickFalse;
292   entry->seekable_stream=MagickTrue;
293   entry->format_type=ExplicitFormatType;
294   entry->description=ConstantString("Epson RAW Format");
295   entry->module=ConstantString("DNG");
296   (void) RegisterMagickInfo(entry);
297   entry=SetMagickInfo("KDC");
298   entry->decoder=(DecodeImageHandler *) ReadDNGImage;
299   entry->blob_support=MagickFalse;
300   entry->seekable_stream=MagickTrue;
301   entry->format_type=ExplicitFormatType;
302   entry->description=ConstantString("Kodak Digital Camera Raw Image Format");
303   entry->module=ConstantString("DNG");
304   (void) RegisterMagickInfo(entry);
305   entry=SetMagickInfo("K25");
306   entry->decoder=(DecodeImageHandler *) ReadDNGImage;
307   entry->blob_support=MagickFalse;
308   entry->seekable_stream=MagickTrue;
309   entry->format_type=ExplicitFormatType;
310   entry->description=ConstantString("Kodak Digital Camera Raw Image Format");
311   entry->module=ConstantString("DNG");
312   (void) RegisterMagickInfo(entry);
313   entry=SetMagickInfo("MRW");
314   entry->decoder=(DecodeImageHandler *) ReadDNGImage;
315   entry->blob_support=MagickFalse;
316   entry->seekable_stream=MagickTrue;
317   entry->format_type=ExplicitFormatType;
318   entry->description=ConstantString("Sony (Minolta) Raw Image File");
319   entry->module=ConstantString("DNG");
320   (void) RegisterMagickInfo(entry);
321   entry=SetMagickInfo("NEF");
322   entry->decoder=(DecodeImageHandler *) ReadDNGImage;
323   entry->blob_support=MagickFalse;
324   entry->seekable_stream=MagickTrue;
325   entry->format_type=ExplicitFormatType;
326   entry->description=ConstantString("Nikon Digital SLR Camera Raw Image File");
327   entry->module=ConstantString("DNG");
328   (void) RegisterMagickInfo(entry);
329   entry=SetMagickInfo("ORF");
330   entry->decoder=(DecodeImageHandler *) ReadDNGImage;
331   entry->blob_support=MagickFalse;
332   entry->seekable_stream=MagickTrue;
333   entry->format_type=ExplicitFormatType;
334   entry->description=ConstantString("Olympus Digital Camera Raw Image File");
335   entry->module=ConstantString("DNG");
336   (void) RegisterMagickInfo(entry);
337   entry=SetMagickInfo("PEF");
338   entry->decoder=(DecodeImageHandler *) ReadDNGImage;
339   entry->blob_support=MagickFalse;
340   entry->seekable_stream=MagickTrue;
341   entry->format_type=ExplicitFormatType;
342   entry->description=ConstantString("Pentax Electronic File");
343   entry->module=ConstantString("DNG");
344   (void) RegisterMagickInfo(entry);
345   entry=SetMagickInfo("RAF");
346   entry->decoder=(DecodeImageHandler *) ReadDNGImage;
347   entry->blob_support=MagickFalse;
348   entry->seekable_stream=MagickTrue;
349   entry->format_type=ExplicitFormatType;
350   entry->description=ConstantString("Fuji CCD-RAW Graphic File");
351   entry->module=ConstantString("DNG");
352   (void) RegisterMagickInfo(entry);
353   entry=SetMagickInfo("SRF");
354   entry->decoder=(DecodeImageHandler *) ReadDNGImage;
355   entry->blob_support=MagickFalse;
356   entry->seekable_stream=MagickTrue;
357   entry->format_type=ExplicitFormatType;
358   entry->description=ConstantString("Sony Raw Format");
359   entry->module=ConstantString("DNG");
360   (void) RegisterMagickInfo(entry);
361   entry=SetMagickInfo("SR2");
362   entry->decoder=(DecodeImageHandler *) ReadDNGImage;
363   entry->blob_support=MagickFalse;
364   entry->seekable_stream=MagickTrue;
365   entry->format_type=ExplicitFormatType;
366   entry->description=ConstantString("Sony Raw Format 2");
367   entry->module=ConstantString("DNG");
368   (void) RegisterMagickInfo(entry);
369   entry=SetMagickInfo("X3F");
370   entry->decoder=(DecodeImageHandler *) ReadDNGImage;
371   entry->blob_support=MagickFalse;
372   entry->seekable_stream=MagickTrue;
373   entry->format_type=ExplicitFormatType;
374   entry->description=ConstantString("Sigma Camera RAW Picture File");
375   entry->module=ConstantString("DNG");
376   (void) RegisterMagickInfo(entry);
377   return(MagickImageCoderSignature);
378 }
379 \f
380 /*
381 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
382 %                                                                             %
383 %                                                                             %
384 %                                                                             %
385 %   U n r e g i s t e r D N G I m a g e                                       %
386 %                                                                             %
387 %                                                                             %
388 %                                                                             %
389 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
390 %
391 %  UnregisterDNGImage() removes format registrations made by the
392 %  BIM module from the list of supported formats.
393 %
394 %  The format of the UnregisterBIMImage method is:
395 %
396 %      UnregisterDNGImage(void)
397 %
398 */
399 ModuleExport void UnregisterDNGImage(void)
400 {
401   (void) UnregisterMagickInfo("X3F");
402   (void) UnregisterMagickInfo("SR2");
403   (void) UnregisterMagickInfo("SRF");
404   (void) UnregisterMagickInfo("RAF");
405   (void) UnregisterMagickInfo("PEF");
406   (void) UnregisterMagickInfo("ORF");
407   (void) UnregisterMagickInfo("NEF");
408   (void) UnregisterMagickInfo("MRW");
409   (void) UnregisterMagickInfo("K25");
410   (void) UnregisterMagickInfo("KDC");
411   (void) UnregisterMagickInfo("DCR");
412   (void) UnregisterMagickInfo("CRW");
413   (void) UnregisterMagickInfo("CR2");
414   (void) UnregisterMagickInfo("DNG");
415   (void) UnregisterMagickInfo("ARW");
416   (void) UnregisterMagickInfo("3FR");
417 }