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; }
wxString IACFile::ReadToken(wxInputStream &file) { // 0 = read chars until digit // 1 = read digits until no digit // 2 = token found wxString token = wxEmptyString; int mode = 0; while (file.IsOk() && mode != 2) { int c = file.GetC(); if (c != wxEOF && c <= 128) { if (c == '\n' && m_tokensI > 0) { m_newlineTokens.push_back(m_tokensI + 1); } m_RawData.Append((char)c); switch (mode) { case 0: if (isdigit(c)) { token.Append((char)c); mode = 1; } break; case 1: if (isdigit(c) || c == '/') { token.Append((char)c); } else { if (token.Len() == 5) { // token found!! mode = 2; } else { token.Empty(); } } break; /* this is dead code case 2: mode = 0; break; */ } // case } } // while if (mode != 2) { token.Empty(); } return token; }
static void SkipWhitespace(wxInputStream& input, unsigned int& lineNumber) { char c; while (!input.Eof()) { c = input.Peek(); if (c == '\n') { ++lineNumber; } else if (c == '-') { input.GetC(); char c2 = input.Peek(); if (c2 == '-') { // Lua single line comment. while (!input.Eof() && input.GetC() != '\n') { } ++lineNumber; continue; } } else if (c == '/') { input.GetC(); char c2 = input.Peek(); if (c2 == '*') { // C++ block comment. input.GetC(); while (!input.Eof()) { c = input.GetC(); if (c == '\n') { ++lineNumber; } if (c == '*' && input.Peek() == '/') { input.GetC(); break; } } continue; } else if (c2 == '/') { // C++ single line comment. while (!input.Eof() && input.GetC() != '\n') { } ++lineNumber; continue; } else { input.Ungetch(c); break; } } if (!IsSpace(c)) { break; } input.GetC(); } }
bool GetToken(wxInputStream& input, wxString& result, unsigned int& lineNumber) { result.Empty(); SkipWhitespace(input, lineNumber); // Reached the end of the file. if (input.Eof()) { return false; } char c = input.GetC(); if (c == '\"') { // Quoted string, search for the end quote. do { result += c; c = input.GetC(); } while (input.IsOk() && c != '\"'); result += c; return true; } char n = input.Peek(); if (IsDigit(c) || (c == '.' && IsDigit(n)) || (c == '-' && IsDigit(n))) { bool hasDecimal = false; while (!IsSpace(c)) { result.Append(c); if (input.Eof()) { return true; } c = input.Peek(); if (!IsDigit(c) && c != '.') { return true; } input.GetC(); if (c == '\n') { ++lineNumber; return true; } } } else { if (IsSymbol(c)) { result = c; return true; } while (!IsSpace(c) && !input.Eof()) { result.Append(c); if (IsSymbol(input.Peek())) { break; } c = input.GetC(); if (c == '\n') { ++lineNumber; return true; } } } return true; }
bool wxBMPHandler::DoLoadDib(wxImage * image, int width, int height, int bpp, int ncolors, int comp, wxFileOffset bmpOffset, wxInputStream& stream, bool verbose, bool IsBmp, bool hasPalette) { wxInt32 aDword, rmask = 0, gmask = 0, bmask = 0, amask = 0; int rshift = 0, gshift = 0, bshift = 0, ashift = 0; int rbits = 0, gbits = 0, bbits = 0; wxInt32 dbuf[4]; wxInt8 bbuf[4]; wxUint8 aByte; wxUint16 aWord; // allocate space for palette if needed: BMPPalette *cmap; if ( bpp < 16 ) { cmap = new BMPPalette[ncolors]; if ( !cmap ) { if (verbose) { wxLogError(_("BMP: Couldn't allocate memory.")); } return false; } } else // no palette { cmap = NULL; } wxON_BLOCK_EXIT1(&BMPPalette::Free, cmap); // destroy existing here instead of: image->Destroy(); image->Create(width, height); unsigned char *ptr = image->GetData(); if ( !ptr ) { if ( verbose ) { wxLogError( _("BMP: Couldn't allocate memory.") ); } return false; } unsigned char *alpha; if ( bpp == 32 ) { // tell the image to allocate an alpha buffer image->SetAlpha(); alpha = image->GetAlpha(); if ( !alpha ) { if ( verbose ) { wxLogError(_("BMP: Couldn't allocate memory.")); } return false; } } else // no alpha { alpha = NULL; } // Reading the palette, if it exists: if ( bpp < 16 && ncolors != 0 ) { unsigned char* r = new unsigned char[ncolors]; unsigned char* g = new unsigned char[ncolors]; unsigned char* b = new unsigned char[ncolors]; for (int j = 0; j < ncolors; j++) { if (hasPalette) { stream.Read(bbuf, 4); cmap[j].b = bbuf[0]; cmap[j].g = bbuf[1]; cmap[j].r = bbuf[2]; r[j] = cmap[j].r; g[j] = cmap[j].g; b[j] = cmap[j].b; } else { //used in reading .ico file mask r[j] = cmap[j].r = g[j] = cmap[j].g = b[j] = cmap[j].b = ( j ? 255 : 0 ); } } #if wxUSE_PALETTE // Set the palette for the wxImage image->SetPalette(wxPalette(ncolors, r, g, b)); #endif // wxUSE_PALETTE delete[] r; delete[] g; delete[] b; } else if ( bpp == 16 || bpp == 32 ) { if ( comp == BI_BITFIELDS ) { int bit; stream.Read(dbuf, 4 * 3); rmask = wxINT32_SWAP_ON_BE(dbuf[0]); gmask = wxINT32_SWAP_ON_BE(dbuf[1]); bmask = wxINT32_SWAP_ON_BE(dbuf[2]); // find shift amount (Least significant bit of mask) for (bit = bpp-1; bit>=0; bit--) { if (bmask & (1 << bit)) bshift = bit; if (gmask & (1 << bit)) gshift = bit; if (rmask & (1 << bit)) rshift = bit; } // Find number of bits in mask (MSB-LSB+1) for (bit = 0; bit < bpp; bit++) { if (bmask & (1 << bit)) bbits = bit-bshift+1; if (gmask & (1 << bit)) gbits = bit-gshift+1; if (rmask & (1 << bit)) rbits = bit-rshift+1; } } else if ( bpp == 16 ) { rmask = 0x7C00; gmask = 0x03E0; bmask = 0x001F; rshift = 10; gshift = 5; bshift = 0; rbits = 5; gbits = 5; bbits = 5; } else if ( bpp == 32 ) { rmask = 0x00FF0000; gmask = 0x0000FF00; bmask = 0x000000FF; amask = 0xFF000000; ashift = 24; rshift = 16; gshift = 8; bshift = 0; rbits = 8; gbits = 8; bbits = 8; } } /* * Reading the image data */ if ( IsBmp ) { // NOTE: seeking a positive amount in wxFromCurrent mode allows us to // load even non-seekable streams (see wxInputStream::SeekI docs)! const wxFileOffset pos = stream.TellI(); if (pos != wxInvalidOffset && bmpOffset > pos) if (stream.SeekI(bmpOffset - pos, wxFromCurrent) == wxInvalidOffset) return false; //else: icon, just carry on } unsigned char *data = ptr; /* set the whole image to the background color */ if ( bpp < 16 && (comp == BI_RLE4 || comp == BI_RLE8) ) { for (int i = 0; i < width * height; i++) { *ptr++ = cmap[0].r; *ptr++ = cmap[0].g; *ptr++ = cmap[0].b; } ptr = data; } int linesize = ((width * bpp + 31) / 32) * 4; /* BMPs are stored upside down */ for ( int line = (height - 1); line >= 0; line-- ) { int linepos = 0; for ( int column = 0; column < width ; ) { if ( bpp < 16 ) { linepos++; aByte = stream.GetC(); if ( bpp == 1 ) { for (int bit = 0; bit < 8 && column < width; bit++) { int index = ((aByte & (0x80 >> bit)) ? 1 : 0); ptr[poffset] = cmap[index].r; ptr[poffset + 1] = cmap[index].g; ptr[poffset + 2] = cmap[index].b; column++; } } else if ( bpp == 4 ) { if ( comp == BI_RLE4 ) { wxUint8 first; first = aByte; aByte = stream.GetC(); if ( first == 0 ) { if ( aByte == 0 ) { if ( column > 0 ) column = width; } else if ( aByte == 1 ) { column = width; line = -1; } else if ( aByte == 2 ) { aByte = stream.GetC(); column += aByte; linepos = column * bpp / 4; aByte = stream.GetC(); line -= aByte; // upside down } else { int absolute = aByte; wxUint8 nibble[2] ; int readBytes = 0 ; for (int k = 0; k < absolute; k++) { if ( !(k % 2 ) ) { ++readBytes ; aByte = stream.GetC(); nibble[0] = (wxUint8)( (aByte & 0xF0) >> 4 ) ; nibble[1] = (wxUint8)( aByte & 0x0F ) ; } ptr[poffset ] = cmap[nibble[k%2]].r; ptr[poffset + 1] = cmap[nibble[k%2]].g; ptr[poffset + 2] = cmap[nibble[k%2]].b; column++; if ( k % 2 ) linepos++; } if ( readBytes & 0x01 ) aByte = stream.GetC(); } } else { wxUint8 nibble[2] ; nibble[0] = (wxUint8)( (aByte & 0xF0) >> 4 ) ; nibble[1] = (wxUint8)( aByte & 0x0F ) ; for ( int l = 0; l < first && column < width; l++ ) { ptr[poffset ] = cmap[nibble[l%2]].r; ptr[poffset + 1] = cmap[nibble[l%2]].g; ptr[poffset + 2] = cmap[nibble[l%2]].b; column++; if ( l % 2 ) linepos++; } } }