]> git.saurik.com Git - wxWidgets.git/blame - src/common/imagpnm.cpp
added wxUSE_TOOLBAR_SIMPLE setting, enabled wxLongLong in configure by default
[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
7// Licence: wxWindows licence
8/////////////////////////////////////////////////////////////////////////////
9
10/*
11 We don't put pragma implement in this file because it is already present in
12 src/common/image.cpp
13*/
14
15// For compilers that support precompilation, includes "wx.h".
16#include "wx/wxprec.h"
17
18#ifdef __BORLANDC__
19#pragma hdrstop
20#endif
21
b9b32d5c
GRG
22#ifndef WX_PRECOMP
23# include "wx/setup.h"
24#endif
25
9ab6ee85 26#if wxUSE_PNM
b9b32d5c 27
a8d9809f
SB
28#include "wx/image.h"
29#include "wx/log.h"
58c837a4 30#include "wx/intl.h"
a8d9809f
SB
31#include "wx/txtstrm.h"
32
a8d9809f
SB
33//-----------------------------------------------------------------------------
34// wxBMPHandler
35//-----------------------------------------------------------------------------
36
37#if !USE_SHARED_LIBRARIES
38IMPLEMENT_DYNAMIC_CLASS(wxPNMHandler,wxImageHandler)
39#endif
40
9ab6ee85 41#if wxUSE_STREAMS
a8d9809f
SB
42
43void Skip_Comment(wxInputStream &stream)
44{
a8d9809f
SB
45 wxTextInputStream text_stream(stream);
46
995612e2 47 if (stream.Peek()==wxT('#'))
a8d9809f 48 {
41d1f82d 49 text_stream.ReadLine();
a8d9809f
SB
50 Skip_Comment(stream);
51 }
52}
53
58c837a4 54bool wxPNMHandler::LoadFile( wxImage *image, wxInputStream& stream, bool verbose, int WXUNUSED(index) )
a8d9809f
SB
55{
56 wxUint32 width, height;
57 wxUint16 maxval;
a8d9809f 58 char c(0);
995612e2 59
a8d9809f
SB
60 image->Destroy();
61
62 /*
63 * Read the PNM header
64 */
65
6319afe3
GL
66 wxBufferedInputStream buf_stream(stream);
67 wxTextInputStream text_stream(buf_stream);
a8d9809f 68
6319afe3 69 Skip_Comment(buf_stream);
223d09f6 70 if (buf_stream.GetC()==wxT('P')) c=buf_stream.GetC();
a8d9809f
SB
71
72 switch (c)
58c837a4
RR
73 {
74 case wxT('2'):
75 if (verbose) wxLogError(_("Loading Grey Ascii PNM image is not yet implemented."));
76 return FALSE;
77 case wxT('5'):
78 if (verbose) wxLogError(_("Loading Grey Raw PNM image is not yet implemented."));
79 return FALSE;
80 case wxT('3'):
81 case wxT('6'): break;
82 default:
83 if (verbose) wxLogError(_("PNM: File format is not recognized."));
84 return FALSE;
85 }
a8d9809f 86
41d1f82d 87 text_stream.ReadLine(); // for the \n
6319afe3 88 Skip_Comment(buf_stream);
a8d9809f 89 text_stream >> width >> height ;
995612e2 90 Skip_Comment(buf_stream);
a8d9809f
SB
91 text_stream >> maxval;
92
a2b8bd55 93 //cout << line << " " << width << " " << height << " " << maxval << endl;
a8d9809f
SB
94 image->Create( width, height );
95 unsigned char *ptr = image->GetData();
96 if (!ptr)
97 {
58c837a4
RR
98 if (verbose)
99 wxLogError( _("PNM: Couldn't allocate memory.") );
995612e2 100 return FALSE;
a8d9809f
SB
101 }
102
a2b8bd55 103 if (c=='3') // Ascii RBG
995612e2
VZ
104 {
105 wxUint32 value, size=3*width*height;
106 for (wxUint32 i=0; i<size; ++i)
107 {
108 //this is very slow !!!
109 //I wonder how we can make any better ?
110 value=text_stream.Read32();
111 *ptr++=(unsigned char)value;
112
113 if (buf_stream.LastError()!=wxSTREAM_NOERROR)
114 {
58c837a4 115 if (verbose) wxLogError(_("PNM: File seems truncated."));
995612e2
VZ
116 return FALSE;
117 }
118 }
a8d9809f
SB
119 }
120 if (c=='6') // Raw RGB
503aa33d 121 buf_stream.Read( ptr, 3*width*height );
a8d9809f
SB
122
123 image->SetMask( FALSE );
124
503aa33d 125 return (buf_stream.LastError()==wxStream_NOERROR || buf_stream.LastError()==wxStream_EOF);
a8d9809f
SB
126}
127
128bool wxPNMHandler::SaveFile( wxImage *image, wxOutputStream& stream, bool WXUNUSED(verbose) )
129{
130 wxTextOutputStream text_stream(stream);
995612e2
VZ
131
132 //text_stream << "P6" << endl
133 //<< image->GetWidth() << " " << image->GetHeight() << endl
a8d9809f
SB
134 //<< "255" << endl;
135 text_stream << "P6\n" << image->GetWidth() << " " << image->GetHeight() << "\n255\n";
136 stream.Write(image->GetData(),3*image->GetWidth()*image->GetHeight());
137
138 return (stream.LastError()==wxStream_NOERROR);
139}
140
995612e2 141bool wxPNMHandler::DoCanRead( wxInputStream& stream )
0828c087 142{
995612e2 143 off_t pos = stream.TellI();
93dfff5a
SB
144
145 Skip_Comment(stream);
146
995612e2
VZ
147 if ( stream.GetC() == 'P' )
148 {
149 switch (stream.GetC())
150 {
151 case '3':
152 case '6':
153 stream.SeekI(pos);
154 return TRUE;
155 }
156 }
93dfff5a
SB
157
158 stream.SeekI(pos);
159 return FALSE;
0828c087
VS
160}
161
162
9ab6ee85 163#endif // wxUSE_STREAMS
a8d9809f 164
9ab6ee85 165#endif // wxUSE_PNM