void Skip_Comment(wxInputStream &stream) { wxTextInputStream text_stream(stream); if (stream.Peek()==wxT('#')) { text_stream.ReadLine(); Skip_Comment(stream); } }
bool wxPNMHandler::DoCanRead( wxInputStream& stream ) { Skip_Comment(stream); if ( stream.GetC() == 'P' ) { switch (stream.GetC()) { case '3': case '6': return true; } } return false; }
bool wxPNMHandler::DoCanRead( wxInputStream& stream ) { Skip_Comment(stream); // it's ok to modify the stream position here if ( stream.GetC() == 'P' ) { switch ( stream.GetC() ) { case '2': // ASCII Grey case '3': // ASCII RGB case '5': // RAW Grey case '6': // RAW RGB return true; } } return false; }
Object General_Read (Object port, int konst) { register FILE *f; register int c, str; Object ret; Check_Input_Port (port); Flush_Output (Curr_Output_Port); f = PORT(port)->file; str = PORT(port)->flags & P_STRING; while (1) { Reader_Getc; if (c == EOF) { ret = Eof; break; } if (Whitespace (c)) continue; if (c == ';') { comment: if (Skip_Comment (port) == EOF) { ret = Eof; break; } continue; } if (c == '(' || c == '[') { ret = Read_Sequence (port, 0, konst, c); } else if (c == '#') { ret = Read_Sharp (port, konst); if (TYPE(ret) == T_Special) /* it was a #! */ goto comment; } else { Reader_Ungetc; ret = Read_Atom (port, konst); } break; } Reader_Tweak_Stream; return ret; }
bool wxPNMHandler::LoadFile( wxImage *image, wxInputStream& stream, bool verbose, int WXUNUSED(index) ) { wxUint32 width, height; wxUint16 maxval; char c(0); image->Destroy(); /* * Read the PNM header */ wxBufferedInputStream buf_stream(stream); wxTextInputStream text_stream(buf_stream); Skip_Comment(buf_stream); if (buf_stream.GetC()==wxT('P')) c=buf_stream.GetC(); switch (c) { case wxT('2'): // ASCII Grey case wxT('3'): // ASCII RGB case wxT('5'): // RAW Grey case wxT('6'): break; default: if (verbose) { wxLogError(_("PNM: File format is not recognized.")); } return false; } text_stream.ReadLine(); // for the \n Skip_Comment(buf_stream); text_stream >> width >> height ; Skip_Comment(buf_stream); text_stream >> maxval; //cout << line << " " << width << " " << height << " " << maxval << endl; image->Create( width, height ); unsigned char *ptr = image->GetData(); if (!ptr) { if (verbose) { wxLogError( _("PNM: Couldn't allocate memory.") ); } return false; } if (c=='2') // Ascii GREY { wxUint32 value, size=width*height; for (wxUint32 i=0; i<size; ++i) { value=text_stream.Read32(); if ( maxval != 255 ) value = (255 * value)/maxval; *ptr++=(unsigned char)value; // R *ptr++=(unsigned char)value; // G *ptr++=(unsigned char)value; // B if ( !buf_stream ) { if (verbose) { wxLogError(_("PNM: File seems truncated.")); } return false; } } } if (c=='3') // Ascii RBG { wxUint32 value, size=3*width*height; for (wxUint32 i=0; i<size; ++i) { //this is very slow !!! //I wonder how we can make any better ? value=text_stream.Read32(); if ( maxval != 255 ) value = (255 * value)/maxval; *ptr++=(unsigned char)value; if ( !buf_stream ) { if (verbose) { wxLogError(_("PNM: File seems truncated.")); } return false; } } } if (c=='5') // Raw GREY { wxUint32 size=width*height; unsigned char value; for (wxUint32 i=0; i<size; ++i) { buf_stream.Read(&value,1); if ( maxval != 255 ) value = (255 * value)/maxval; *ptr++=value; // R *ptr++=value; // G *ptr++=value; // B if ( !buf_stream ) { if (verbose) { wxLogError(_("PNM: File seems truncated.")); } return false; } } } if ( c=='6' ) // Raw RGB { buf_stream.Read(ptr, 3*width*height); if ( maxval != 255 ) { for ( unsigned i = 0; i < 3*width*height; i++ ) ptr[i] = (255 * ptr[i])/maxval; } } image->SetMask( false ); const wxStreamError err = buf_stream.GetLastError(); return err == wxSTREAM_NO_ERROR || err == wxSTREAM_EOF; }
Object Read_Special (Object port, int konst) { Object ret; register int c, str; register FILE *f; #define READ_QUOTE(sym) \ ( ret = Read_Atom (port, konst),\ konst ? (ret = Const_Cons (ret, Null), Const_Cons (sym, ret))\ : (ret = Cons (ret, Null), Cons (sym, ret))) f = PORT(port)->file; str = PORT(port)->flags & P_STRING; again: Reader_Getc; switch (c) { case EOF: eof: Reader_Tweak_Stream; Reader_Error (port, "premature end of file"); case ';': if (Skip_Comment (port) == EOF) goto eof; goto again; case ']': case ')': SET(ret, T_Special, c); return ret; case '[': case '(': return Read_Sequence (port, 0, konst, c); case '\'': return READ_QUOTE(Sym_Quote); case '`': return READ_QUOTE(Sym_Quasiquote); case ',': Reader_Getc; if (c == EOF) goto eof; if (c == '@') { return READ_QUOTE(Sym_Unquote_Splicing); } else { Reader_Ungetc; return READ_QUOTE(Sym_Unquote); } case '"': return Read_String (port, konst); case '#': ret = Read_Sharp (port, konst); if (TYPE(ret) == T_Special) goto again; return ret; default: if (Whitespace (c)) goto again; Read_Reset (); if (c == '.') { Reader_Getc; if (c == EOF) goto eof; if (Whitespace (c)) { Reader_Ungetc; SET(ret, T_Special, '.'); return ret; } Read_Store ('.'); } while (!Whitespace (c) && !Delimiter (c) && c != EOF) { if (c == '\\') { Reader_Getc; if (c == EOF) break; } Read_Store (c); Reader_Getc; } Read_Store ('\0'); if (c != EOF) Reader_Ungetc; ret = Parse_Number (port, Read_Buf, 10); if (Nullp (ret)) ret = Intern (Read_Buf); return ret; } /*NOTREACHED*/ }