aboutsummaryrefslogtreecommitdiff
path: root/graphics/surface.cpp
diff options
context:
space:
mode:
authorMatthew Hoops2011-06-30 08:12:39 -0400
committerJohannes Schickel2012-03-20 01:06:47 +0100
commita004897252fba00d48a6acb0dc5298897faefdfb (patch)
tree2e4072cac6463af6d96c16d94233ff9db597899c /graphics/surface.cpp
parent9997493e36ff4a391b485d3794f04e5e1c3cf53f (diff)
downloadscummvm-rg350-a004897252fba00d48a6acb0dc5298897faefdfb.tar.gz
scummvm-rg350-a004897252fba00d48a6acb0dc5298897faefdfb.tar.bz2
scummvm-rg350-a004897252fba00d48a6acb0dc5298897faefdfb.zip
GRAPHICS: Add a convertTo() function to Surface
Diffstat (limited to 'graphics/surface.cpp')
-rw-r--r--graphics/surface.cpp73
1 files changed, 73 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