]>
git.saurik.com Git - wxWidgets.git/blob - src/common/imagpnm.cpp
1 /////////////////////////////////////////////////////////////////////////////
2 // Name: src/common/imagpnm.cpp
3 // Purpose: wxImage PNM handler
4 // Author: Sylvain Bougnoux
5 // Copyright: (c) Sylvain Bougnoux
6 // Licence: wxWindows licence
7 /////////////////////////////////////////////////////////////////////////////
9 // For compilers that support precompilation, includes "wx.h".
10 #include "wx/wxprec.h"
16 #if wxUSE_IMAGE && wxUSE_PNM
18 #include "wx/imagpnm.h"
25 #include "wx/txtstrm.h"
27 //-----------------------------------------------------------------------------
29 //-----------------------------------------------------------------------------
31 IMPLEMENT_DYNAMIC_CLASS(wxPNMHandler
,wxImageHandler
)
35 void Skip_Comment(wxInputStream
&stream
)
37 wxTextInputStream
text_stream(stream
);
39 if (stream
.Peek()==wxT('#'))
41 text_stream
.ReadLine();
46 bool wxPNMHandler::LoadFile( wxImage
*image
, wxInputStream
& stream
, bool verbose
, int WXUNUSED(index
) )
48 wxUint32 width
, height
;
58 wxBufferedInputStream
buf_stream(stream
);
59 wxTextInputStream
text_stream(buf_stream
);
61 Skip_Comment(buf_stream
);
62 if (buf_stream
.GetC()==wxT('P')) c
=buf_stream
.GetC();
66 case wxT('2'): // ASCII Grey
67 case wxT('3'): // ASCII RGB
68 case wxT('5'): // RAW Grey
73 wxLogError(_("PNM: File format is not recognized."));
78 text_stream
.ReadLine(); // for the \n
79 Skip_Comment(buf_stream
);
80 text_stream
>> width
>> height
;
81 Skip_Comment(buf_stream
);
82 text_stream
>> maxval
;
84 //cout << line << " " << width << " " << height << " " << maxval << endl;
85 image
->Create( width
, height
);
86 unsigned char *ptr
= image
->GetData();
91 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();
104 value
= (255 * value
)/maxval
;
105 *ptr
++=(unsigned char)value
; // R
106 *ptr
++=(unsigned char)value
; // G
107 *ptr
++=(unsigned char)value
; // B
112 wxLogError(_("PNM: File seems truncated."));
118 if (c
=='3') // Ascii RBG
120 wxUint32 value
, size
=3*width
*height
;
121 for (wxUint32 i
=0; i
<size
; ++i
)
123 //this is very slow !!!
124 //I wonder how we can make any better ?
125 value
=text_stream
.Read32();
127 value
= (255 * value
)/maxval
;
128 *ptr
++=(unsigned char)value
;
134 wxLogError(_("PNM: File seems truncated."));
140 if (c
=='5') // Raw GREY
142 wxUint32 size
=width
*height
;
144 for (wxUint32 i
=0; i
<size
; ++i
)
146 buf_stream
.Read(&value
,1);
148 value
= (255 * value
)/maxval
;
156 wxLogError(_("PNM: File seems truncated."));
163 if ( c
=='6' ) // Raw RGB
165 buf_stream
.Read(ptr
, 3*width
*height
);
168 for ( unsigned i
= 0; i
< 3*width
*height
; i
++ )
169 ptr
[i
] = (255 * ptr
[i
])/maxval
;
173 image
->SetMask( false );
175 const wxStreamError err
= buf_stream
.GetLastError();
176 return err
== wxSTREAM_NO_ERROR
|| err
== wxSTREAM_EOF
;
179 bool wxPNMHandler::SaveFile( wxImage
*image
, wxOutputStream
& stream
, bool WXUNUSED(verbose
) )
181 wxTextOutputStream
text_stream(stream
);
183 //text_stream << "P6" << endl
184 //<< image->GetWidth() << " " << image->GetHeight() << endl
186 text_stream
<< wxT("P6\n") << image
->GetWidth() << wxT(" ") << image
->GetHeight() << wxT("\n255\n");
187 stream
.Write(image
->GetData(),3*image
->GetWidth()*image
->GetHeight());
189 return stream
.IsOk();
192 bool wxPNMHandler::DoCanRead( wxInputStream
& stream
)
194 Skip_Comment(stream
);
196 // it's ok to modify the stream position here
197 if ( stream
.GetC() == 'P' )
199 switch ( stream
.GetC() )
201 case '2': // ASCII Grey
202 case '3': // ASCII RGB
203 case '5': // RAW Grey
213 #endif // wxUSE_STREAMS
215 #endif // wxUSE_IMAGE && wxUSE_PNM