]>
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"
30 #include "wx/txtstrm.h"
32 //-----------------------------------------------------------------------------
34 //-----------------------------------------------------------------------------
36 #if !USE_SHARED_LIBRARIES
37 IMPLEMENT_DYNAMIC_CLASS(wxPNMHandler
,wxImageHandler
)
42 void Skip_Comment(wxInputStream
&stream
)
45 wxTextInputStream
text_stream(stream
);
47 if (stream
.Peek()==wxT('#'))
54 bool wxPNMHandler::LoadFile( wxImage
*image
, wxInputStream
& stream
, bool WXUNUSED(verbose
) )
56 wxUint32 width
, height
;
67 wxBufferedInputStream
buf_stream(stream
);
68 wxTextInputStream
text_stream(buf_stream
);
70 Skip_Comment(buf_stream
);
71 if (buf_stream
.GetC()==wxT('P')) c
=buf_stream
.GetC();
76 wxLogError(wxT("Loading Grey Ascii PNM image is not yet implemented."));
79 wxLogError(wxT("Loading Grey Raw PNM image is not yet implemented."));
81 case wxT('3'): case wxT('6'): break;
83 wxLogError(wxT("Loading PNM image : file not recognized."));
87 text_stream
>> line
; // 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();
98 wxLogError( wxT("Cannot allocate RAM for RGB data in PNM file.") );
102 if (c
=='3') // Ascii RBG
104 wxUint32 value
, size
=3*width
*height
;
105 for (wxUint32 i
=0; i
<size
; ++i
)
107 //this is very slow !!!
108 //I wonder how we can make any better ?
109 value
=text_stream
.Read32();
110 *ptr
++=(unsigned char)value
;
112 if (buf_stream
.LastError()!=wxSTREAM_NOERROR
)
114 wxLogError(wxT("Loading PNM image : file seems truncated."));
119 if (c
=='6') // Raw RGB
120 buf_stream
.Read( ptr
, 3*width
*height
);
122 image
->SetMask( FALSE
);
124 return (buf_stream
.LastError()==wxStream_NOERROR
|| buf_stream
.LastError()==wxStream_EOF
);
127 bool wxPNMHandler::SaveFile( wxImage
*image
, wxOutputStream
& stream
, bool WXUNUSED(verbose
) )
129 wxTextOutputStream
text_stream(stream
);
131 //text_stream << "P6" << endl
132 //<< image->GetWidth() << " " << image->GetHeight() << endl
134 text_stream
<< "P6\n" << image
->GetWidth() << " " << image
->GetHeight() << "\n255\n";
135 stream
.Write(image
->GetData(),3*image
->GetWidth()*image
->GetHeight());
137 return (stream
.LastError()==wxStream_NOERROR
);
140 bool wxPNMHandler::CanRead( wxInputStream
& stream
)
142 off_t pos
=stream
.TellI();
144 Skip_Comment(stream
);
146 if (stream
.GetC()==wxT('P'))
147 switch (stream
.GetC())
149 case wxT('3'): case wxT('6'):
159 #endif // wxUSE_STREAMS