1 /*
2  * in_tiff.cpp -- read TIFF (Tag Image File Format) files with tif22pnm
3  * by pts@fazekas.hu at Sun Apr 14 14:50:30 CEST 2002
4  */
5 /* Imp: get docs about the TIFF format, and rewrite this from scratch */
6 /* Imp: use xviff.c */
7 
8 #ifdef __GNUC__
9 #ifndef __clang__
10 #pragma implementation
11 #endif
12 #endif
13 
14 #include "image.hpp"
15 
16 #if USE_IN_TIFF
17 
18 #include "error.hpp"
19 #include "gensio.hpp"
20 #include "helpere.hpp"
21 #include <string.h> /* memchr() */
22 #include <stdio.h> /* printf() */
23 
in_tiff_reader(Image::Loader::UFD * ufd,SimBuffer::Flat const &)24 static Image::Sampled *in_tiff_reader(Image::Loader::UFD *ufd, SimBuffer::Flat const&) {
25   // Error::sev(Error::EERROR) << "Cannot load TIFF images yet." << (Error*)0;
26   // HelperE helper("tifftopnm %S"); /* Cannot extract alpha channel */
27   // HelperE helper("tif22pnm -rgba %S"); /* tif22pnm <= 0.07 */
28   char const *cmd=
29   #if OS_COTY==COTY_WIN9X || OS_COTY==COTY_WINNT
30 #if 0 /* unsafe if both tifftopnm and png22pnm exist */
31     "tifftopnm <%S >%D\ntif22pnm -rgba %S pnm: %D"; /* slow but safe */
32 #else
33     "tif22pnm -rgba %S pnm:";
34 #endif
35   #else
36     #if OS_COTY==COTY_UNIX
37       #if 1
38         "(tif22pnm -rgba %S pnm: || tifftopnm %S)";
39       #else
40         /* Dat: not using this to suppress `sh: tif22pnm: command not found', because
41          * it would hide precious error messages printed by tif22pnm.
42          */
43          "((tif22pnm -rgba %S pnm: 2>/dev/null)|| tifftopnm %S)";
44       #endif
45     #else
46       "tif22pnm -rgba %S pnm:"; /* Wants to seek in the file. */
47     #endif
48   #endif
49   HelperE helper(cmd);
50   Encoder::writeFrom(*(Filter::PipeE*)&helper, *(Filter::UngetFILED*)ufd);
51   ((Filter::PipeE*)&helper)->vi_write(0,0); /* Signal EOF */
52   return helper.getImg();
53 }
54 
in_tiff_checker(char buf[Image::Loader::MAGIC_LEN],char[Image::Loader::MAGIC_LEN],SimBuffer::Flat const &,Image::Loader::UFD *)55 static Image::Loader::reader_t in_tiff_checker(char buf[Image::Loader::MAGIC_LEN], char [Image::Loader::MAGIC_LEN], SimBuffer::Flat const&, Image::Loader::UFD*) {
56   /* MM\x00\x2a: TIFF image data, big-endian
57    * II\x2a\x00: TIFF image data, little-endian
58    * The second word of TIFF files is the TIFF version number, 42, which has
59    * never changed.  The TIFF specification recommends testing for it.
60    */
61   return (0==memcmp(buf,"MM\x00\x2a",4) || 0==memcmp(buf,"II\x2a\x00",4)) ? in_tiff_reader : 0;
62 }
63 
64 #else
65 #define in_tiff_checker (Image::Loader::checker_t)NULLP
66 #endif /* USE_IN_XPM */
67 
68 Image::Loader in_tiff_loader = { "TIFF", in_tiff_checker, 0 };
69