]>
Commit | Line | Data |
---|---|---|
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 | 36 | IMPLEMENT_DYNAMIC_CLASS(wxPNMHandler,wxImageHandler) |
a8d9809f | 37 | |
9ab6ee85 | 38 | #if wxUSE_STREAMS |
a8d9809f SB |
39 | |
40 | void 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 | 51 | bool 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 RR |
70 | { |
71 | case wxT('2'): | |
72 | if (verbose) wxLogError(_("Loading Grey Ascii PNM image is not yet implemented.")); | |
73 | return FALSE; | |
74 | case wxT('5'): | |
75 | if (verbose) wxLogError(_("Loading Grey Raw PNM image is not yet implemented.")); | |
76 | return FALSE; | |
77 | case wxT('3'): | |
78 | case wxT('6'): break; | |
79 | default: | |
80 | if (verbose) wxLogError(_("PNM: File format is not recognized.")); | |
81 | return FALSE; | |
82 | } | |
a8d9809f | 83 | |
41d1f82d | 84 | text_stream.ReadLine(); // for the \n |
6319afe3 | 85 | Skip_Comment(buf_stream); |
a8d9809f | 86 | text_stream >> width >> height ; |
995612e2 | 87 | Skip_Comment(buf_stream); |
a8d9809f SB |
88 | text_stream >> maxval; |
89 | ||
a2b8bd55 | 90 | //cout << line << " " << width << " " << height << " " << maxval << endl; |
a8d9809f SB |
91 | image->Create( width, height ); |
92 | unsigned char *ptr = image->GetData(); | |
93 | if (!ptr) | |
94 | { | |
58c837a4 RR |
95 | if (verbose) |
96 | wxLogError( _("PNM: Couldn't allocate memory.") ); | |
995612e2 | 97 | return FALSE; |
a8d9809f SB |
98 | } |
99 | ||
a2b8bd55 | 100 | if (c=='3') // Ascii RBG |
995612e2 VZ |
101 | { |
102 | wxUint32 value, size=3*width*height; | |
103 | for (wxUint32 i=0; i<size; ++i) | |
104 | { | |
105 | //this is very slow !!! | |
106 | //I wonder how we can make any better ? | |
107 | value=text_stream.Read32(); | |
108 | *ptr++=(unsigned char)value; | |
109 | ||
2b5f62a0 | 110 | if ( !buf_stream ) |
995612e2 | 111 | { |
58c837a4 | 112 | if (verbose) wxLogError(_("PNM: File seems truncated.")); |
995612e2 VZ |
113 | return FALSE; |
114 | } | |
115 | } | |
a8d9809f SB |
116 | } |
117 | if (c=='6') // Raw RGB | |
503aa33d | 118 | buf_stream.Read( ptr, 3*width*height ); |
a8d9809f SB |
119 | |
120 | image->SetMask( FALSE ); | |
121 | ||
2b5f62a0 VZ |
122 | const wxStreamError err = buf_stream.GetLastError(); |
123 | return err == wxSTREAM_NO_ERROR || err == wxSTREAM_EOF; | |
a8d9809f SB |
124 | } |
125 | ||
126 | bool wxPNMHandler::SaveFile( wxImage *image, wxOutputStream& stream, bool WXUNUSED(verbose) ) | |
127 | { | |
128 | wxTextOutputStream text_stream(stream); | |
995612e2 VZ |
129 | |
130 | //text_stream << "P6" << endl | |
131 | //<< image->GetWidth() << " " << image->GetHeight() << endl | |
a8d9809f | 132 | //<< "255" << endl; |
2b5f62a0 | 133 | text_stream << wxT("P6\n") << image->GetWidth() << wxT(" ") << image->GetHeight() << wxT("\n255\n"); |
a8d9809f SB |
134 | stream.Write(image->GetData(),3*image->GetWidth()*image->GetHeight()); |
135 | ||
2b5f62a0 | 136 | return stream.IsOk(); |
a8d9809f SB |
137 | } |
138 | ||
995612e2 | 139 | bool wxPNMHandler::DoCanRead( wxInputStream& stream ) |
0828c087 | 140 | { |
93dfff5a SB |
141 | Skip_Comment(stream); |
142 | ||
995612e2 VZ |
143 | if ( stream.GetC() == 'P' ) |
144 | { | |
145 | switch (stream.GetC()) | |
146 | { | |
147 | case '3': | |
148 | case '6': | |
995612e2 VZ |
149 | return TRUE; |
150 | } | |
151 | } | |
93dfff5a | 152 | |
93dfff5a | 153 | return FALSE; |
0828c087 VS |
154 | } |
155 | ||
156 | ||
9ab6ee85 | 157 | #endif // wxUSE_STREAMS |
a8d9809f | 158 | |
9ab6ee85 | 159 | #endif // wxUSE_PNM |