File: sfffile.cpp

package info (click to toggle)
sffview 0.5.0-7
  • links: PTS
  • area: main
  • in suites: forky, sid, trixie
  • size: 488 kB
  • sloc: cpp: 1,975; makefile: 23
file content (327 lines) | stat: -rw-r--r-- 9,219 bytes parent folder | download | duplicates (5)
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
//
// This file is part of sffview, a program to view structured fax files (sff)
//
// Copyright (C) 1998-2012 Peter Schaefer-Hutter and contributors ("THE AUTHORS")
//
// Permission to use, copy, modify, distribute, and sell this software and
// its documentation for any purpose is hereby granted without fee.
//
// THE SOFTWARE IS PROVIDED "AS-IS" AND WITHOUT WARRANTY OF ANY KIND,
// EXPRESS, IMPLIED OR OTHERWISE, INCLUDING WITHOUT LIMITATION, ANY
// WARRANTY OF MERCHANTABILITY OR FITNESS FOR A PARTICULAR PURPOSE.
//
// IN NO EVENT SHALL THE AUTHORS BE LIABLE FOR ANY SPECIAL,
// INCIDENTAL, INDIRECT OR CONSEQUENTIAL DAMAGES OF ANY KIND, OR ANY
// DAMAGES WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS,
// WHETHER OR NOT ADVISED OF THE POSSIBILITY OF DAMAGE, AND ON ANY
// THEORY OF LIABILITY, ARISING OUT OF OR IN CONNECTION WITH THE USE
// OR PERFORMANCE OF THIS SOFTWARE.
//
// Contributor(s): 
//   None
//
// You can contact the original author by email at peter.schaefer@gmx.de.
//
// I'm always pleased to hear that somebody is actually using my software.
// If you can manage it, e-mail me a quick notice. Thanks!
// 
/*-RCS-Info----------------------------------------------------

 $Id: sfffile.cpp,v 1.4 2008/06/29 09:30:48 pschaefer Exp $

---RCS-Info--------------------------------------------------*/

#include <cstdlib>
#include <cstring>
#include <vector>
#include <iostream>

#include "sfftypes.h"
#include "common.h"
#include "codes.h"
#include "decoder.h"
#include "sfffile.h"

using namespace std;

//-Constants-------------------------------------------------------

sff_byte CSffFile::m_SFFID[4] = { 0x53, 0x66, 0x66, 0x66 };

//-Types-----------------------------------------------------------

typedef enum {
  NEED_MAGIC,
  NEED_PAGESTART,
  NEED_PAGEHEADER,
  NEED_RECORD,
  LAST_PAGE 
} TScannerState;

//-----------------------------------------------------------------

CSffFile::CSffFile(const std::string& strFileName) :
  CFile(strFileName)
{
  ScanFile();
}

CSffFile::~CSffFile()
{
  PAGEVECTOR::iterator it = m_acPages.begin();
  for (; it != m_acPages.end(); it++) {
    delete (*it);
  }
}

void CSffFile::ScanFile()
{
  TSFFFileHeader dh;
  TSFFPageHeader ph;
  sff_byte       b1 = 0, b2 = 0;
  sff_word       w;
  int            nLineCount = 0;
  int            fuzz = 0;
  TSFFPage      *pPage;
  
  if (Eof())
    return;
  
  TScannerState state = NEED_MAGIC; 
  do {
    switch (state) {
    case NEED_MAGIC :
      for (fuzz = 0; fuzz < 2048; ++fuzz) {
        Seek(fuzz, CFile::sk_from_start);
        Read(&dh, sizeof(dh));
        if (Eof()) 
          throw CSimpleException(CSimpleException::err_invalidfile);
        if (::memcmp(&dh.sff_id, &m_SFFID, sizeof(m_SFFID)) == 0)
          break;
      }
      if (::memcmp(&dh.sff_id, &m_SFFID, sizeof(m_SFFID)) != 0) 
        throw CSimpleException(CSimpleException::err_invalidfile);
      if (dh.version > 1) 
        throw CSimpleException(CSimpleException::err_invalidversion);
      if (fuzz>0) {
        dh.first_page += fuzz;
      }
      Seek(dh.first_page, CFile::sk_from_start);
      state = NEED_PAGESTART;
      break;
    case NEED_PAGESTART :     
      b1 = GetC(); // Recordheader (0xFE fuer Seitenanfang)
      if (Eof()) 
        throw CSimpleException(CSimpleException::err_corruptfile);  
      if (b1 != 0xFE)
        throw CSimpleException(CSimpleException::err_corruptfile);  
      b1 = GetC(); // Recordlaenge (normalerweise 0x10)
      if (b1 == 0) 
        state = LAST_PAGE;
      else
        state = NEED_PAGEHEADER;
      break;
    case NEED_PAGEHEADER :
      Read(&ph, sizeof(TSFFPageHeader));
      if (Eof()) 
        throw CSimpleException(CSimpleException::err_corruptfile);  
      if (ph.coding > 0) 
        throw CSimpleException(CSimpleException::err_unknowncoding);
      Seek(b1 - sizeof(TSFFPageHeader), CFile::sk_current); // skip user data
      pPage = new TSFFPage;
      pPage->filepos = Tell();
      pPage->width   = ph.linelen;
      pPage->height  = ph.pagelen;
      // Values 254/255 are known for RVS COM
      pPage->dpi = (ph.horiz_res == 0) ? 203 : 
                   (ph.horiz_res == 255) ? 300 : 
                   (ph.horiz_res == 254) ? 400 : 0;
      pPage->lpi = (ph.vert_res == 0) ?  98 :
                   (ph.vert_res == 1) ? 196 : 
                   (ph.vert_res == 255) ? 300 : 
                   (ph.vert_res == 254) ? 400 : 0;
      m_acPages.push_back(pPage);
      state = NEED_RECORD;
      nLineCount = 0;
      break;
    case NEED_RECORD :
      b1 = GetC(); // Recordtyp einlesen
      if (Eof()) {
        m_acPages[GetPageCount()-1]->height = nLineCount;
        state = LAST_PAGE;
      } else if (b1 == 0) {
        // variable Anzahl Bytes folgt
        b1 = GetC(); // LSB
        if (Eof())
          throw CSimpleException(CSimpleException::err_corruptfile);  
        b2 = GetC(); // MSB
        if (Eof()) 
          throw CSimpleException(CSimpleException::err_corruptfile);  
        w = ((b2 << 8) | b1);
        Seek(w, CFile::sk_current); // Skip data
        if (Eof()) 
          throw CSimpleException(CSimpleException::err_corruptfile);  
        ++nLineCount;
      } else if (b1 < 217) {
        // normale Anzahl BYTEs folgt
        Seek((long)b1, CFile::sk_current);  // Skip data
        if (Eof()) {
          throw CSimpleException(CSimpleException::err_corruptfile);  
        }
        ++nLineCount;
      } else if (b1 < 254) {
        // Whiteskip
        nLineCount+=(b1 - 216);
      } else if (b1 < 255) {
        // 254 -> Pageheader
        m_acPages[GetPageCount()-1]->height = nLineCount;
        nLineCount = 0;
        b1 = GetC(); // Recordlaenge (normalerweise 0x10)
        if (Eof()) 
          throw CSimpleException(CSimpleException::err_corruptfile);  
        if (b1 == 0) {
          state = LAST_PAGE;
        } else {
          state = NEED_PAGEHEADER;
        }
      } else {
        // Fehlerhafte Zeile oder Benutzerinfo
        b1 = GetC(); // LSB
        if (Eof()) 
          throw CSimpleException(CSimpleException::err_corruptfile);  
        if (b1 == 0) {  
          ++nLineCount;
        } else {
          Seek(b1, CFile::sk_current);  // Skip user info
          if (Eof()) 
            throw CSimpleException(CSimpleException::err_corruptfile);
        }
      }
      break;
    case LAST_PAGE :
      break;
    }
  } while(state != LAST_PAGE);
  return;
}

