most numeric parameters to the XS implementation now throw an exception if supplied...
[imager.git] / design / fax.diff
CommitLineData
c2c42e64
TC
1? fax.diff
2? devel/lib/Imager/Regops.pm
3Index: devel/Imager.pm
4===================================================================
5RCS file: /u02/cvsroot/Imager/devel/Imager.pm,v
6retrieving revision 1.12
7diff -u -r1.12 Imager.pm
8--- devel/Imager.pm 2001/05/05 14:40:23 1.12
9+++ devel/Imager.pm 2001/05/07 12:58:59
10@@ -81,6 +81,7 @@
11
12 i_readtiff_wiol
13 i_writetiff_wiol
14+ i_writetiff_wiol_faxable
15
16 i_readpng
17 i_writepng
18@@ -548,6 +549,7 @@
19 sub write {
20 my $self = shift;
21 my %input=(jpegquality=>75, gifquant=>'mc', lmdither=>6.0, lmfixed=>[], @_);
22+
23 my ($fh, $rc, $fd, $IO);
24
25 my %iolready=( tiff=>1 ); # this will be SO MUCH BETTER once they are all in there
26@@ -572,7 +574,6 @@
27 }
28
29
30-
31 if ($iolready{$input{type}}) {
32 if ($fd) {
33 $IO = io_new_fd($fd);
34@@ -582,6 +583,11 @@
35 if (!i_writetiff_wiol($self->{IMG}, $IO)) { $self->{ERRSTR}='Could not write to buffer'; return undef; }
36 }
37
38+ if ($input{type} eq 'fax') {
39+ print STDERR "writing a faxable; TIFF\n";
40+ if (!i_writetiff_wiol_faxable($self->{IMG}, $IO)) { $self->{ERRSTR}='Could not write to buffer'; return undef; }
41+ }
42+
43 my $data = io_slurp($IO);
44 if (!$data) { $self->{ERRSTR}='Could not slurp from buffer'; return undef; }
45
46@@ -668,6 +674,13 @@
47 $self->{DEBUG} && print "writing a raw file\n";
48 } elsif ( $input{type} eq 'tiff' ) {
49 $rc=i_writetiff_wiol($self->{IMG},io_new_fd($fd) );
50+ if ( !defined($rc) ) {
51+ $self->{ERRSTR}='unable to write tiff image'; return undef;
52+ }
53+ $self->{DEBUG} && print "writing a tiff file\n";
54+ } elsif ( $input{type} eq 'fax' ) {
55+ print STDERR "writing a faxable tiff (2)\n";
56+ $rc=i_writetiff_wiol_faxable($self->{IMG},io_new_fd($fd) );
57 if ( !defined($rc) ) {
58 $self->{ERRSTR}='unable to write tiff image'; return undef;
59 }
60Index: devel/Imager.xs
61===================================================================
62RCS file: /u02/cvsroot/Imager/devel/Imager.xs,v
63retrieving revision 1.9
64diff -u -r1.9 Imager.xs
65--- devel/Imager.xs 2001/05/04 07:15:09 1.9
66+++ devel/Imager.xs 2001/05/07 12:59:02
67@@ -1023,6 +1023,11 @@
68 Imager::ImgRaw im
69 Imager::IO ig
70
71+undef_int
72+i_writetiff_wiol_faxable(im, ig)
73+ Imager::ImgRaw im
74+ Imager::IO ig
75+
76
77 #endif /* HAVE_LIBTIFF */
78
79Index: devel/feat.h
80===================================================================
81RCS file: /u02/cvsroot/Imager/devel/feat.h,v
82retrieving revision 1.1.1.1
83diff -u -r1.1.1.1 feat.h
84--- devel/feat.h 2001/03/25 11:55:00 1.1.1.1
85+++ devel/feat.h 2001/05/07 12:59:02
86@@ -6,6 +6,7 @@
87 #endif
88 #ifdef HAVE_LIBTIFF
89 "tiff",
90+ "fax",
91 #endif
92 #ifdef HAVE_LIBPNG
93 "png",
94Index: devel/tiff.c
95===================================================================
96RCS file: /u02/cvsroot/Imager/devel/tiff.c,v
97retrieving revision 1.1.1.1
98diff -u -r1.1.1.1 tiff.c
99--- devel/tiff.c 2001/03/25 11:55:00 1.1.1.1
100+++ devel/tiff.c 2001/05/07 12:59:03
101@@ -344,3 +344,126 @@
102 return 1;
103 }
104
105+undef_int
106+i_writetiff_wiol_faxable(i_img *im, io_glue *ig) {
107+ uint32 width, height;
108+ i_color val;
109+ unsigned char *linebuf = NULL;
110+ uint32 y;
111+ int linebufpos;
112+ int ch, ci, rc;
113+ uint32 x;
114+ TIFF* tif;
115+ int luma_channel;
116+ uint32 rowsperstrip;
117+
118+ width = im->xsize;
119+ height = im->ysize;
120+
121+ switch (im->channels) {
122+ case 1:
123+ luma_channel = 0;
124+ break;
125+ case 3:
126+ luma_channel = 1;
127+ break;
128+ default:
129+ /* This means a colorspace we don't handle yet */
130+ mm_log((1, "i_writetiff_wiol_faxable: don't handle %d channel images.\n", im->channels));
131+ return 0;
132+ }
133+
134+ /* Add code to get the filename info from the iolayer */
135+ /* Also add code to check for mmapped code */
136+
137+ io_glue_commit_types(ig);
138+ mm_log((1, "i_writetiff_wiol_faxable(im 0x%p, ig 0x%p)\n", im, ig));
139+
140+ /* FIXME: Enable the mmap interface */
141+
142+ tif = TIFFClientOpen("No name",
143+ "wm",
144+ (thandle_t) ig,
145+ (TIFFReadWriteProc) ig->readcb,
146+ (TIFFReadWriteProc) ig->writecb,
147+ (TIFFSeekProc) comp_seek,
148+ (TIFFCloseProc) ig->closecb,
149+ (TIFFSizeProc) ig->sizecb,
150+ (TIFFMapFileProc) NULL,
151+ (TIFFUnmapFileProc) NULL);
152+
153+ if (!tif) {
154+ mm_log((1, "i_writetiff_wiol_faxable: Unable to open tif file for writing\n"));
155+ return 0;
156+ }
157+
158+ mm_log((1, "i_writetiff_wiol_faxable: width=%d, height=%d, channels=%d\n", width, height, im->channels));
159+
160+ if (!TIFFSetField(tif, TIFFTAG_IMAGEWIDTH, width) )
161+ { mm_log((1, "i_writetiff_wiol_faxable: TIFFSetField width=%d failed\n", width)); return 0; }
162+ if (!TIFFSetField(tif, TIFFTAG_IMAGELENGTH, height) )
163+ { mm_log((1, "i_writetiff_wiol_faxable: TIFFSetField length=%d failed\n", height)); return 0; }
164+ if (!TIFFSetField(tif, TIFFTAG_SAMPLESPERPIXEL, 1))
165+ { mm_log((1, "i_writetiff_wiol_faxable: TIFFSetField samplesperpixel=1 failed\n")); return 0; }
166+ if (!TIFFSetField(tif, TIFFTAG_ORIENTATION, ORIENTATION_TOPLEFT))
167+ { mm_log((1, "i_writetiff_wiol_faxable: TIFFSetField Orientation=topleft\n")); return 0; }
168+ if (!TIFFSetField(tif, TIFFTAG_BITSPERSAMPLE, 1) )
169+ { mm_log((1, "i_writetiff_wiol_faxable: TIFFSetField bitpersample=1\n")); return 0; }
170+ if (!TIFFSetField(tif, TIFFTAG_PLANARCONFIG, PLANARCONFIG_CONTIG))
171+ { mm_log((1, "i_writetiff_wiol_faxable: TIFFSetField planarconfig\n")); return 0; }
172+ if (!TIFFSetField(tif, TIFFTAG_PHOTOMETRIC, PHOTOMETRIC_MINISBLACK))
173+ { mm_log((1, "i_writetiff_wiol_faxable: TIFFSetField photometric=%d\n", PHOTOMETRIC_MINISBLACK)); return 0; }
174+ if (!TIFFSetField(tif, TIFFTAG_COMPRESSION, 3))
175+ { mm_log((1, "i_writetiff_wiol_faxable: TIFFSetField compression=3\n")); return 0; }
176+
177+ linebuf = (unsigned char *)_TIFFmalloc( TIFFScanlineSize(tif) );
178+
179+ if (!TIFFSetField(tif, TIFFTAG_ROWSPERSTRIP, TIFFDefaultStripSize(tif, -1))) {
180+ mm_log((1, "i_writetiff_wiol_faxable: TIFFSetField rowsperstrip=-1\n")); return 0; }
181+
182+ TIFFGetField(tif, TIFFTAG_ROWSPERSTRIP, &rowsperstrip);
183+ TIFFGetField(tif, TIFFTAG_ROWSPERSTRIP, &rc);
184+
185+ mm_log((1, "i_writetiff_wiol_faxable: TIFFGetField rowsperstrip=%d\n", rowsperstrip));
186+ mm_log((1, "i_writetiff_wiol_faxable: TIFFGetField scanlinesize=%d\n", TIFFScanlineSize(tif) ));
187+ mm_log((1, "i_writetiff_wiol_faxable: TIFFGetField planarconfig=%d == %d\n", rc, PLANARCONFIG_CONTIG));
188+
189+ /*
190+ if (!TIFFSetField(tif, TIFFTAG_XRESOLUTION, 204))
191+ { mm_log((1, "i_writetiff_wiol_faxable: TIFFSetField Xresolution=204\n")); return 0; }
192+ if (!TIFFSetField(tif, TIFFTAG_YRESOLUTION, 196))
193+ { mm_log((1, "i_writetiff_wiol_faxable: TIFFSetField Yresolution=196\n")); return 0; }
194+ if (!TIFFSetField(tif, TIFFTAG_RESOLUTIONUNIT, RESUNIT_INCH)) {
195+ mm_log((1, "i_writetiff_wiol_faxable: TIFFSetField ResolutionUnit=%d\n", RESUNIT_INCH)); return 0;
196+ }
197+ */
198+
199+ for (y=0; y<height; y++) {
200+ int linebufpos=0;
201+ for(x=0; x<width; x+=8) {
202+ int bits;
203+ int bitpos;
204+ uint8 bitval = 128;
205+ linebuf[linebufpos]=0;
206+ bits = width-x; if(bits>8) bits=8;
207+ for(bitpos=0;bitpos<bits;bitpos++) {
208+ int luma;
209+ luma = im->data[(x+bitpos+y*im->xsize)*im->channels+luma_channel];
210+ linebuf[linebufpos] |= ((luma>=128)?bitval:0);
211+ bitval >>= 1;
212+ }
213+ linebufpos++;
214+ }
215+ if (TIFFWriteScanline(tif, linebuf, y, 0) < 0) {
216+ mm_log((1, "i_writetiff_wiol_faxable: TIFFWriteScanline failed.\n"));
217+ break;
218+ }
219+ }
220+ (void) TIFFClose(tif);
221+ if (linebuf) _TIFFfree(linebuf);
222+ return 1;
223+}
224+
225+
226+
227+