]>
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
6 // Copyright: (c) Sylvain Bougnoux
7 // Licence: wxWindows licence
8 /////////////////////////////////////////////////////////////////////////////
10 // For compilers that support precompilation, includes "wx.h".
11 #include "wx/wxprec.h"
20 #if wxUSE_IMAGE && wxUSE_PNM
22 #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
71 if (verbose
) wxLogError(_("PNM: File format is not recognized."));
75 text_stream
.ReadLine(); // for the \n
76 Skip_Comment(buf_stream
);
77 text_stream
>> width
>> height
;
78 Skip_Comment(buf_stream
);
79 text_stream
>> maxval
;
81 //cout << line << " " << width << " " << height << " " << maxval << endl;
82 image
->Create( width
, height
);
83 unsigned char *ptr
= image
->GetData();
87 wxLogError( _("PNM: Couldn't allocate memory.") );
92 if (c
=='2') // Ascii GREY
94 wxUint32 value
, size
=width
*height
;
95 for (wxUint32 i
=0; i
<size
; ++i
)
97 value
=text_stream
.Read32();
98 *ptr
++=(unsigned char)value
; // R
99 *ptr
++=(unsigned char)value
; // G
100 *ptr
++=(unsigned char)value
; // B
103 if (verbose
) wxLogError(_("PNM: File seems truncated."));
108 if (c
=='3') // Ascii RBG
110 wxUint32 value
, size
=3*width
*height
;
111 for (wxUint32 i
=0; i
<size
; ++i
)
113 //this is very slow !!!
114 //I wonder how we can make any better ?
115 value
=text_stream
.Read32();
116 *ptr
++=(unsigned char)value
;
120 if (verbose
) wxLogError(_("PNM: File seems truncated."));
125 if (c
=='5') // Raw GREY
127 wxUint32 size
=width
*height
;
129 for (wxUint32 i
=0; i
<size
; ++i
)
131 buf_stream
.Read(&value
,1);
137 if (verbose
) wxLogError(_("PNM: File seems truncated."));
142 if (c
=='6') // Raw RGB
143 buf_stream
.Read( ptr
, 3*width
*height
);
145 image
->SetMask( false );
147 const wxStreamError err
= buf_stream
.GetLastError();
148 return err
== wxSTREAM_NO_ERROR
|| err
== wxSTREAM_EOF
;
151 bool wxPNMHandler::SaveFile( wxImage
*image
, wxOutputStream
& stream
, bool WXUNUSED(verbose
) )
153 wxTextOutputStream
text_stream(stream
);
155 //text_stream << "P6" << endl
156 //<< image->GetWidth() << " " << image->GetHeight() << endl
158 text_stream
<< wxT("P6\n") << image
->GetWidth() << wxT(" ") << image
->GetHeight() << wxT("\n255\n");
159 stream
.Write(image
->GetData(),3*image
->GetWidth()*image
->GetHeight());
161 return stream
.IsOk();
164 bool wxPNMHandler::DoCanRead( wxInputStream
& stream
)
166 Skip_Comment(stream
);
168 if ( stream
.GetC() == 'P' )
170 switch ( stream
.GetC() )
172 case '2': // ASCII Grey
173 case '3': // ASCII RGB
174 case '5': // RAW Grey
184 #endif // wxUSE_STREAMS