File: sffdoc.cpp

package info (click to toggle)
sffview 0.4.1-2
  • links: PTS
  • area: main
  • in suites: wheezy
  • size: 416 kB
  • sloc: cpp: 1,978; makefile: 23
file content (418 lines) | stat: -rw-r--r-- 10,094 bytes parent folder | download
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
//
// This file is part of sffview, a program to view structured fax files (sff)
//
// Copyright (C) 1998-2008 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: sffdoc.cpp,v 1.4 2011/10/18 19:39:38 pschaefer Exp $
  
---RCS-Info--------------------------------------------------*/

#include <wx/wx.h>

#if !wxUSE_DOC_VIEW_ARCHITECTURE
#error You must set wxUSE_DOC_VIEW_ARCHITECTURE to 1 in setup.h!
#endif

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

#include <iostream>
using namespace std;

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

CBitmapDecoder::CBitmapDecoder(wxUint32 aWidth, wxUint32 aHeight) :
		m_pBitmap(0),
		m_Width(aWidth),
		m_Height(aHeight),
		m_Scanline(0),
		m_sink(m_abBuffer, sizeof(m_abBuffer))
{  
	m_WidthInBytes = aWidth>>3;
	m_Width = aWidth;
	m_Height = aHeight;
	m_pBitmap = new wxUint8[m_WidthInBytes*aHeight];
	memset(m_pBitmap, 0x00, m_WidthInBytes*aHeight);
	Reset();
}

CBitmapDecoder::~CBitmapDecoder()
{
  if (m_pBitmap) delete m_pBitmap;
}
	
void CBitmapDecoder::Reset()
{
	m_Scanline = 0;
	m_sink.Reset();
}

void CBitmapDecoder::BlankLine()
{
	::memset(m_abBuffer, 0x00, sizeof(m_abBuffer));
}

CBitSink& CBitmapDecoder::GetBitSink()
{
	m_sink.Reset();
	return m_sink;
}

void CBitmapDecoder::WriteLine()
{
	if (m_Scanline < m_Height) {
		memcpy(m_pBitmap+(m_WidthInBytes*m_Scanline), m_abBuffer, m_WidthInBytes);
		++m_Scanline;
	}
}

const wxUint8 *CBitmapDecoder::GetBitmap()
{
	return m_pBitmap;
}

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

IMPLEMENT_DYNAMIC_CLASS(SffDocument, wxDocument)

SffDocument::SffDocument(void) :
	m_pSffFile(NULL),
	m_pDecoder(NULL),
	m_pPageBmp(NULL),
	m_nPageIdx(0)
{
}

SffDocument::~SffDocument(void)
{
  if (m_pSffFile) {
    delete m_pSffFile;
	}
	if (m_pPageBmp) {
		delete m_pPageBmp;
	}
	if (m_pDecoder) {
	  delete m_pDecoder;
	}
}

bool SffDocument::OnOpenDocument(const wxString& filename)
{
  bool rc =  false;
  
  try {
    wxBusyCursor wait;
    m_nPageIdx = 0;
    std::string strFN(filename.fn_str());
    m_pSffFile = new CSffFile(strFN);
		m_pDecoder = new CBitmapDecoder(GetWidth(), GetHeight());
    SetFilename(filename, TRUE);
		Modify(FALSE);
    CreatePageBitmap();
    rc = true;
  } 
  catch (CSimpleException e) {
    wxLogError( (wxChar *) e.what().c_str() );
  }
  return rc;
}

int SffDocument::GetPageCount()
{
  wxASSERT(m_pSffFile != NULL);
  return (m_pSffFile != NULL) ? m_pSffFile->GetPageCount() : 0;
}

int SffDocument::GetCurrentPageIdx()
{
  wxASSERT(m_pSffFile != NULL);
  return m_nPageIdx;
}

