void loadTGA(Common::SeekableReadStream *data, Texture *t) { Graphics::TGADecoder *tgaDecoder = new Graphics::TGADecoder(); tgaDecoder->loadStream(*data); const Graphics::Surface *tgaSurface = tgaDecoder->getSurface(); t->_width = tgaSurface->w; t->_height = tgaSurface->h; t->_texture = NULL; int bpp = tgaSurface->format.bytesPerPixel; if (bpp == 4) { t->_colorFormat = BM_BGRA; t->_bpp = 4; t->_hasAlpha = true; } else { t->_colorFormat = BM_BGR888; t->_bpp = 3; t->_hasAlpha = false; } assert(bpp == 3 || bpp == 4); // Assure we have 24/32 bpp // Allocate room for the texture. t->_data = new char[t->_width * t->_height * (bpp)]; // Copy the texture data, as the decoder owns the current copy. memcpy(t->_data, tgaSurface->getPixels(), t->_width * t->_height * (bpp)); delete tgaDecoder; }
bool BitmapData::loadTGA(Common::SeekableReadStream *data) { Graphics::TGADecoder dec; bool success = dec.loadStream(*data); if (!success) return false; const Graphics::Surface *origSurf = dec.getSurface(); Graphics::PixelFormat pixelFormat = Graphics::PixelFormat(4, 8, 8, 8, 8, 0, 8, 16, 24); const Graphics::Surface *surf = origSurf->convertTo(pixelFormat); _width = surf->w; _height = surf->h; _format = 1; _x = _y = 0; _bpp = 4; _colorFormat = BM_RGBA; _numImages = 1; _data = new Graphics::PixelBuffer[1]; _data[0].set(pixelFormat, (unsigned char *)surf->pixels); g_driver->createBitmap(this); freeData(); delete surf; return true; }
static GLES8888Texture *loadBuiltinTexture(const char *filename) { Common::ArchiveMemberPtr member = SearchMan.getMember(filename); Common::SeekableReadStream *str = member->createReadStream(); Graphics::TGADecoder dec; dec.loadStream(*str); const void *pixels = dec.getSurface()->getPixels(); GLES8888Texture *ret = new GLES8888Texture(); uint16 w = dec.getSurface()->w; uint16 h = dec.getSurface()->h; uint16 pitch = dec.getSurface()->pitch; ret->allocBuffer(w, h); ret->updateBuffer(0, 0, w, h, pixels, pitch); delete str; return ret; }
void RenderManager::readImageToSurface(const Common::String &fileName, Graphics::Surface &destination) { Common::File file; if (!file.open(fileName)) { warning("Could not open file %s", fileName.c_str()); return; } // Read the magic number // Some files are true TGA, while others are TGZ uint32 fileType = file.readUint32BE(); uint32 imageWidth; uint32 imageHeight; Graphics::TGADecoder tga; uint16 *buffer; bool isTransposed = _renderTable.getRenderState() == RenderTable::PANORAMA; // All ZVision images are in RGB 555 Graphics::PixelFormat pixelFormat555 = Graphics::PixelFormat(2, 5, 5, 5, 0, 10, 5, 0, 0); destination.format = pixelFormat555; bool isTGZ; // Check for TGZ files if (fileType == MKTAG('T', 'G', 'Z', '\0')) { isTGZ = true; // TGZ files have a header and then Bitmap data that is compressed with LZSS uint32 decompressedSize = file.readSint32LE(); imageWidth = file.readSint32LE(); imageHeight = file.readSint32LE(); LzssReadStream lzssStream(&file); buffer = (uint16 *)(new uint16[decompressedSize]); lzssStream.read(buffer, decompressedSize); } else { isTGZ = false; // Reset the cursor file.seek(0); // Decode if (!tga.loadStream(file)) { warning("Error while reading TGA image"); return; } Graphics::Surface tgaSurface = *(tga.getSurface()); imageWidth = tgaSurface.w; imageHeight = tgaSurface.h; buffer = (uint16 *)tgaSurface.getPixels(); } // Flip the width and height if transposed if (isTransposed) { uint16 temp = imageHeight; imageHeight = imageWidth; imageWidth = temp; } // If the destination internal buffer is the same size as what we're copying into it, // there is no need to free() and re-create if (imageWidth != destination.w || imageHeight != destination.h) { destination.create(imageWidth, imageHeight, pixelFormat555); } // If transposed, 'un-transpose' the data while copying it to the destination // Otherwise, just do a simple copy if (isTransposed) { uint16 *dest = (uint16 *)destination.getPixels(); for (uint32 y = 0; y < imageHeight; ++y) { uint32 columnIndex = y * imageWidth; for (uint32 x = 0; x < imageWidth; ++x) { dest[columnIndex + x] = buffer[x * imageHeight + y]; } } } else { memcpy(destination.getPixels(), buffer, imageWidth * imageHeight * _pixelFormat.bytesPerPixel); } // Cleanup if (isTGZ) { delete[] buffer; } else { tga.destroy(); } // Convert in place to RGB 565 from RGB 555 destination.convertToInPlace(_pixelFormat); }