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
|
// sffview.cpp
//
// This file is part of sffview, a program to view
// structured fax files (sff).
// Copyright (C) 2000 Peter Schaefer
//
// Permission to use, copy, modify, and distribute this software and its
// documentation for any purpose and without fee is hereby granted, provided
// that the above copyright notice appear in all copies. This software
// is provided "as is" without express or implied warranty.
//
// You can contact the author by email at peter.schaefer@gmx.de.
//
// For compilers that support precompilation, includes "wx/wx.h".
#include "wx/wxprec.h"
#ifdef __BORLANDC__
#pragma hdrstop
#endif
#ifndef WX_PRECOMP
#include "wx/wx.h"
#endif
#include "wx/image.h"
#include "wx/wfstream.h"
#include "wx/toolbar.h"
#include "inc/sfftobmp.h"
#ifndef __WXMSW__
#include "bitmaps/open.xpm"
#include "bitmaps/help.xpm"
#include "bitmaps/prev.xpm"
#include "bitmaps/next.xpm"
#endif
// ----------------------------------------------------------------------------
// Class declarations
// ----------------------------------------------------------------------------
// forwards
class MyFrame;
class MyApp;
// MyCanvas
class MyCanvas: public wxScrolledWindow
{
public:
MyCanvas() {};
MyCanvas( wxWindow *parent, wxWindowID, const wxPoint &pos, const wxSize &size );
~MyCanvas();
void OnPaint( wxPaintEvent &event );
bool OpenFile(const wxString& strFilename);
void DisplayPage(int nPage);
int GetPageCount();
wxBitmap *my_bmp;
CSffFile *my_file;
DECLARE_DYNAMIC_CLASS(MyCanvas)
DECLARE_EVENT_TABLE()
};
// MyFrame
class MyFrame: public wxFrame
{
public:
MyFrame();
void RecreateToolbar();
void UpdateUI();
void OnAbout( wxCommandEvent &event );
void OnQuit( wxCommandEvent &event );
void OnFileOpen( wxCommandEvent &event );
void OnPrevPage( wxCommandEvent &event );
void OnNextPage( wxCommandEvent &event );
int m_nCurrentPage;
int m_nPageCount;
MyCanvas *m_canvas;
DECLARE_DYNAMIC_CLASS(MyFrame)
DECLARE_EVENT_TABLE()
};
// MyApp
class MyApp: public wxApp
{
public:
virtual bool OnInit();
};
// ----------------------------------------------------------------------------
// wxSFFHandler (not used currently)
// ----------------------------------------------------------------------------
#if defined(wxSFFHandler)
#define wxBITMAP_TYPE_SFF 49
class WXDLLEXPORT wxSFFHandler: public wxImageHandler
{
DECLARE_DYNAMIC_CLASS(wxSFFHandler)
public:
inline wxSFFHandler()
{
m_name = "SFF file";
m_extension = "sff";
m_type = wxBITMAP_TYPE_SFF;
m_mime = "image/sff";
};
virtual bool LoadFile( wxImage *image, wxInputStream& stream, bool verbose=TRUE );
virtual bool SaveFile( wxImage *image, wxOutputStream& stream, bool verbose=TRUE );
virtual bool DoCanRead( wxInputStream& stream );
};
#if !USE_SHARED_LIBRARIES
IMPLEMENT_DYNAMIC_CLASS(wxSFFHandler,wxImageHandler)
#endif
#if wxUSE_STREAMS
bool wxSFFHandler::DoCanRead( wxInputStream& stream )
{
TSFFFileHeader dh;
unsigned char SFFID[4] = { 0x53, 0x66, 0x66, 0x66 };
stream.Read(&dh, sizeof(dh));
if (stream.LastError() == wxStream_NOERROR) {
if (::memcmp(&dh.sff_id, &SFFID, sizeof(SFFID)) == 0) {
if (dh.version > 1) {
return true;
}
}
}
return false;
}
bool wxSFFHandler::SaveFile( wxImage *image, wxOutputStream& stream, bool verbose )
{
return false;
}
bool wxSFFHandler::LoadFile( wxImage *image, wxInputStream& stream, bool verbose )
{
static wxByte acOutBuffer[5120];
TSFFRecord rec;
int i,j;
try
{
CSffFile sff(stream);
image->Destroy();
long dwHeight = sff.GetPageHeight(1);
long dwWidth = sff.GetPageWidth(1);
image->Create( dwWidth, dwHeight );
/* alloc ram */
wxUint8 *ptr = image->GetData();
if (!ptr)
{
wxLogError( "Cannot allocate RAM for RGB data in file\n" );
return FALSE;
}
/*
* Decode SFF Records ...
*/
sff.SeekPage(1);
long dwBytesPerLine = dwWidth>>3;
int bit, val;
wxUint8 *data;
long line = 0;
while (sff.GetRecord(rec)) {
switch(rec.type) {
case NORMAL :
if (sff.DecodeRecord(rec, acOutBuffer, sizeof(acOutBuffer))) {
data = ptr;
for (i = 0; i < dwBytesPerLine; i++) {
for (bit = 0; bit < 8; bit++) {
val = ((acOutBuffer[i] & (0x80 >> bit)) ? 0x00 : 0xFF);
*data++ = val; *data++ = val; *data++ = val;
}
}
ptr += dwWidth; ptr += dwWidth; ptr += dwWidth;
line++;
}
if (rec.pData != 0) free(rec.pData);
break;
case USERINFO :
// momentan unbercksichtigt
if (rec.pData != 0) free(rec.pData);
break;
case BADLINE :
// Puffer wiederholen
data = ptr;
for (i = 0; i < dwBytesPerLine; i++) {
for (bit = 0; bit < 8; bit++) {
val = ((acOutBuffer[i] & (0x80 >> bit)) ? 0x00 : 0xFF);
*data++ = val; *data++ = val; *data++ = val;
}
}
ptr += dwWidth; ptr += dwWidth; ptr += dwWidth;
line++;
break;
case WHITESKIP :
data = ptr;
for (j=0; j < rec.cb; ++j) {
for (i = 0; i < dwBytesPerLine; i++) {
for (bit = 0; bit < 8; bit++) {
*data++ = 0xFF; *data++ = 0xFF; *data++ = 0xFF;
}
}
ptr += dwWidth; ptr += dwWidth; ptr += dwWidth;
line++;
}
break;
default :
break;
}
}
}
catch (CSimpleException e) {
wxLogError( e.GetReason() );
image->Destroy();
return FALSE;
}
image->SetMask( FALSE );
return TRUE;
}
#endif // wxUSE_STREAMS
#endif // wxSFFHandler
// ----------------------------------------------------------------------------
// Implementation
// ----------------------------------------------------------------------------
// ----------------------------------------------------------------------------
// constants
// ----------------------------------------------------------------------------
const int ID_QUIT = 108;
const int ID_ABOUT = 109;
const int ID_OPEN = 110;
const int ID_PREVPAGE = 111;
const int ID_NEXTPAGE = 112;
const int ID_TOOLBAR = 500;
IMPLEMENT_APP(MyApp)
// ----------------------------------------------------------------------------
// MyCanvas
// ----------------------------------------------------------------------------
IMPLEMENT_DYNAMIC_CLASS(MyCanvas, wxScrolledWindow)
BEGIN_EVENT_TABLE(MyCanvas, wxScrolledWindow)
EVT_PAINT(MyCanvas::OnPaint)
END_EVENT_TABLE()
MyCanvas::MyCanvas( wxWindow *parent, wxWindowID id,
const wxPoint &pos, const wxSize &size )
: wxScrolledWindow( parent, id, pos, size, wxSUNKEN_BORDER )
{
my_bmp = (wxBitmap*) NULL;
my_file = (CSffFile *) NULL;
SetBackgroundColour(*wxWHITE);
}
MyCanvas::~MyCanvas()
{
if (my_bmp) delete my_bmp;
if (my_file) delete my_file;
}
bool MyCanvas::OpenFile(const wxString& strFilename)
{
bool rc = false;
if (wxFileExists(strFilename))
{
try
{
my_file = new CSffFile(strFilename);
rc = true;
}
catch (CSimpleException e) {
wxLogError( e.GetReason() );
}
}
return rc;
}
void MyCanvas::DisplayPage(int nPage)
{
if (my_file == NULL)
return;
if (my_bmp) delete my_bmp;
my_bmp = (wxBitmap*) NULL;
try
{
TSFFRecord rec;
long dwHeight = my_file->GetPageHeight(1);
long dwWidth = my_file->GetPageWidth(1);
wxUint8 *bitmap = (wxUint8 *)malloc(dwWidth*dwHeight);
/*
* Decode SFF Records ...
*/
my_file->SeekPage(nPage);
long dwBytesPerLine = dwWidth>>3;
wxUint8 *ptr = bitmap;
while (my_file->GetRecord(rec)) {
switch(rec.type) {
case NORMAL :
my_file->DecodeRecord(rec, ptr, dwBytesPerLine);
ptr += dwBytesPerLine;
if (rec.pData != 0) free(rec.pData);
break;
case USERINFO :
// momentan unbercksichtigt
if (rec.pData != 0) free(rec.pData);
break;
case BADLINE :
// Puffer wiederholen
memset(ptr, 0x00, dwBytesPerLine);
ptr += dwBytesPerLine;
break;
case WHITESKIP :
memset(ptr, 0x00, dwBytesPerLine*rec.cb);
ptr += dwBytesPerLine*rec.cb;
break;
default :
break;
}
}
my_bmp = new wxBitmap((const char *)bitmap, dwWidth, dwHeight, 1);
free(bitmap);
SetScrollbars( 1, 1, dwWidth, dwHeight);
Refresh();
}
catch (CSimpleException e) {
wxLogError( e.GetReason() );
}
}
int MyCanvas::GetPageCount()
{
if (my_file == NULL)
return -1;
return my_file->GetPageCount();
}
void MyCanvas::OnPaint( wxPaintEvent &WXUNUSED(event) )
{
wxPaintDC dc( this );
PrepareDC( dc );
if (my_bmp && my_bmp->Ok()) dc.DrawBitmap( *my_bmp, 0, 0 );
}
// ----------------------------------------------------------------------------
// MyFrame
// ----------------------------------------------------------------------------
IMPLEMENT_DYNAMIC_CLASS( MyFrame, wxFrame )
BEGIN_EVENT_TABLE(MyFrame,wxFrame)
EVT_MENU (ID_OPEN, MyFrame::OnFileOpen)
EVT_MENU (ID_PREVPAGE, MyFrame::OnPrevPage)
EVT_MENU (ID_NEXTPAGE, MyFrame::OnNextPage)
EVT_MENU (ID_ABOUT, MyFrame::OnAbout)
EVT_MENU (ID_QUIT, MyFrame::OnQuit)
END_EVENT_TABLE()
MyFrame::MyFrame()
: wxFrame( (wxFrame *)NULL, -1, "SFF Viewer",
wxPoint(20,20), wxSize(800,600) )
{
wxMenu *file_menu = new wxMenu();
file_menu->Append( ID_OPEN, "&ffnen..");
file_menu->Append( ID_ABOUT, "Info..");
file_menu->Append( ID_QUIT, "B&eenden");
wxMenuBar *menu_bar = new wxMenuBar();
menu_bar->Append(file_menu, "&Datei");
SetMenuBar( menu_bar );
CreateStatusBar(2);
int widths[] = { -1, 100 };
SetStatusWidths( 2, widths );
m_canvas = new MyCanvas( this, -1, wxPoint(0,0), wxSize(800,600) );
m_canvas->SetScrollbars( 1, 1, 800, 600);
m_nCurrentPage = 1;
m_nPageCount = 1;
RecreateToolbar();
SetStatusText("Bereit.");
}
void MyFrame::RecreateToolbar()
{
// delete and recreate the toolbar
wxToolBarBase *toolBar = GetToolBar();
delete toolBar;
SetToolBar(NULL);
long style = wxNO_BORDER | wxTB_FLAT | wxTB_DOCKABLE | wxTB_HORIZONTAL;
toolBar = CreateToolBar(style, ID_TOOLBAR);
toolBar->SetMargins( 4, 4 );
// Set up toolbar
wxBitmap toolBarBitmaps[4];
toolBarBitmaps[0] = wxBITMAP(open);
toolBarBitmaps[1] = wxBITMAP(prev);
toolBarBitmaps[2] = wxBITMAP(next);
toolBarBitmaps[3] = wxBITMAP(help);
#ifdef __WXMSW__
int width = 24;
#else
int width = 16;
#endif
int currentX = 5;
toolBar->AddTool(ID_OPEN, toolBarBitmaps[0], wxNullBitmap, FALSE, currentX, -1, (wxObject *) NULL, "Datei ffnen");
currentX += width + 5;
toolBar->AddSeparator();
toolBar->AddTool(ID_PREVPAGE, toolBarBitmaps[1], wxNullBitmap, FALSE, currentX, -1, (wxObject *) NULL, "vorherige Seite");
currentX += width + 5;
toolBar->AddTool(ID_NEXTPAGE, toolBarBitmaps[2], wxNullBitmap, FALSE, currentX, -1, (wxObject *) NULL, "nchste Seite");
currentX += width + 5;
toolBar->AddSeparator();
toolBar->AddTool(ID_ABOUT, toolBarBitmaps[3], wxNullBitmap, FALSE, currentX, -1, (wxObject *) NULL, "Info");
toolBar->Realize();
// toolBar->SetRows(1);
toolBar->EnableTool( ID_PREVPAGE, FALSE );
toolBar->EnableTool( ID_NEXTPAGE, FALSE );
}
void MyFrame::UpdateUI()
{
char acBuf[20];
wxToolBarBase *tb = GetToolBar();
tb->EnableTool( ID_PREVPAGE, (m_nPageCount > 1) && (m_nCurrentPage > 1));
tb->EnableTool( ID_NEXTPAGE, (m_nPageCount > 1) && (m_nCurrentPage < m_nPageCount));
sprintf(acBuf, "%d/%d", m_nCurrentPage, m_nPageCount);
SetStatusText(acBuf, 1);
}
void MyFrame::OnQuit( wxCommandEvent &WXUNUSED(event) )
{
Close( TRUE );
}
void MyFrame::OnPrevPage( wxCommandEvent &WXUNUSED(event) )
{
if (m_nCurrentPage > 1) {
m_canvas->DisplayPage(--m_nCurrentPage);
}
UpdateUI();
}
void MyFrame::OnNextPage( wxCommandEvent &WXUNUSED(event) )
{
if (m_nCurrentPage < m_nPageCount) {
m_canvas->DisplayPage(++m_nCurrentPage);
}
UpdateUI();
}
void MyFrame::OnAbout( wxCommandEvent &WXUNUSED(event) )
{
(void)wxMessageBox(
"This is sffview, a program to view structured fax files (sff)\n\n"
"This software and its documentation is\n"
"Copyright (C) 2000 Peter Schaefer\n\n"
"Permission to use, copy, modify, and distribute this software and its\n"
"documentation for any purpose and without fee is hereby granted, provided\n"
"that the above copyright notice appear in all copies. This software\n"
"is provided 'as is' without express or implied warranty.\n\n"
"You can contact the author by email at peter.schaefer@gmx.de",
"ber SFF-Viewer", wxICON_INFORMATION | wxOK );
}
void MyFrame::OnFileOpen( wxCommandEvent& WXUNUSED(event) )
{
wxFileDialog dialog(this, "Datei ffnen", "", "", "Faxdateien (*.sff)|*.sff", 0);
if (dialog.ShowModal() == wxID_OK)
{
m_nCurrentPage = 1;
m_nPageCount = 1;
if (m_canvas->OpenFile(dialog.GetPath())) {
m_nCurrentPage = 1;
m_nPageCount=m_canvas->GetPageCount();
m_canvas->DisplayPage(m_nCurrentPage);
SetStatusText("Datei erfolgreich geladen.");
} else {
SetStatusText("Datei konnte nicht geladen werden.");
}
UpdateUI();
}
}
//-----------------------------------------------------------------------------
// MyApp
//-----------------------------------------------------------------------------
bool MyApp::OnInit()
{
wxFrame *frame = new MyFrame();
frame->Show( TRUE );
return TRUE;
}
|