]>
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"
32 //-----------------------------------------------------------------------------
34 //-----------------------------------------------------------------------------
36 #if !USE_SHARED_LIBRARIES
37 IMPLEMENT_DYNAMIC_CLASS(wxPNMHandler
,wxImageHandler
)
42 //#include <stream.h> // for cout
44 void Skip_Comment(wxInputStream
&stream
)
47 wxTextInputStream
text_stream(stream
);
49 if (stream
.Peek()==_T('#'))
56 bool wxPNMHandler::LoadFile( wxImage
*image
, wxInputStream
& stream
, bool WXUNUSED(verbose
) )
58 wxUint32 width
, height
;
69 wxBufferedInputStream
buf_stream(stream
);
70 wxTextInputStream
text_stream(buf_stream
);
72 Skip_Comment(buf_stream
);
73 if (buf_stream
.GetC()==_T('P')) c
=buf_stream
.GetC();
78 wxLogError(_T("Loading Grey Ascii PNM image is not yet implemented."));
81 wxLogError(_T("Loading Grey Raw PNM image is not yet implemented."));
83 case _T('3'): case _T('6'): break;
85 wxLogError(_T("Loading PNM image : file not recognized."));
89 text_stream
>> line
; // for the \n
90 Skip_Comment(buf_stream
);
91 text_stream
>> width
>> height
;
92 Skip_Comment(buf_stream
);
93 text_stream
>> maxval
;
95 //cout << line << " " << width << " " << height << " " << maxval << endl;
96 image
->Create( width
, height
);
97 unsigned char *ptr
= image
->GetData();
100 wxLogError( _T("Cannot allocate RAM for RGB data in PNM file.") );
104 if (c
=='3') // Ascii RBG
106 wxUint32 value
, size
=3*width
*height
;
107 for (wxUint32 i
=0; i
<size
; ++i
)
109 //this is very slow !!!
110 //I wonder how we can make any better ?
111 value
=text_stream
.Read32();
112 *ptr
++=(unsigned char)value
;
114 if (buf_stream
.LastError()!=wxSTREAM_NOERROR
)
116 wxLogError(_T("Loading PNM image : file seems truncated."));
121 if (c
=='6') // Raw RGB
122 buf_stream
.Read( ptr
, 3*width
*height
);
124 image
->SetMask( FALSE
);
126 return (buf_stream
.LastError()==wxStream_NOERROR
|| buf_stream
.LastError()==wxStream_EOF
);
129 bool wxPNMHandler::SaveFile( wxImage
*image
, wxOutputStream
& stream
, bool WXUNUSED(verbose
) )
131 wxTextOutputStream
text_stream(stream
);
133 //text_stream << "P6" << endl
134 //<< image->GetWidth() << " " << image->GetHeight() << endl
136 text_stream
<< "P6\n" << image
->GetWidth() << " " << image
->GetHeight() << "\n255\n";
137 stream
.Write(image
->GetData(),3*image
->GetWidth()*image
->GetHeight());
139 return (stream
.LastError()==wxStream_NOERROR
);
142 bool wxPNMHandler::CanRead( wxInputStream
& stream
)
144 off_t pos
=stream
.TellI();
146 Skip_Comment(stream
);
148 if (stream
.GetC()==_T('P'))
149 switch (stream
.GetC())
151 case _T('3'): case _T('6'):
161 #endif // wxUSE_STREAMS