summaryrefslogtreecommitdiffstats
path: root/kfaxview/libkfaximage/kfaximage.cpp
blob: 6da8486cb82ae505072e028bdf312f33f7de4e76 (plain)
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
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
/* 
   This file is part of KDE FAX image library
   Copyright (c) 2005 Helge Deller <deller@kde.org>

   based on Frank D. Cringle's viewfax package
   Copyright (C) 1990, 1995  Frank D. Cringle.

   This library is free software; you can redistribute it and/or
   modify it under the terms of the GNU Library General Public
   License version 2 as published by the Free Software Foundation.
   
   This library is distributed in the hope that it will be useful,
   but WITHOUT ANY WARRANTY; without even the implied warranty of
   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
   Library General Public License for more details.
   
   You should have received a copy of the GNU Library General Public License
   along with this library; see the file COPYING.LIB.  If not, write to
   the Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor,
   Boston, MA 02110-1301, USA.
*/

#include <config.h>

#include <stdlib.h>

#include <tqimage.h>
#include <tqfile.h>

#include <tdeglobal.h>
#include <tdelocale.h>
#include <kdebug.h>

#include "faxexpand.h"
#include "kfaximage.h"

static const char FAXMAGIC[]   = "\000PC Research, Inc\000\000\000\000\000\000";
static const char littleTIFF[] = "\x49\x49\x2a\x00";
static const char bigTIFF[]    = "\x4d\x4d\x00\x2a";

KFaxImage::KFaxImage( const TQString &filename, TQObject *parent, const char *name )
   : TQObject(parent,name)
{
  TDEGlobal::locale()->insertCatalogue( TQString::fromLatin1("libkfaximage") );
  loadImage(filename);
}

KFaxImage::~KFaxImage()
{ }

bool KFaxImage::loadImage( const TQString &filename )
{
  reset();

  m_filename = filename;
  m_errorString = TQString();

  if (m_filename.isEmpty())
	return false;

  int ok = notetiff();
  if (!ok) {
	reset();
  }
  return ok == 1;
}

void KFaxImage::reset()
{
  fax_init_tables();
  m_pagenodes.setAutoDelete(true);
  m_pagenodes.clear();
  // do not reset m_errorString and m_filename, since 
  // they may be needed by calling application 
}

TQImage KFaxImage::page( unsigned int pageNr )
{
  if (pageNr >= numPages()) {
    kdDebug() << "KFaxImage::page() called with invalid page number\n";
    return TQImage();
  }
  pagenode *pn = m_pagenodes.at(pageNr);
  GetImage(pn);
  return pn->image;
}

TQPoint KFaxImage::page_dpi( unsigned int pageNr )
{
  if (pageNr >= numPages()) {
    kdDebug() << "KFaxImage::page_dpi() called with invalid page number\n";
    return TQPoint(0,0);
  }
  pagenode *pn = m_pagenodes.at(pageNr);
  GetImage(pn);
  return pn->dpi;
}

TQSize KFaxImage::page_size( unsigned int pageNr )
{
  if (pageNr >= numPages()) {
    kdDebug() << "KFaxImage::page_size() called with invalid page number\n";
    return TQSize(0,0);
  }
  pagenode *pn = m_pagenodes.at(pageNr);
  GetImage(pn);
  return pn->size;
}


pagenode *KFaxImage::AppendImageNode(int type)
{
    pagenode *pn = new pagenode();
    if (pn) {
	pn->type = type;
        pn->expander = g31expand;
	pn->strips = NULL;
	pn->size = TQSize(1728,2339);
	pn->vres = -1;
	pn->dpi = KFAX_DPI_FINE;
	m_pagenodes.append(pn);
    }
    return pn;
}

bool KFaxImage::NewImage(pagenode *pn, int w, int h)
{
    pn->image = TQImage( w, h, 1, 2, TQImage::systemByteOrder() );
    pn->image.setColor(0, tqRgb(255,255,255));
    pn->image.setColor(1, tqRgb(0,0,0));
    pn->data = (TQ_UINT16*) pn->image.bits();
    pn->bytes_per_line = pn->image.bytesPerLine();
    pn->dpi = KFAX_DPI_FINE;

    return !pn->image.isNull();
}

