]>
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"
24 #include "wx/txtstrm.h"
30 //-----------------------------------------------------------------------------
32 //-----------------------------------------------------------------------------
34 #if !USE_SHARED_LIBRARIES
35 IMPLEMENT_DYNAMIC_CLASS(wxPNMHandler
,wxImageHandler
)
40 //#include <stream.h> // for cout
42 void Skip_Comment(wxInputStream
&stream
)
45 wxTextInputStream
text_stream(stream
);
47 if (stream
.Peek()==_T('#'))
54 bool wxPNMHandler::LoadFile( wxImage
*image
, wxInputStream
& stream
, bool WXUNUSED(verbose
) )
56 wxUint32 width
, height
;
67 wxTextInputStream
text_stream(stream
);
70 if (stream
.GetC()==_T('P')) c
=stream
.GetC();
75 wxLogError(_T("Loading Grey Ascii PNM image is not yet implemented."));
78 wxLogError(_T("Loading Grey Raw PNM image is not yet implemented."));
80 case _T('3'): case _T('6'): break;
82 wxLogError(_T("Loading PNM image : file not recognized."));
86 text_stream
>> line
; // for the \n
88 text_stream
>> width
>> height
;
90 text_stream
>> maxval
;
92 //cout << width << " " << height << " " << maxval << endl;
93 image
->Create( width
, height
);
94 unsigned char *ptr
= image
->GetData();
97 wxLogError( _T("Cannot allocate RAM for RGB data in PNM file.") );
101 if (c
=='3') // Ascii RBG
103 wxUint32 value
, size
=3*width
*height
;
104 for (wxUint32 i
=0; i
<size
; ++i
)
106 //this is very slow !!!
107 //I wonder how we can make any better ?
108 value
=text_stream
.Read32();
109 *ptr
++=(unsigned char)value
;
111 if (stream
.LastError()!=wxSTREAM_NOERROR
)
113 wxLogError(_T("Loading PNM image : file seems truncated."));
118 if (c
=='6') // Raw RGB
119 stream
.Read( ptr
, 3*width
*height
);
121 image
->SetMask( FALSE
);
123 return (stream
.LastError()==wxStream_NOERROR
|| stream
.LastError()==wxStream_EOF
);
126 bool wxPNMHandler::SaveFile( wxImage
*image
, wxOutputStream
& stream
, bool WXUNUSED(verbose
) )
128 wxTextOutputStream
text_stream(stream
);
130 //text_stream << "P6" << endl
131 //<< image->GetWidth() << " " << image->GetHeight() << endl
133 text_stream
<< "P6\n" << image
->GetWidth() << " " << image
->GetHeight() << "\n255\n";
134 stream
.Write(image
->GetData(),3*image
->GetWidth()*image
->GetHeight());
136 return (stream
.LastError()==wxStream_NOERROR
);
139 bool wxPNMHandler::CanRead( wxInputStream
& stream
)
141 off_t pos
=stream
.TellI();
143 Skip_Comment(stream
);
145 if (stream
.GetC()==_T('P'))
146 switch (stream
.GetC())
148 case _T('3'): case _T('6'):
158 #endif // wxUSE_STREAMS