void SffDocument::CreatePageBitmap()
{
	int i;
	int lines, cols;
	bool bLowRes;
	
	if (m_pSffFile == NULL)
		return;
	
  try
  {
		TSFFRecord rec;
		// CDCFile outfile;
		m_pSffFile->SeekPage(m_nPageIdx);
		bLowRes = m_pSffFile->IsLowRes(m_nPageIdx);
		lines = GetHeight();
		cols  = GetWidth();
		// Decode SFF Records ...
		m_pDecoder->Reset();
    while ((lines > 0) && m_pSffFile->GetRecord(rec)) {
      switch(rec.type) {
			case NORMAL :
				m_pDecoder->BlankLine();
				m_pSffFile->DecodeRecord(rec, m_pDecoder->GetBitSink());
				m_pDecoder->WriteLine(); --lines;
				if (bLowRes) { // double line if low-res
					m_pDecoder->WriteLine(); --lines;
				}
				if (rec.pData != 0) free(rec.pData);
				break;
			case USERINFO :
				// not supported
				if (rec.pData != 0) free(rec.pData);
				break;
			case BADLINE :
			case WHITESKIP :
				// a white skip is, ah, skipped ;)
				m_pDecoder->BlankLine();
				for (i=0; i < rec.cb; ++i) {
					m_pDecoder->WriteLine(); --lines;
					if (bLowRes) { // double line if low-res
						m_pDecoder->WriteLine(); --lines;
					}
				}
				break;
			default :
				break;
      }
    }
		wxBitmap *pOld = m_pPageBmp;
		m_pPageBmp = new wxBitmap((const char*)m_pDecoder->GetBitmap(), 
			GetWidth(), GetHeight());
		UpdateAllViews();
		if (pOld) {
			delete pOld;
		}
  }
  catch (CSimpleException e) {
    wxLogError( (wxChar *)e.what().c_str() );
  }
}

wxBitmap *SffDocument::GetPageBitmap(int nPage)
{
	int i;
	int lines, cols;
	bool bLowRes;

	
	if (m_pSffFile == NULL)
		return NULL;

  wxBitmap *pBmp = NULL;
	
  try
  {
		TSFFRecord rec;
		// CDCFile outfile;
		m_pSffFile->SeekPage(nPage-1);
		bLowRes = m_pSffFile->IsLowRes(nPage-1);
		lines = GetHeight();
		cols  = GetWidth();
		// Decode SFF Records ...
		m_pDecoder->Reset();
    while ((lines > 0) && m_pSffFile->GetRecord(rec)) {
      switch(rec.type) {
			case NORMAL :
				m_pDecoder->BlankLine();
				m_pSffFile->DecodeRecord(rec, m_pDecoder->GetBitSink());
				m_pDecoder->WriteLine(); --lines;
				if (bLowRes) { // double line if low-res
					m_pDecoder->WriteLine(); --lines;
				}
				if (rec.pData != 0) free(rec.pData);
				break;
			case USERINFO :
				// not supported
				if (rec.pData != 0) free(rec.pData);
				break;
			case BADLINE :
			case WHITESKIP :
				// a white skip is, ah, skipped ;)
				m_pDecoder->BlankLine();
				for (i=0; i < rec.cb; ++i) {
					m_pDecoder->WriteLine(); --lines;
					if (bLowRes) { // double line if low-res
						m_pDecoder->WriteLine(); --lines;
					}
				}
				break;
			default :
				break;
      }
    }
		pBmp = new wxBitmap((const char*)m_pDecoder->GetBitmap(), 
			GetWidth(), GetHeight());
  }
  catch (CSimpleException e) {
    wxLogError( (wxChar *)e.what().c_str() );
  }

  return pBmp;
}

wxUint32 SffDocument::GetHeight()
{
  wxASSERT(m_pSffFile != NULL);
  if (m_pSffFile != NULL) {
		wxUint32 h = m_pSffFile->GetPageHeight(m_nPageIdx);
    return m_pSffFile->IsLowRes(m_nPageIdx) ? h * 2 : h;
  }
	return 0;
}

wxUint32 SffDocument::GetWidth()
{
  wxASSERT(m_pSffFile != NULL);
  if (m_pSffFile != NULL) {
    return m_pSffFile->GetPageWidth(m_nPageIdx);
  }
	return 0;
}

