diff options
author | Matthew Hoops | 2011-06-30 08:12:39 -0400 |
---|---|---|
committer | Johannes Schickel | 2012-03-20 01:06:47 +0100 |
commit | a004897252fba00d48a6acb0dc5298897faefdfb (patch) | |
tree | 2e4072cac6463af6d96c16d94233ff9db597899c /graphics | |
parent | 9997493e36ff4a391b485d3794f04e5e1c3cf53f (diff) | |
download | scummvm-rg350-a004897252fba00d48a6acb0dc5298897faefdfb.tar.gz scummvm-rg350-a004897252fba00d48a6acb0dc5298897faefdfb.tar.bz2 scummvm-rg350-a004897252fba00d48a6acb0dc5298897faefdfb.zip |
GRAPHICS: Add a convertTo() function to Surface
Diffstat (limited to 'graphics')
-rw-r--r-- | graphics/surface.cpp | 73 | ||||
-rw-r--r-- | graphics/surface.h | 11 |
2 files changed, 84 insertions, 0 deletions
diff --git a/graphics/surface.cpp b/graphics/surface.cpp index 79a7821feb..0a5b109eb0 100644 --- a/graphics/surface.cpp +++ b/graphics/surface.cpp @@ -270,4 +270,77 @@ void Surface::move(int dx, int dy, int height) { } } +Graphics::Surface *Surface::convertTo(const PixelFormat &dstFormat, const byte *palette) const { + assert(pixels); + + Graphics::Surface *surface = new Graphics::Surface(); + + // If the target format is the same, just copy + if (format == dstFormat) { + surface->copyFrom(*this); + return surface; + } + + if (dstFormat.bytesPerPixel != 2 && dstFormat.bytesPerPixel != 4) + error("Surface::convertTo(): Only 2Bpp and 4Bpp supported"); + + surface->create(w, h, dstFormat); + + if (format.bytesPerPixel == 1) { + // Converting from paletted to high color + assert(palette); + + for (int y = 0; y < h; y++) { + const byte *srcRow = (byte *)getBasePtr(0, y); + byte *dstRow = (byte *)surface->getBasePtr(0, y); + + for (int x = 0; x < w; x++) { + byte index = *srcRow++; + byte r = palette[index * 3]; + byte g = palette[index * 3 + 1]; + byte b = palette[index * 3 + 2]; + + uint32 color = dstFormat.RGBToColor(r, g, b); + + if (dstFormat.bytesPerPixel == 2) + *((uint16 *)dstRow) = color; + else + *((uint32 *)dstRow) = color; + + dstRow += dstFormat.bytesPerPixel; + } + } + } else { + // Converting from high color to high color + for (int y = 0; y < h; y++) { + const byte *srcRow = (byte *)getBasePtr(0, y); + byte *dstRow = (byte *)surface->getBasePtr(0, y); + + for (int x = 0; x < w; x++) { + uint32 srcColor; + if (format.bytesPerPixel == 2) + srcColor = *((uint16 *)srcRow); + else + srcColor = *((uint32 *)srcRow); + + srcRow += format.bytesPerPixel; + + // Convert that color to the new format + byte r, g, b, a; + format.colorToARGB(srcColor, a, r, g, b); + uint32 color = dstFormat.ARGBToColor(a, r, g, b); + + if (dstFormat.bytesPerPixel == 2) + *((uint16 *)dstRow) = color; + else + *((uint32 *)dstRow) = color; + + dstRow += dstFormat.bytesPerPixel; + } + } + } + + return surface; +} + } // End of namespace Graphics diff --git a/graphics/surface.h b/graphics/surface.h index 018a283aad..eb8d1ac42e 100644 --- a/graphics/surface.h +++ b/graphics/surface.h @@ -135,6 +135,17 @@ struct Surface { void copyFrom(const Surface &surf); /** + * Convert the data to another pixel format. + * + * The calling code must call free on the returned surface and then delete + * it. + * + * @param dstFormat The desired format + * @param palette The palette (in RGB888), if the source format has a Bpp of 1 + */ + Graphics::Surface *convertTo(const PixelFormat &dstFormat, const byte *palette = 0) const; + + /** * Draw a line. * * @param x0 The x coordinate of the start point. |