void KFaxImage::FreeImage(pagenode *pn)
{
    pn->image = TQImage();
    pn->data = NULL;
    pn->bytes_per_line = 0;
}

void KFaxImage::kfaxerror(const TQString& error)
{
    m_errorString = error;
    kdError() << "kfaxerror: " << error << endl;
}


/* Enter an argument in the linked list of pages */
pagenode *
KFaxImage::notefile(int type)
{
    pagenode *newnode = new pagenode();
    newnode->type = type;
    newnode->size = TQSize(1728,0);
    return newnode;
}

static t32bits
get4(unsigned char *p, TQImage::Endian endian)
{
    return (endian == TQImage::BigEndian) 
	? (p[0]<<24)|(p[1]<<16)|(p[2]<<8)|p[3] :
	   p[0]|(p[1]<<8)|(p[2]<<16)|(p[3]<<24);
}

static int
get2(unsigned char *p, TQImage::Endian endian)
{
    return (endian == TQImage::BigEndian) ? (p[0]<<8)|p[1] : p[0]|(p[1]<<8);
}

/* generate pagenodes for the images in a tiff file */
int
KFaxImage::notetiff()
{
#define SC(x) (char *)(x)
    unsigned char header[8];
    TQImage::Endian endian;
    t32bits IFDoff;
    pagenode *pn = NULL;
    TQString str;

    TQFile file(filename());
    if (!file.open(IO_ReadOnly)) {
	kfaxerror(i18n("Unable to open file for reading."));
	return 0;
    }

    if (file.readBlock(SC(header), 8) != 8) {
	kfaxerror(i18n("Unable to read file header (file too short)."));
	return 0;
    }
    if (memcmp(header, &littleTIFF, 4) == 0)
	endian = TQImage::LittleEndian;
    else if (memcmp(header, &bigTIFF, 4) == 0)
	endian = TQImage::BigEndian;
    else {
 maybe_RAW_FAX:
        kfaxerror(i18n("This is not a TIFF FAX file."));
        // AppendImageNode(FAX_RAW);
	return 0;
    }
    IFDoff = get4(header+4, endian);
    if (IFDoff & 1) {
	goto maybe_RAW_FAX;
    }
    do {			/* for each page */
	unsigned char buf[8];
	unsigned char *dir = NULL , *dp = NULL;
	int ndirent;
	pixnum iwidth = 1728;
	pixnum iheight = 2339;
	int inverse = false;
	int lsbfirst = 0;
	int t4opt = 0, comp = 0;
	int orient = TURN_NONE;
	int yres = 196; /* 98.0 */
	struct strip *strips = NULL;
	unsigned int rowsperstrip = 0;
	t32bits nstrips = 1;

	if (!file.at(IFDoff)) {
	realbad:
	  kfaxerror( i18n("Invalid or incomplete TIFF file.") );
	bad:
	    if (strips)
		free(strips);
	    if (dir)
		free(dir);
	    file.close();
	    return 1;
	}
	if (file.readBlock(SC(buf), 2) != 2)
	    goto realbad;
	ndirent = get2(buf, endian);
        int len = 12*ndirent+4;
	dir = (unsigned char *) malloc(len);
	if (file.readBlock(SC(dir), len) != len)
	    goto realbad;
	for (dp = dir; ndirent; ndirent--, dp += 12) {
	    /* for each directory entry */
	    int tag, ftype;
	    t32bits count, value = 0;
	    tag = get2(dp, endian);
	    ftype = get2(dp+2, endian);
	    count = get4(dp+4, endian);
	    switch(ftype) {	/* value is offset to list if count*size > 4 */
	    case 3:		/* short */
		value = get2(dp+8, endian);
		break;
	    case 4:		/* long */
		value = get4(dp+8, endian);
		break;
	    case 5:		/* offset to rational */
		value = get4(dp+8, endian);
		break;
	    }
	    switch(tag) {
	    case 256:		/* ImageWidth */
		iwidth = value;
		break;
	    case 257:		/* ImageLength */
		iheight = value;
		break;
	    case 259:		/* Compression */
		comp = value;
		break;
	    case 262:		/* PhotometricInterpretation */
		inverse ^= (value == 1);
		break;
	    case 266:		/* FillOrder */
		lsbfirst = (value == 2);
		break;
	    case 273:		/* StripOffsets */
		nstrips = count;
		strips = (struct strip *) malloc(count * sizeof *strips);
		if (count == 1 || (count == 2 && ftype == 3)) {
		    strips[0].offset = value;
		    if (count == 2)
			strips[1].offset = get2(dp+10, endian);
		    break;
		}
		if (!file.at(value))
		    goto realbad;
		for (count = 0; count < nstrips; count++) {
		    if (file.readBlock(SC(buf), (ftype == 3) ? 2 : 4) <= 0)
			goto realbad;
		    strips[count].offset = (ftype == 3) ?
			get2(buf, endian) : get4(buf, endian);
		}
		break;
	    case 274:		/* Orientation */
		switch(value) {
		default:	/* row0 at top,    col0 at left   */
		    orient = 0;
		    break;
		case 2:		/* row0 at top,    col0 at right  */
		    orient = TURN_M;
		    break;
		case 3:		/* row0 at bottom, col0 at right  */
		    orient = TURN_U;
		    break;
		case 4:		/* row0 at bottom, col0 at left   */
		    orient = TURN_U|TURN_M;
		    break;
		case 5:		/* row0 at left,   col0 at top    */
		    orient = TURN_M|TURN_L;
		    break;
		case 6:		/* row0 at right,  col0 at top    */
		    orient = TURN_U|TURN_L;
		    break;
		case 7:		/* row0 at right,  col0 at bottom */
		    orient = TURN_U|TURN_M|TURN_L;
		    break;
		case 8:		/* row0 at left,   col0 at bottom */
		    orient = TURN_L;
		    break;
		}
		break;
	    case 278:		/* RowsPerStrip */
		rowsperstrip = value;	
		break;
	    case 279:		/* StripByteCounts */
		if (count != nstrips) {
		  str = i18n("In file %1\nStripsPerImage tag 273=%2,tag279=%3\n")
			      .arg(filename()).arg(nstrips).arg(count);
		  kfaxerror(str);
		  goto realbad;
		}
		if (count == 1 || (count == 2 && ftype == 3)) {
		    strips[0].size = value;
		    if (count == 2)
			strips[1].size = get2(dp+10, endian);
		    break;
		}
		if (!file.at(value))
		    goto realbad;
		for (count = 0; count < nstrips; count++) {
		    if (file.readBlock(SC(buf), (ftype == 3) ? 2 : 4) <= 0)
			goto realbad;
		    strips[count].size = (ftype == 3) ?
			get2(buf, endian) : get4(buf, endian);
		}
		break;
	    case 283:		/* YResolution */
		if (!file.at(value) ||
		    file.readBlock(SC(buf), 8) != 8)
		    goto realbad;
		yres = get4(buf, endian) / get4(buf+4, endian);
		break;
	    case 292:		/* T4Options */
		t4opt = value;
		break;
	    case 293:		/* T6Options */
		/* later */
		break;
	    case 296:		/* ResolutionUnit */
		if (value == 3)
		    yres = (yres * 254) / 100;  /* *2.54 */
		break;
	    }
	}
	IFDoff = get4(dp, endian);
	free(dir);
	dir = NULL;
	if (comp == 5) {
          // compression type 5 is LZW compression // XXX
	  kfaxerror(i18n("Due to patent reasons LZW (Lempel-Ziv & Welch) "
		    "compressed Fax files cannot be loaded yet.\n"));
	    goto bad;
	}
	if (comp < 2 || comp > 4) {
	  kfaxerror(i18n("This version can only handle Fax files\n"));
	    goto bad;
	}
	pn = AppendImageNode(FAX_TIFF);
	pn->nstrips = nstrips;
	pn->rowsperstrip = nstrips > 1 ? rowsperstrip : iheight;
	pn->strips = strips;
	pn->size = TQSize(iwidth,iheight);
	pn->inverse = inverse;
	pn->lsbfirst = lsbfirst;
	pn->orient = orient;
	pn->dpi.setY(yres);
	pn->vres = (yres > 150) ? 1:0; /* arbitrary threshold for fine resolution */
	if (comp == 2)
	    pn->expander = MHexpand;
	else if (comp == 3)
	    pn->expander = (t4opt & 1) ? g32expand : g31expand;
	else
	    pn->expander = g4expand;
    } while (IFDoff);
    file.close();
    return 1;
#undef UC
}

