From c46b3fbfec70d1f8ec316916fb4495a0519f51aa Mon Sep 17 00:00:00 2001 From: Sam Hocevar Date: Tue, 5 Mar 2013 15:09:18 +0000 Subject: [PATCH] image: remove the Image::Format enum and use PixelFormat instead. --- src/Makefile.am | 4 ++-- src/core.h | 3 +-- src/gpu/texture.cpp | 27 ++++++++++++--------------- src/image/codec/android-image.cpp | 10 +++++----- src/image/codec/dummy-image.cpp | 4 ++-- src/image/codec/gdiplus-image.cpp | 10 +++++----- src/image/codec/ios-image.cpp | 4 ++-- src/image/codec/ps3-image.cpp | 4 ++-- src/image/codec/sdl-image.cpp | 7 ++++--- src/image/image-private.h | 6 ++---- src/image/image.cpp | 16 ++++++++-------- src/lol/gpu/texture.h | 1 + src/lol/image/all.h | 1 + src/{ => lol}/image/image.h | 24 +++++++----------------- src/lolcore.vcxproj | 2 +- src/tileset.cpp | 12 ++++++------ 16 files changed, 61 insertions(+), 74 deletions(-) rename src/{ => lol}/image/image.h (58%) diff --git a/src/Makefile.am b/src/Makefile.am index 64a2e8d4..667aa14b 100644 --- a/src/Makefile.am +++ b/src/Makefile.am @@ -45,7 +45,7 @@ liblolcore_headers = \ lol/sys/timer.h \ \ lol/image/all.h \ - lol/image/color.h \ + lol/image/color.h lol/image/image.h \ \ lol/gpu/all.h \ lol/gpu/shader.h lol/gpu/indexbuffer.h lol/gpu/vertexbuffer.h \ @@ -98,7 +98,7 @@ liblolcore_sources = \ sys/init.cpp sys/timer.cpp sys/file.cpp \ sys/threadbase.h \ \ - image/image.cpp image/image.h image/image-private.h \ + image/image.cpp image/image-private.h \ image/codec/gdiplus-image.cpp \ image/codec/ios-image.cpp \ image/codec/dummy-image.cpp \ diff --git a/src/core.h b/src/core.h index 15710824..0649a3e0 100644 --- a/src/core.h +++ b/src/core.h @@ -94,8 +94,8 @@ static inline int isnan(float f) #include #include #include -#include #include +#include #include #include "numeric.h" @@ -130,7 +130,6 @@ static inline int isnan(float f) #include "map.h" #include "layer.h" #include "mesh/mesh.h" -#include "image/image.h" #include "application/application.h" #include "easymesh/csgbsp.h" #include "easymesh/easymesh.h" diff --git a/src/gpu/texture.cpp b/src/gpu/texture.cpp index 0ff63415..4cf42f30 100644 --- a/src/gpu/texture.cpp +++ b/src/gpu/texture.cpp @@ -82,26 +82,19 @@ Texture::Texture(ivec2 size, PixelFormat format) /* Unknown */ { D3DFMT_UNKNOWN, 0 }, - /* R8G8B8 */ + /* FIXME: this is all mixed up for the RGBA/ARGB combinations */ # if defined USE_D3D9 - { D3DFMT_R8G8B8, 3 }, + { D3DFMT_R8G8B8, 3 }, /* RGB_8 */ + { D3DFMT_UNKNOWN, 0 }, /* RGBA_8 */ + { D3DFMT_A8R8G8B8, 4 }, /* ARGB_8 */ + { D3DFMT_UNKNOWN, 0 }, /* ABGR_8 */ + { D3DFMT_L8, 1 }, /* Y8 */ # else { D3DFMT_UNKNOWN, 0 }, -# endif - - /* A8R8G8B8 */ -# if defined USE_D3D9 - { D3DFMT_A8R8G8B8, 4 }, -# else + { D3DFMT_UNKNOWN, 0 }, /* By default the X360 will swizzle the texture. Ask for linear. */ { D3DFMT_LIN_A8R8G8B8, 4 }, -# endif - - /* Y8 */ -# if defined USE_D3D9 - { D3DFMT_L8, 1 }, -# else - /* By default the X360 will swizzle the texture. Ask for linear. */ + { D3DFMT_UNKNOWN, 0 }, { D3DFMT_LIN_L8, 1 }, # endif }; @@ -128,14 +121,17 @@ Texture::Texture(ivec2 size, PixelFormat format) { { 0, 0, 0, 0 }, /* Unknown */ + /* FIXME: this is all mixed up for the RGBA/ARGB combinations */ #if __CELLOS_LV2__ { GL_RGB8, GL_RGB, GL_UNSIGNED_BYTE, 3 }, { GL_ARGB_SCE, GL_RGBA, GL_UNSIGNED_INT_8_8_8_8, 4 }, + { GL_ARGB_SCE, GL_RGBA, GL_UNSIGNED_INT_8_8_8_8, 4 }, { GL_ARGB_SCE, GL_BGRA, GL_UNSIGNED_INT_8_8_8_8, 4 }, { GL_LUMINANCE8, GL_LUMINANCE, GL_UNSIGNED_BYTE, 1 }, #elif defined __native_client__ || defined HAVE_GLES_2X { GL_RGB, GL_RGB, GL_UNSIGNED_BYTE, 3 }, { GL_RGBA, GL_RGBA, GL_UNSIGNED_BYTE, 4 }, + { GL_RGBA, GL_RGBA, GL_UNSIGNED_BYTE, 4 }, /* FIXME: if GL_RGBA is not available, we should advertise * this format as "not available" on this platform. */ { GL_RGBA, GL_RGBA, GL_UNSIGNED_BYTE, 4 }, @@ -144,6 +140,7 @@ Texture::Texture(ivec2 size, PixelFormat format) { GL_RGB8, GL_RGB, GL_UNSIGNED_BYTE, 3 }, /* RGB_8 */ /* Seems efficient for little endian textures */ { GL_RGBA8, GL_RGBA, GL_UNSIGNED_INT_8_8_8_8_REV, 4 }, /* ARGB_8 */ + { GL_RGBA8, GL_RGBA, GL_UNSIGNED_INT_8_8_8_8_REV, 4 }, /* ARGB_8 */ { GL_RGBA8, GL_BGRA, GL_UNSIGNED_INT_8_8_8_8_REV, 4 }, /* ABGR_8 */ { GL_R8, GL_RED, GL_UNSIGNED_BYTE, 1 }, /* A8 */ #endif diff --git a/src/image/codec/android-image.cpp b/src/image/codec/android-image.cpp index caf9530b..d9c63d49 100644 --- a/src/image/codec/android-image.cpp +++ b/src/image/codec/android-image.cpp @@ -83,24 +83,24 @@ bool AndroidImageData::Open(char const *path) /* Get image dimensions */ mid = env->GetMethodID(cls, "getWidth", "(Landroid/graphics/Bitmap;)I"); - size.x = env->CallIntMethod(g_activity, mid, bmp); + m_size.x = env->CallIntMethod(g_activity, mid, bmp); mid = env->GetMethodID(cls, "getHeight", "(Landroid/graphics/Bitmap;)I"); - size.y = env->CallIntMethod(g_activity, mid, bmp); + m_size.y = env->CallIntMethod(g_activity, mid, bmp); /* Get pixels */ - array = env->NewIntArray(size.x * size.y); + array = env->NewIntArray(m_size.x * m_size.y); env->NewGlobalRef(array); mid = env->GetMethodID(cls, "getPixels", "(Landroid/graphics/Bitmap;[I)V"); env->CallVoidMethod(g_activity, mid, bmp, array); pixels = env->GetIntArrayElements(array, 0); - for (int n = 0; n < size.x * size.y; n++) + for (int n = 0; n < m_size.x * m_size.y; n++) { uint32_t u = pixels[n]; u = (u & 0xff00ff00) | ((u & 0xff0000) >> 16) | ((u & 0xff) << 16); pixels[n] = u; } - format = Image::FORMAT_RGBA; + m_format = PixelFormat::RGBA_8; return true; } diff --git a/src/image/codec/dummy-image.cpp b/src/image/codec/dummy-image.cpp index 764d1110..a18e9345 100644 --- a/src/image/codec/dummy-image.cpp +++ b/src/image/codec/dummy-image.cpp @@ -44,8 +44,8 @@ bool DummyImageData::Open(char const *path) { UNUSED(path); - size = ivec2(256); - format = Image::FORMAT_RGBA; + m_size = ivec2(256); + m_format = PixelFormat::RGBA_8; pixels = (uint8_t *)malloc(256 * 256 * 4 * sizeof(*pixels)); uint8_t *parser = pixels; for (int j = 0; j < 256; j++) diff --git a/src/image/codec/gdiplus-image.cpp b/src/image/codec/gdiplus-image.cpp index 4c37898f..dc639cdb 100644 --- a/src/image/codec/gdiplus-image.cpp +++ b/src/image/codec/gdiplus-image.cpp @@ -109,10 +109,10 @@ bool GdiPlusImageData::Open(char const *path) return false; } - size = ivec2(m_bitmap->GetWidth(), m_bitmap->GetHeight()); - format = Image::FORMAT_RGBA; + m_size = ivec2(m_bitmap->GetWidth(), m_bitmap->GetHeight()); + m_format = PixelFormat::RGBA_8; - Gdiplus::Rect rect(0, 0, size.x, size.y); + Gdiplus::Rect rect(0, 0, m_size.x, m_size.y); if(m_bitmap->LockBits(&rect, Gdiplus::ImageLockModeRead, PixelFormat32bppARGB, &m_bdata) != Gdiplus::Ok) { @@ -127,8 +127,8 @@ bool GdiPlusImageData::Open(char const *path) * know about ARGB, only RGBA. So we swap bytes. We could also fix * this in the shader. */ uint8_t *p = static_cast(m_bdata.Scan0); - for (int y = 0; y < size.y; y++) - for (int x = 0; x < size.x; x++) + for (int y = 0; y < m_size.y; y++) + for (int x = 0; x < m_size.x; x++) { uint8_t tmp = p[2]; p[2] = p[0]; diff --git a/src/image/codec/ios-image.cpp b/src/image/codec/ios-image.cpp index 3b48dda2..075fddab 100644 --- a/src/image/codec/ios-image.cpp +++ b/src/image/codec/ios-image.cpp @@ -64,8 +64,8 @@ bool IosImageData::Open(char const *path) int w = CGImageGetWidth(image.CGImage); int h = CGImageGetHeight(image.CGImage); - size = ivec2(w, h); - format = Image::FORMAT_RGBA; + m_size = ivec2(w, h); + m_format = PixelFormat::RGBA_8; CGColorSpaceRef cspace = CGColorSpaceCreateDeviceRGB(); pixels = (uint8_t *)malloc(w * h * 4); diff --git a/src/image/codec/ps3-image.cpp b/src/image/codec/ps3-image.cpp index e1cf7d3a..0cabc870 100644 --- a/src/image/codec/ps3-image.cpp +++ b/src/image/codec/ps3-image.cpp @@ -137,8 +137,8 @@ bool Ps3ImageData::Open(char const *path) } /* Decode image */ - size = ivec2(info.imageWidth, info.imageHeight); - format = Image::FORMAT_RGBA; + m_size = ivec2(info.imageWidth, info.imageHeight); + m_format = PixelFormat::RGBA_8; pixels = (uint8_t *)malloc(info.imageWidth * 4 * info.imageHeight); CellPngDecDataCtrlParam data_ctrl_param; data_ctrl_param.outputBytesPerLine = info.imageWidth * 4; diff --git a/src/image/codec/sdl-image.cpp b/src/image/codec/sdl-image.cpp index 2b383903..a401fc8f 100644 --- a/src/image/codec/sdl-image.cpp +++ b/src/image/codec/sdl-image.cpp @@ -73,17 +73,18 @@ bool SdlImageData::Open(char const *path) return false; } - size = ivec2(m_img->w, m_img->h); + m_size = ivec2(m_img->w, m_img->h); if (m_img->format->BytesPerPixel != 4) { - SDL_Surface *tmp = Create32BppSurface(size); + SDL_Surface *tmp = Create32BppSurface(m_size); SDL_BlitSurface(m_img, nullptr, tmp, nullptr); SDL_FreeSurface(m_img); m_img = tmp; } - format = m_img->format->Amask ? Image::FORMAT_RGBA : Image::FORMAT_RGB; + m_format = m_img->format->Amask ? PixelFormat::RGBA_8 + : PixelFormat::RGB_8; return true; } diff --git a/src/image/image-private.h b/src/image/image-private.h index 38bf07df..781e8b20 100644 --- a/src/image/image-private.h +++ b/src/image/image-private.h @@ -16,8 +16,6 @@ #if !defined __LOL_IMAGE_PRIVATE_H__ #define __LOL_IMAGE_PRIVATE_H__ -#include "image.h" - namespace lol { @@ -81,8 +79,8 @@ public: virtual void *GetData() const = 0; protected: - ivec2 size; - Image::format_t format; + ivec2 m_size; + PixelFormat m_format; }; #define REGISTER_IMAGE_LOADER(name) \ diff --git a/src/image/image.cpp b/src/image/image.cpp index ff68c2ed..16ad945c 100644 --- a/src/image/image.cpp +++ b/src/image/image.cpp @@ -1,7 +1,7 @@ // // Lol Engine // -// Copyright: (c) 2010-2011 Sam Hocevar +// Copyright: (c) 2010-2013 Sam Hocevar // This program is free software; you can redistribute it and/or // modify it under the terms of the Do What The Fuck You Want To // Public License, Version 2, as published by Sam Hocevar. See @@ -58,28 +58,28 @@ Image::Image(char const *path) static bool init = RegisterAllLoaders(); UNUSED(init); - data = ImageLoader::Load(path); + m_data = ImageLoader::Load(path); } ivec2 Image::GetSize() const { - return data->size; + return m_data->m_size; } -Image::format_t Image::GetFormat() const +PixelFormat Image::GetFormat() const { - return data->format; + return m_data->m_format; } void * Image::GetData() const { - return data->GetData(); + return m_data->GetData(); } Image::~Image() { - data->Close(); - delete data; + m_data->Close(); + delete m_data; } } /* namespace lol */ diff --git a/src/lol/gpu/texture.h b/src/lol/gpu/texture.h index 8dc9df53..6173ca6b 100644 --- a/src/lol/gpu/texture.h +++ b/src/lol/gpu/texture.h @@ -26,6 +26,7 @@ struct PixelFormat { Unknown = 0, RGB_8, + RGBA_8, ARGB_8, ABGR_8, Y_8, diff --git a/src/lol/image/all.h b/src/lol/image/all.h index d770b5fb..b0b08ec4 100644 --- a/src/lol/image/all.h +++ b/src/lol/image/all.h @@ -12,6 +12,7 @@ #define __LOL_IMAGE_ALL_H__ #include +#include #endif // __LOL_IMAGE_ALL_H__ diff --git a/src/image/image.h b/src/lol/image/image.h similarity index 58% rename from src/image/image.h rename to src/lol/image/image.h index 2243075c..a2a39d40 100644 --- a/src/image/image.h +++ b/src/lol/image/image.h @@ -1,7 +1,7 @@ // // Lol Engine // -// Copyright: (c) 2010-2011 Sam Hocevar +// Copyright: (c) 2010-2013 Sam Hocevar // This program is free software; you can redistribute it and/or // modify it under the terms of the Do What The Fuck You Want To // Public License, Version 2, as published by Sam Hocevar. See @@ -13,39 +13,29 @@ // --------------- // -#if !defined __LOL_IMAGE_H__ -#define __LOL_IMAGE_H__ +#if !defined __LOL_IMAGE_IMAGE_H__ +#define __LOL_IMAGE_IMAGE_H__ -#include "lol/math/vector.h" +#include namespace lol { -class ImageData; - class Image { public: Image(char const *path); ~Image(); - typedef enum - { - FORMAT_RGBA = 0, - FORMAT_RGB, - FORMAT_UNKNOWN, - } - format_t; - ivec2 GetSize() const; - format_t GetFormat() const; + PixelFormat GetFormat() const; void *GetData() const; private: - ImageData *data; + class ImageData *m_data; }; } /* namespace lol */ -#endif // __LOL_IMAGE_H__ +#endif // __LOL_IMAGE_IMAGE_H__ diff --git a/src/lolcore.vcxproj b/src/lolcore.vcxproj index 3493d859..bd1e4787 100644 --- a/src/lolcore.vcxproj +++ b/src/lolcore.vcxproj @@ -188,7 +188,6 @@ - @@ -215,6 +214,7 @@ + diff --git a/src/tileset.cpp b/src/tileset.cpp index dc7402b6..805812d3 100644 --- a/src/tileset.cpp +++ b/src/tileset.cpp @@ -115,17 +115,17 @@ void TileSet::TickDraw(float seconds) else if (data->img) { int planes; - PixelFormat format = PixelFormat::Unknown; + PixelFormat format = data->img->GetFormat(); - switch (data->img->GetFormat()) + switch (format) { - case Image::FORMAT_RGB: - format = PixelFormat::RGB_8; + case PixelFormat::RGB_8: planes = 3; break; - case Image::FORMAT_RGBA: + case PixelFormat::RGBA_8: + case PixelFormat::ARGB_8: + case PixelFormat::ABGR_8: default: - format = PixelFormat::ARGB_8; planes = 4; break; }