]> git.saurik.com Git - wxWidgets.git/blame - src/common/imagpnm.cpp
don't try to subclass tab control using the same window proc for our class, this...
[wxWidgets.git] / src / common / imagpnm.cpp
CommitLineData
a8d9809f
SB
1/////////////////////////////////////////////////////////////////////////////
2// Name: imagpnm.cpp
3// Purpose: wxImage PNM handler
4// Author: Sylvain Bougnoux
5// RCS-ID: $Id$
6// Copyright: (c) Sylvain Bougnoux
65571936 7// Licence: wxWindows licence
a8d9809f
SB
8/////////////////////////////////////////////////////////////////////////////
9
14f355c2 10#if defined(__GNUG__) && !defined(NO_GCC_PRAGMA)
8f493002
VS
11#pragma implementation "imagpnm.h"
12#endif
a8d9809f
SB
13
14// For compilers that support precompilation, includes "wx.h".
15#include "wx/wxprec.h"
16
17#ifdef __BORLANDC__
18#pragma hdrstop
19#endif
20
b9b32d5c
GRG
21#ifndef WX_PRECOMP
22# include "wx/setup.h"
23#endif
24
c96ea657 25#if wxUSE_IMAGE && wxUSE_PNM
b9b32d5c 26
8f493002 27#include "wx/imagpnm.h"
a8d9809f 28#include "wx/log.h"
58c837a4 29#include "wx/intl.h"
a8d9809f
SB
30#include "wx/txtstrm.h"
31
a8d9809f
SB
32//-----------------------------------------------------------------------------
33// wxBMPHandler
34//-----------------------------------------------------------------------------
35
a8d9809f 36IMPLEMENT_DYNAMIC_CLASS(wxPNMHandler,wxImageHandler)
a8d9809f 37
9ab6ee85 38#if wxUSE_STREAMS
a8d9809f
SB
39
40void Skip_Comment(wxInputStream &stream)
41{
2b5f62a0 42 wxTextInputStream text_stream(stream);
a8d9809f 43
2b5f62a0 44 if (stream.Peek()==wxT('#'))
a8d9809f 45 {
2b5f62a0
VZ
46 text_stream.ReadLine();
47 Skip_Comment(stream);
a8d9809f
SB
48 }
49}
50
58c837a4 51bool wxPNMHandler::LoadFile( wxImage *image, wxInputStream& stream, bool verbose, int WXUNUSED(index) )
a8d9809f
SB
52{
53 wxUint32 width, height;
54 wxUint16 maxval;
a8d9809f 55 char c(0);
995612e2 56
a8d9809f
SB
57 image->Destroy();
58
59 /*
60 * Read the PNM header
61 */
62
6319afe3
GL
63 wxBufferedInputStream buf_stream(stream);
64 wxTextInputStream text_stream(buf_stream);
a8d9809f 65
6319afe3 66 Skip_Comment(buf_stream);
223d09f6 67 if (buf_stream.GetC()==wxT('P')) c=buf_stream.GetC();
a8d9809f
SB
68
69 switch (c)
58c837a4 70 {
3e8711ce
JS
71 case wxT('2'): // ASCII Grey
72 case wxT('3'): // ASCII RGB
73 case wxT('5'): // RAW Grey
7beb59f3 74 case wxT('6'): break;
58c837a4
RR
75 default:
76 if (verbose) wxLogError(_("PNM: File format is not recognized."));
7beb59f3 77 return false;
58c837a4 78 }
a8d9809f 79
41d1f82d 80 text_stream.ReadLine(); // for the \n
6319afe3 81 Skip_Comment(buf_stream);
a8d9809f 82 text_stream >> width >> height ;
995612e2 83 Skip_Comment(buf_stream);
a8d9809f
SB
84 text_stream >> maxval;
85
a2b8bd55 86 //cout << line << " " << width << " " << height << " " << maxval << endl;
a8d9809f
SB
87 image->Create( width, height );
88 unsigned char *ptr = image->GetData();
89 if (!ptr)
90 {
58c837a4
RR
91 if (verbose)
92 wxLogError( _("PNM: Couldn't allocate memory.") );
7beb59f3 93 return false;
a8d9809f
SB
94 }
95
3e8711ce
JS
96
97 if (c=='2') // Ascii GREY
98 {
99 wxUint32 value, size=width*height;
100 for (wxUint32 i=0; i<size; ++i)
101 {
102 value=text_stream.Read32();
103 *ptr++=(unsigned char)value; // R
104 *ptr++=(unsigned char)value; // G
105 *ptr++=(unsigned char)value; // B
106 if ( !buf_stream )
107 {
108 if (verbose) wxLogError(_("PNM: File seems truncated."));
109 return false;
110 }
111 }
112 }
a2b8bd55 113 if (c=='3') // Ascii RBG
995612e2
VZ
114 {
115 wxUint32 value, size=3*width*height;
116 for (wxUint32 i=0; i<size; ++i)
117 {
118 //this is very slow !!!
119 //I wonder how we can make any better ?
120 value=text_stream.Read32();
121 *ptr++=(unsigned char)value;
122
2b5f62a0 123 if ( !buf_stream )
995612e2 124 {
58c837a4 125 if (verbose) wxLogError(_("PNM: File seems truncated."));
7beb59f3 126 return false;
995612e2
VZ
127 }
128 }
a8d9809f 129 }
3e8711ce
JS
130 if (c=='5') // Raw GREY
131 {
132 wxUint32 size=width*height;
133 unsigned char value;
134 for (wxUint32 i=0; i<size; ++i)
135 {
136 buf_stream.Read(&value,1);
137 *ptr++=value; // R
138 *ptr++=value; // G
139 *ptr++=value; // B
140 if ( !buf_stream )
141 {
142 if (verbose) wxLogError(_("PNM: File seems truncated."));
143 return false;
144 }
145 }
146 }
a8d9809f 147 if (c=='6') // Raw RGB
503aa33d 148 buf_stream.Read( ptr, 3*width*height );
a8d9809f 149
7beb59f3 150 image->SetMask( false );
a8d9809f 151
2b5f62a0
VZ
152 const wxStreamError err = buf_stream.GetLastError();
153 return err == wxSTREAM_NO_ERROR || err == wxSTREAM_EOF;
a8d9809f
SB
154}
155
156bool wxPNMHandler::SaveFile( wxImage *image, wxOutputStream& stream, bool WXUNUSED(verbose) )
157{
158 wxTextOutputStream text_stream(stream);
995612e2
VZ
159
160 //text_stream << "P6" << endl
161 //<< image->GetWidth() << " " << image->GetHeight() << endl
a8d9809f 162 //<< "255" << endl;
2b5f62a0 163 text_stream << wxT("P6\n") << image->GetWidth() << wxT(" ") << image->GetHeight() << wxT("\n255\n");
a8d9809f
SB
164 stream.Write(image->GetData(),3*image->GetWidth()*image->GetHeight());
165
2b5f62a0 166 return stream.IsOk();
a8d9809f
SB
167}
168
995612e2 169bool wxPNMHandler::DoCanRead( wxInputStream& stream )
0828c087 170{
93dfff5a
SB
171 Skip_Comment(stream);
172
995612e2
VZ
173 if ( stream.GetC() == 'P' )
174 {
175 switch (stream.GetC())
176 {
177 case '3':
178 case '6':
7beb59f3 179 return true;
995612e2
VZ
180 }
181 }
93dfff5a 182
7beb59f3 183 return false;
0828c087
VS
184}
185
186
9ab6ee85 187#endif // wxUSE_STREAMS
a8d9809f 188
9ab6ee85 189#endif // wxUSE_PNM