/* report error and remove bad file from the list */
void
KFaxImage::badfile(pagenode *pn)
{
  kfaxerror(i18n("%1: Bad Fax File").arg(filename()));
  FreeImage(pn);
}

/* rearrange input bits into t16bits lsb-first chunks */
static void
normalize(pagenode *pn, int revbits, int swapbytes, size_t length)
{
    t32bits *p = (t32bits *) pn->data;

    kdDebug() << "normalize = " << ((revbits<<1)|swapbytes) << endl;
    switch ((revbits<<1)|swapbytes) {
    case 0:
	break;
    case 1:
	for ( ; length; length -= 4) {
	    t32bits t = *p;
	    *p++ = ((t & 0xff00ff00) >> 8) | ((t & 0x00ff00ff) << 8);
	}
	break;
    case 2:
	for ( ; length; length -= 4) {
	    t32bits t = *p;
	    t = ((t & 0xf0f0f0f0) >> 4) | ((t & 0x0f0f0f0f) << 4);
	    t = ((t & 0xcccccccc) >> 2) | ((t & 0x33333333) << 2);
	    *p++ = ((t & 0xaaaaaaaa) >> 1) | ((t & 0x55555555) << 1);
	}
	break;
    case 3:
	for ( ; length; length -= 4) {
	    t32bits t = *p;
	    t = ((t & 0xff00ff00) >> 8) | ((t & 0x00ff00ff) << 8);
	    t = ((t & 0xf0f0f0f0) >> 4) | ((t & 0x0f0f0f0f) << 4);
	    t = ((t & 0xcccccccc) >> 2) | ((t & 0x33333333) << 2);
	    *p++ = ((t & 0xaaaaaaaa) >> 1) | ((t & 0x55555555) << 1);
	}
    }
}


