]>
git.saurik.com Git - wxWidgets.git/blob - src/common/imagpnm.cpp
   1 ///////////////////////////////////////////////////////////////////////////// 
   3 // Purpose:     wxImage PNM handler 
   4 // Author:      Sylvain Bougnoux 
   6 // Copyright:   (c) Sylvain Bougnoux 
   7 // Licence:     wxWindows licence 
   8 ///////////////////////////////////////////////////////////////////////////// 
  11    We don't put pragma implement in this file because it is already present in 
  15 // For compilers that support precompilation, includes "wx.h". 
  16 #include "wx/wxprec.h" 
  23 #  include "wx/setup.h" 
  31 #include "wx/txtstrm.h" 
  33 //----------------------------------------------------------------------------- 
  35 //----------------------------------------------------------------------------- 
  37 #if !USE_SHARED_LIBRARIES 
  38 IMPLEMENT_DYNAMIC_CLASS(wxPNMHandler
,wxImageHandler
) 
  43 void Skip_Comment(wxInputStream 
&stream
) 
  45   wxTextInputStream 
text_stream(stream
); 
  47   if (stream
.Peek()==wxT('#')) 
  49       text_stream
.ReadLine(); 
  54 bool wxPNMHandler::LoadFile( wxImage 
*image
, wxInputStream
& stream
, bool verbose
, int WXUNUSED(index
) ) 
  56     wxUint32  width
, height
; 
  66     wxBufferedInputStream 
buf_stream(stream
); 
  67     wxTextInputStream 
text_stream(buf_stream
); 
  69     Skip_Comment(buf_stream
); 
  70     if (buf_stream
.GetC()==wxT('P')) c
=buf_stream
.GetC(); 
  75             if (verbose
) wxLogError(_("Loading Grey Ascii PNM image is not yet implemented.")); 
  78             if (verbose
) wxLogError(_("Loading Grey Raw PNM image is not yet implemented.")); 
  83             if (verbose
) wxLogError(_("PNM: File format is not recognized.")); 
  87     text_stream
.ReadLine(); // for the \n 
  88     Skip_Comment(buf_stream
); 
  89     text_stream 
>> width 
>> height 
; 
  90     Skip_Comment(buf_stream
); 
  91     text_stream 
>> maxval
; 
  93     //cout << line << " " << width << " " << height << " " << maxval << endl; 
  94     image
->Create( width
, height 
); 
  95     unsigned char *ptr 
= image
->GetData(); 
  99            wxLogError( _("PNM: Couldn't allocate memory.") ); 
 103    if (c
=='3') // Ascii RBG 
 105         wxUint32 value
, size
=3*width
*height
; 
 106         for (wxUint32 i
=0; i
<size
; ++i
) 
 108             //this is very slow !!! 
 109             //I wonder how we can make any better ? 
 110             value
=text_stream
.Read32(); 
 111             *ptr
++=(unsigned char)value
; 
 113             if (buf_stream
.LastError()!=wxSTREAM_NOERROR
) 
 115                 if (verbose
) wxLogError(_("PNM: File seems truncated.")); 
 120     if (c
=='6') // Raw RGB 
 121       buf_stream
.Read( ptr
, 3*width
*height 
); 
 123     image
->SetMask( FALSE 
); 
 125     return (buf_stream
.LastError()==wxStream_NOERROR 
|| buf_stream
.LastError()==wxStream_EOF
); 
 128 bool wxPNMHandler::SaveFile( wxImage 
*image
, wxOutputStream
& stream
, bool WXUNUSED(verbose
) ) 
 130     wxTextOutputStream 
text_stream(stream
); 
 132     //text_stream << "P6" << endl 
 133     //<< image->GetWidth() << " " << image->GetHeight() << endl 
 135     text_stream 
<< "P6\n" << image
->GetWidth() << " " << image
->GetHeight() << "\n255\n"; 
 136     stream
.Write(image
->GetData(),3*image
->GetWidth()*image
->GetHeight()); 
 138     return (stream
.LastError()==wxStream_NOERROR
); 
 141 bool wxPNMHandler::DoCanRead( wxInputStream
& stream 
) 
 143     off_t pos 
= stream
.TellI(); 
 145     Skip_Comment(stream
); 
 147     if ( stream
.GetC() == 'P' ) 
 149         switch (stream
.GetC()) 
 163 #endif // wxUSE_STREAMS