]>
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 #pragma implementation "imagpnm.h"
14 // For compilers that support precompilation, includes "wx.h".
15 #include "wx/wxprec.h"
22 # include "wx/setup.h"
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();
72 if (verbose
) wxLogError(_("Loading Grey Ascii PNM image is not yet implemented."));
75 if (verbose
) wxLogError(_("Loading Grey Raw PNM image is not yet implemented."));
80 if (verbose
) wxLogError(_("PNM: File format is not recognized."));
84 text_stream
.ReadLine(); // for the \n
85 Skip_Comment(buf_stream
);
86 text_stream
>> width
>> height
;
87 Skip_Comment(buf_stream
);
88 text_stream
>> maxval
;
90 //cout << line << " " << width << " " << height << " " << maxval << endl;
91 image
->Create( width
, height
);
92 unsigned char *ptr
= image
->GetData();
96 wxLogError( _("PNM: Couldn't allocate memory.") );
100 if (c
=='3') // Ascii RBG
102 wxUint32 value
, size
=3*width
*height
;
103 for (wxUint32 i
=0; i
<size
; ++i
)
105 //this is very slow !!!
106 //I wonder how we can make any better ?
107 value
=text_stream
.Read32();
108 *ptr
++=(unsigned char)value
;
110 if (buf_stream
.LastError()!=wxSTREAM_NOERROR
)
112 if (verbose
) wxLogError(_("PNM: File seems truncated."));
117 if (c
=='6') // Raw RGB
118 buf_stream
.Read( ptr
, 3*width
*height
);
120 image
->SetMask( FALSE
);
122 return (buf_stream
.LastError()==wxStream_NOERROR
|| buf_stream
.LastError()==wxStream_EOF
);
125 bool wxPNMHandler::SaveFile( wxImage
*image
, wxOutputStream
& stream
, bool WXUNUSED(verbose
) )
127 wxTextOutputStream
text_stream(stream
);
129 //text_stream << "P6" << endl
130 //<< image->GetWidth() << " " << image->GetHeight() << endl
132 text_stream
<< "P6\n" << image
->GetWidth() << " " << image
->GetHeight() << "\n255\n";
133 stream
.Write(image
->GetData(),3*image
->GetWidth()*image
->GetHeight());
135 return (stream
.LastError()==wxStream_NOERROR
);
138 bool wxPNMHandler::DoCanRead( wxInputStream
& stream
)
140 off_t pos
= stream
.TellI();
142 Skip_Comment(stream
);
144 if ( stream
.GetC() == 'P' )
146 switch (stream
.GetC())
160 #endif // wxUSE_STREAMS