/* get compressed data into memory */
unsigned char *
KFaxImage::getstrip(pagenode *pn, int strip)
{
    size_t offset, roundup;
    unsigned char *Data;

    union { t16bits s; unsigned char b[2]; } so;
#define ShortOrder	so.b[1]
    so.s = 1;  /* XXX */

    TQFile file(filename());
    if (!file.open(IO_ReadOnly)) {
	badfile(pn);
	return NULL;
    }

    if (pn->strips == NULL) {
	offset = 0;
	pn->length = file.size();
    }
    else if (strip < pn->nstrips) {
	offset = pn->strips[strip].offset;
	pn->length = pn->strips[strip].size;
    }
    else {
      kfaxerror( i18n("Trying to expand too many strips.") );
      return NULL;
    }

    /* round size to full boundary plus t32bits */
    roundup = (pn->length + 7) & ~3;

    Data = (unsigned char *) malloc(roundup);
    /* clear the last 2 t32bits, to force the expander to terminate
       even if the file ends in the middle of a fax line  */
    *((t32bits *) Data + roundup/4 - 2) = 0;
    *((t32bits *) Data + roundup/4 - 1) = 0;

    /* we expect to get it in one gulp... */
    if (!file.at(offset) ||
	(size_t) file.readBlock((char *)Data, pn->length) != pn->length) { 
	badfile(pn);
	free(Data);
	return NULL;
    }
    file.close();

    pn->data = (t16bits *) Data;
    if (pn->strips == NULL && memcmp(Data, FAXMAGIC, sizeof(FAXMAGIC)-1) == 0) {
	/* handle ghostscript / PC Research fax file */
      if (Data[24] != 1 || Data[25] != 0){
	kfaxerror( i18n("Only the first page of the PC Research multipage file will be shown.") );
      }
	pn->length -= 64;
	pn->vres = Data[29];
	pn->data += 32;
	roundup -= 64;
    }

    normalize(pn, !pn->lsbfirst, ShortOrder, roundup);
    if (pn->size.height() == 0)
	pn->size.setHeight( G3count(pn, pn->expander == g32expand) );
    if (pn->size.height() == 0) {

      kfaxerror( i18n("No fax found in file.") );
      badfile(pn);
      free(Data);
      return NULL;
    }
    if (pn->strips == NULL)
	pn->rowsperstrip = pn->size.height();
    return Data;
}


