]>
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"
17 #if wxUSE_IMAGE && wxUSE_PNM
19 #include "wx/imagpnm.h"
26 #include "wx/txtstrm.h"
28 //-----------------------------------------------------------------------------
30 //-----------------------------------------------------------------------------
32 IMPLEMENT_DYNAMIC_CLASS(wxPNMHandler
,wxImageHandler
)
36 void Skip_Comment(wxInputStream
&stream
)
38 wxTextInputStream
text_stream(stream
);
40 if (stream
.Peek()==wxT('#'))
42 text_stream
.ReadLine();
47 bool wxPNMHandler::LoadFile( wxImage
*image
, wxInputStream
& stream
, bool verbose
, int WXUNUSED(index
) )
49 wxUint32 width
, height
;
59 wxBufferedInputStream
buf_stream(stream
);
60 wxTextInputStream
text_stream(buf_stream
);
62 Skip_Comment(buf_stream
);
63 if (buf_stream
.GetC()==wxT('P')) c
=buf_stream
.GetC();
67 case wxT('2'): // ASCII Grey
68 case wxT('3'): // ASCII RGB
69 case wxT('5'): // RAW Grey
72 if (verbose
) wxLogError(_("PNM: File format is not recognized."));
76 text_stream
.ReadLine(); // for the \n
77 Skip_Comment(buf_stream
);
78 text_stream
>> width
>> height
;
79 Skip_Comment(buf_stream
);
80 text_stream
>> maxval
;
82 //cout << line << " " << width << " " << height << " " << maxval << endl;
83 image
->Create( width
, height
);
84 unsigned char *ptr
= image
->GetData();
88 wxLogError( _("PNM: Couldn't allocate memory.") );
93 if (c
=='2') // Ascii GREY
95 wxUint32 value
, size
=width
*height
;
96 for (wxUint32 i
=0; i
<size
; ++i
)
98 value
=text_stream
.Read32();
100 value
= (255 * value
)/maxval
;
101 *ptr
++=(unsigned char)value
; // R
102 *ptr
++=(unsigned char)value
; // G
103 *ptr
++=(unsigned char)value
; // B
106 if (verbose
) wxLogError(_("PNM: File seems truncated."));
111 if (c
=='3') // Ascii RBG
113 wxUint32 value
, size
=3*width
*height
;
114 for (wxUint32 i
=0; i
<size
; ++i
)
116 //this is very slow !!!
117 //I wonder how we can make any better ?
118 value
=text_stream
.Read32();
120 value
= (255 * value
)/maxval
;
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);
138 value
= (255 * value
)/maxval
;
144 if (verbose
) wxLogError(_("PNM: File seems truncated."));
150 if ( c
=='6' ) // Raw RGB
152 buf_stream
.Read(ptr
, 3*width
*height
);
155 for ( unsigned i
= 0; i
< 3*width
*height
; i
++ )
156 ptr
[i
] = (255 * ptr
[i
])/maxval
;
160 image
->SetMask( false );
162 const wxStreamError err
= buf_stream
.GetLastError();
163 return err
== wxSTREAM_NO_ERROR
|| err
== wxSTREAM_EOF
;
166 bool wxPNMHandler::SaveFile( wxImage
*image
, wxOutputStream
& stream
, bool WXUNUSED(verbose
) )
168 wxTextOutputStream
text_stream(stream
);
170 //text_stream << "P6" << endl
171 //<< image->GetWidth() << " " << image->GetHeight() << endl
173 text_stream
<< wxT("P6\n") << image
->GetWidth() << wxT(" ") << image
->GetHeight() << wxT("\n255\n");
174 stream
.Write(image
->GetData(),3*image
->GetWidth()*image
->GetHeight());
176 return stream
.IsOk();
179 bool wxPNMHandler::DoCanRead( wxInputStream
& stream
)
181 Skip_Comment(stream
);
183 if ( stream
.GetC() == 'P' )
185 switch ( stream
.GetC() )
187 case '2': // ASCII Grey
188 case '3': // ASCII RGB
189 case '5': // RAW Grey
199 #endif // wxUSE_STREAMS
201 #endif // wxUSE_IMAGE && wxUSE_PNM