bool CSffFile::PageIsValid(int nPage)
{
	return ((nPage >= 0) && (nPage < GetPageCount()));
}

bool CSffFile::SeekPage(int nPage)
{
	if (!PageIsValid(nPage))
		return false;
  Seek(m_acPages[nPage]->filepos, CFile::sk_from_start);
	return true;
} 

TSFFPage *CSffFile::GetPage(int nPage)
{
  return PageIsValid(nPage) ? m_acPages[nPage] : (TSFFPage *)NULL;
}

bool CSffFile::IsLowRes(int nPage)
{
  return PageIsValid(nPage) && (m_acPages[nPage]->lpi == 98);
}

sff_word CSffFile::GetHorizontalResolution(int nPage)
{
  return PageIsValid(nPage) ? m_acPages[nPage]->dpi :0;
}

sff_word CSffFile::GetVerticalResolution(int nPage)
{
  return PageIsValid(nPage) ? m_acPages[nPage]->lpi :0;
}

sff_dword CSffFile::GetPageWidth(int nPage)
{
  return PageIsValid(nPage) ? m_acPages[nPage]->width :0;
}

sff_dword CSffFile::GetPageHeight(int nPage)
{
  return PageIsValid(nPage) ? m_acPages[nPage]->height :0;
}

bool CSffFile::GetRecord(TSFFRecord& rec)
{
  sff_byte b1, b2;
  sff_word w;
  bool result;
  
  if (Eof()) {
    return false;
  }
  
  b1 = GetC();  // Recordtyp einlesen
  if (Eof()) {
    result = false;
  } else if (b1 == 0) {
    // variable Anzahl Bytes folgt
    b1 = GetC(); // LSB
    b2 = GetC(); // MSB
    w = ((b2 << 8) | b1);
    rec.type  = NORMAL;
    rec.cb    = w;
    rec.pData = (sff_byte *)malloc(w);
    Read(rec.pData, w);
    result = true;
  } else if (b1 < 217) {
    // normale Anzahl BYTEs folgt
    rec.type  = NORMAL;
    rec.cb    = b1;
    rec.pData = (sff_byte *)malloc(b1);
    Read(rec.pData, b1);
    result = true;
  } else if (b1 < 254) {
    // Whiteskip
    rec.type  = WHITESKIP;
    rec.cb    = (b1 - 216);
    rec.pData = 0;
    result = true;
  } else if (b1 < 255) {
    // 254 -> Pageheader
    result = false;
  } else {
    // Fehlerhafte Zeile oder Benutzerinfo
    b1 = GetC(); // LSB
    if (b1 == 0) {
      rec.type  = BADLINE;
      rec.cb    = 1;
      rec.pData = 0;
    } else {
      rec.type  = USERINFO;
      rec.cb    = b1;
      rec.pData = (sff_byte *)malloc(b1);
      Read(rec.pData, b1);
    }
    result = true;
  }
  return result;
}

bool CSffFile::DecodeRecord(TSFFRecord& rec, IBitSink& bitsink)
{
  bool  rc;

  if (rec.type != NORMAL)
    return false;

  CHuffDecoder source(rec.pData, rec.cb);

  try {
    rec.runlength = source.DecodeLine(bitsink);
    rc = true;
  }
  catch(CSimpleException e)
  {
    cerr << "ERROR: " << e.what() << endl;
    rc = false;
  }
  return rc;
}