static void
drawline(pixnum *run, int LineNum, pagenode *pn)
{
    t32bits *p, *p1;		/* p - current line, p1 - low-res duplicate */
    pixnum *r;			/* pointer to run-lengths */
    t32bits pix;		/* current pixel value */
    t32bits acc;		/* pixel accumulator */
    int nacc;			/* number of valid bits in acc */
    int tot;			/* total pixels in line */
    int n;

    LineNum += pn->stripnum * pn->rowsperstrip;
    if (LineNum >= pn->size.height()) {
       if (LineNum == pn->size.height())
           kdError() << "Height exceeded\n";
       return;
    }
    
    p = (t32bits *) pn->image.scanLine(LineNum*(2-pn->vres));
    p1 =(t32bits *)( pn->vres ? 0 : pn->image.scanLine(1+LineNum*(2-pn->vres)));
    r = run;
    acc = 0;
    nacc = 0;
    pix = pn->inverse ? ~0 : 0;
    tot = 0;
    while (tot < pn->size.width()) {
	n = *r++;
	tot += n;
        /* Watch out for buffer overruns, e.g. when n == 65535.  */
        if (tot > pn->size.width())
            break;
	if (pix)
	    acc |= (~(t32bits)0 >> nacc);
	else if (nacc)
	    acc &= (~(t32bits)0 << (32 - nacc));
	else
	    acc = 0;
	if (nacc + n < 32) {
	    nacc += n;
	    pix = ~pix;
	    continue;
	}
	*p++ = acc;
	if (p1)
	    *p1++ = acc;
	n -= 32 - nacc;
	while (n >= 32) {
	    n -= 32;
	    *p++ = pix;
	    if (p1)
		*p1++ = pix;
	}
	acc = pix;
	nacc = n;
	pix = ~pix;
    }
    if (nacc) {
	*p++ = acc;
	if (p1)
	    *p1++ = acc;
    }
}

int
KFaxImage::GetPartImage(pagenode *pn, int n)
{
    unsigned char *Data = getstrip(pn, n);
    if (Data == 0)
	return 3;
    pn->stripnum = n;
    (*pn->expander)(pn, drawline);
    free(Data);
    return 1;
}

int 
KFaxImage::GetImage(pagenode *pn)
{
    if (!pn->image.isNull())
	return 1;

    int i;
    if (pn->strips == 0) {

        kdDebug() << "Loading RAW fax file " << m_filename << " size=" << pn->size << endl;

	/* raw file; maybe we don't have the height yet */
	unsigned char *Data = getstrip(pn, 0);
	if (Data == 0){
	  return 0;
	}
	if (!NewImage(pn, pn->size.width(), (pn->vres ? 1:2) * pn->size.height()))
	  return 0;

	(*pn->expander)(pn, drawline);
    }
    else {
	/* multi-strip tiff */
        kdDebug() << "Loading MULTI STRIP TIFF fax file " << m_filename << endl;

	if (!NewImage(pn, pn->size.width(), (pn->vres ? 1:2) * pn->size.height()))
	  return 0;
	pn->stripnum = 0;
	kdDebug() << "has " << pn->nstrips << " strips.\n";
	for (i = 0; i < pn->nstrips; i++){

	  int k = GetPartImage(pn, i); 
	  if ( k == 3 ){
	    FreeImage(pn);
            kfaxerror( i18n("Fax G3 format not yet supported.") );
	    return k;
	  }

	}
    }

    // byte-swapping the image on little endian machines
#if defined(TQ_BYTE_ORDER) && (TQ_BYTE_ORDER == TQ_LITTLE_ENDIAN)
    for (int y=pn->image.height()-1; y>=0; --y) {
      TQ_UINT32 *source = (TQ_UINT32 *) pn->image.scanLine(y);
      TQ_UINT32 *dest   = source;
      for (int x=(pn->bytes_per_line/4)-1; x>=0; --x) {
 	TQ_UINT32 dv = 0, sv = *source;
	for (int bit=32; bit>0; --bit) {
		dv <<= 1;
		dv |= sv&1;
		sv >>= 1;
	}
        *dest = dv;
	++dest;
	++source;
      }
    }
#endif

    kdDebug() << filename()
	<< "\n\tsize = " << pn->size
	<< "\n\tDPI = " << pn->dpi
	<< "\n\tresolution = " << (pn->vres ? "fine" : "normal")
	<< endl;

    return 1;
}


#include "kfaximage.moc"