void SffDocument::NextPage()
{
  wxASSERT(m_pSffFile != NULL);
  if (m_pSffFile != NULL) {
    if (m_nPageIdx < m_pSffFile->GetPageCount()) {
      ++m_nPageIdx;
	    CreatePageBitmap();
    }
  }
}

void SffDocument::PrevPage()
{
  wxASSERT(m_pSffFile != NULL);
  if (m_nPageIdx >= 1) {
    --m_nPageIdx;
    CreatePageBitmap();
  }
}
/*
void SffDocument::PreparePage(int nPage)
{
  wxASSERT(m_pSffFile != NULL);
  if (m_nPageIdx >= 1) {
    --m_nPageIdx;
    CreatePageBitmap();
  }
}
*/
// ---------------------------------------------------------------------

IMPLEMENT_DYNAMIC_CLASS(SffPrintout, wxPrintout)

SffPrintout::SffPrintout(SffDocument *doc) : 
	wxPrintout() 
{ 
	m_Doc = doc;
	m_nPageCount = doc->GetPageCount();
};

bool SffPrintout::OnPrintPage(int page)
{
    wxBitmap *pBmp = m_Doc->GetPageBitmap(page);
		int nBitmapHeight = pBmp->GetHeight();
		int nBitmapWidth  = pBmp->GetWidth();

    wxDC *dc = GetDC();

    wxMemoryDC mem_dc;
    mem_dc.SelectObject(*pBmp);

    // Get the logical pixels per inch of screen and printer
    int ppiScreenX, ppiScreenY;
    GetPPIScreen(&ppiScreenX, &ppiScreenY);
    wxUnusedVar(ppiScreenY);
    int ppiPrinterX, ppiPrinterY;
    GetPPIPrinter(&ppiPrinterX, &ppiPrinterY);
    wxUnusedVar(ppiPrinterY);

    // This scales the DC so that the printout roughly represents the
    // the screen scaling. The text point size _should_ be the right size
    // but in fact is too small for some reason. This is a detail that will
    // need to be addressed at some point but can be fudged for the
    // moment.
    float scale = (float)((float)ppiPrinterX/(float)ppiScreenX);

    // Now we have to check in case our real page size is reduced
    // (e.g. because we're drawing to a print preview memory DC)
    int pageWidth, pageHeight;
    int w, h;
    dc->GetSize(&w, &h);
    GetPageSizePixels(&pageWidth, &pageHeight);
    wxUnusedVar(pageHeight);

    // If printer pageWidth == current DC width, then this doesn't
    // change. But we might be the preview bitmap width, so scale down.
    float overallScale = scale * (float)(w/(float)pageWidth);
    float zoomW = w; zoomW = zoomW/(nBitmapWidth*overallScale);
    float zoomH = h; zoomH = zoomH/(nBitmapHeight*overallScale);
    dc->SetUserScale(overallScale * zoomW, overallScale * zoomH);
    dc->SetBackground(*wxWHITE_BRUSH);
#if defined(__WXGTK__) && !wxCHECK_VERSION(2,8,0)
    if (pageWidth != w) {
    	// Strange misbehaviour of older wxGTK -> if we blit zoomed, the colours
  		// get inversed ??!? Workaround: wxSRC_INVERT
      dc->Blit(0,0,nBitmapWidth,nBitmapHeight,&mem_dc,0,0,wxSRC_INVERT);
  	} else {
      dc->Blit(0,0,nBitmapWidth,nBitmapHeight,&mem_dc,0,0,wxCOPY);
  	}
#else
    dc->Blit(0,0,nBitmapWidth,nBitmapHeight,&mem_dc,0,0,wxCOPY);
#endif
    return TRUE;
}

bool SffPrintout::HasPage(int pageNum)
{
	return (pageNum <= m_nPageCount);
}

bool SffPrintout::OnBeginDocument(int startPage, int endPage)
{
	if (!wxPrintout::OnBeginDocument(startPage, endPage))
			return FALSE;

	return TRUE;
}

void SffPrintout::GetPageInfo(int *minPage, int *maxPage, int *selPageFrom, int *selPageTo)
{
	*minPage = 1;
	*maxPage = m_nPageCount;
	*selPageFrom = 1;
	*selPageTo = 1;
}