]>
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 /////////////////////////////////////////////////////////////////////////////
10 #if defined(__GNUG__) && !defined(NO_GCC_PRAGMA)
11 #pragma implementation "imagpnm.h"
14 // For compilers that support precompilation, includes "wx.h".
15 #include "wx/wxprec.h"
22 # include "wx/setup.h"
25 #if wxUSE_IMAGE && wxUSE_PNM
27 #include "wx/imagpnm.h"
30 #include "wx/txtstrm.h"
32 //-----------------------------------------------------------------------------
34 //-----------------------------------------------------------------------------
36 IMPLEMENT_DYNAMIC_CLASS(wxPNMHandler
,wxImageHandler
)
40 void Skip_Comment(wxInputStream
&stream
)
42 wxTextInputStream
text_stream(stream
);
44 if (stream
.Peek()==wxT('#'))
46 text_stream
.ReadLine();
51 bool wxPNMHandler::LoadFile( wxImage
*image
, wxInputStream
& stream
, bool verbose
, int WXUNUSED(index
) )
53 wxUint32 width
, height
;
63 wxBufferedInputStream
buf_stream(stream
);
64 wxTextInputStream
text_stream(buf_stream
);
66 Skip_Comment(buf_stream
);
67 if (buf_stream
.GetC()==wxT('P')) c
=buf_stream
.GetC();
71 case wxT('2'): // ASCII Grey
72 case wxT('3'): // ASCII RGB
73 case wxT('5'): // RAW Grey
76 if (verbose
) wxLogError(_("PNM: File format is not recognized."));
80 text_stream
.ReadLine(); // for the \n
81 Skip_Comment(buf_stream
);
82 text_stream
>> width
>> height
;
83 Skip_Comment(buf_stream
);
84 text_stream
>> maxval
;
86 //cout << line << " " << width << " " << height << " " << maxval << endl;
87 image
->Create( width
, height
);
88 unsigned char *ptr
= image
->GetData();
92 wxLogError( _("PNM: Couldn't allocate memory.") );
97 if (c
=='2') // Ascii GREY
99 wxUint32 value
, size
=width
*height
;
100 for (wxUint32 i
=0; i
<size
; ++i
)
102 value
=text_stream
.Read32();
103 *ptr
++=(unsigned char)value
; // R
104 *ptr
++=(unsigned char)value
; // G
105 *ptr
++=(unsigned char)value
; // B
108 if (verbose
) wxLogError(_("PNM: File seems truncated."));
113 if (c
=='3') // Ascii RBG
115 wxUint32 value
, size
=3*width
*height
;
116 for (wxUint32 i
=0; i
<size
; ++i
)
118 //this is very slow !!!
119 //I wonder how we can make any better ?
120 value
=text_stream
.Read32();
121 *ptr
++=(unsigned char)value
;
125 if (verbose
) wxLogError(_("PNM: File seems truncated."));
130 if (c
=='5') // Raw GREY
132 wxUint32 size
=width
*height
;
134 for (wxUint32 i
=0; i
<size
; ++i
)
136 buf_stream
.Read(&value
,1);
142 if (verbose
) wxLogError(_("PNM: File seems truncated."));
147 if (c
=='6') // Raw RGB
148 buf_stream
.Read( ptr
, 3*width
*height
);
150 image
->SetMask( false );
152 const wxStreamError err
= buf_stream
.GetLastError();
153 return err
== wxSTREAM_NO_ERROR
|| err
== wxSTREAM_EOF
;
156 bool wxPNMHandler::SaveFile( wxImage
*image
, wxOutputStream
& stream
, bool WXUNUSED(verbose
) )
158 wxTextOutputStream
text_stream(stream
);
160 //text_stream << "P6" << endl
161 //<< image->GetWidth() << " " << image->GetHeight() << endl
163 text_stream
<< wxT("P6\n") << image
->GetWidth() << wxT(" ") << image
->GetHeight() << wxT("\n255\n");
164 stream
.Write(image
->GetData(),3*image
->GetWidth()*image
->GetHeight());
166 return stream
.IsOk();
169 bool wxPNMHandler::DoCanRead( wxInputStream
& stream
)
171 Skip_Comment(stream
);
173 if ( stream
.GetC() == 'P' )
175 switch ( stream
.GetC() )
177 case '2': // ASCII Grey
178 case '3': // ASCII RGB
179 case '5': // RAW Grey
189 #endif // wxUSE_STREAMS