From f16704ecb014d03a38745622b97997691538bb6e Mon Sep 17 00:00:00 2001 From: Sam Hocevar Date: Tue, 5 Mar 2013 15:48:11 +0000 Subject: [PATCH] image: GetData returns uint8_t* instead of void*, it's a lot safer. --- src/image/codec/android-image.cpp | 6 +++--- src/image/codec/dummy-image.cpp | 8 ++++---- src/image/codec/gdiplus-image.cpp | 4 ++-- src/image/codec/ios-image.cpp | 4 ++-- src/image/codec/ps3-image.cpp | 4 ++-- src/image/codec/sdl-image.cpp | 6 +++--- src/image/image-private.h | 2 +- src/image/image.cpp | 9 ++++++--- src/lol/image/image.h | 2 +- src/tileset.cpp | 6 +++--- test/unit/image.cpp | 2 +- 11 files changed, 28 insertions(+), 25 deletions(-) diff --git a/src/image/codec/android-image.cpp b/src/image/codec/android-image.cpp index d9c63d49..5240b7f2 100644 --- a/src/image/codec/android-image.cpp +++ b/src/image/codec/android-image.cpp @@ -38,7 +38,7 @@ public: virtual bool Open(char const *); virtual bool Close(); - virtual void *GetData() const; + virtual uint8_t *GetData() const; private: jobject bmp; @@ -130,9 +130,9 @@ bool AndroidImageData::Close() return true; } -void * AndroidImageData::GetData() const +uint8_t *AndroidImageData::GetData() const { - return pixels; + return (uint8_t *)pixels; } } /* namespace lol */ diff --git a/src/image/codec/dummy-image.cpp b/src/image/codec/dummy-image.cpp index a18e9345..3ea1b9f3 100644 --- a/src/image/codec/dummy-image.cpp +++ b/src/image/codec/dummy-image.cpp @@ -30,7 +30,7 @@ public: virtual bool Open(char const *); virtual bool Close(); - virtual void *GetData() const; + virtual uint8_t *GetData() const; private: uint8_t *pixels; @@ -46,7 +46,7 @@ bool DummyImageData::Open(char const *path) m_size = ivec2(256); m_format = PixelFormat::RGBA_8; - pixels = (uint8_t *)malloc(256 * 256 * 4 * sizeof(*pixels)); + pixels = new uint8_t[256 * 256 * 4 * sizeof(*pixels)]; uint8_t *parser = pixels; for (int j = 0; j < 256; j++) for (int i = 0; i < 256; i++) @@ -62,12 +62,12 @@ bool DummyImageData::Open(char const *path) bool DummyImageData::Close() { - free(pixels); + delete[] pixels; return true; } -void * DummyImageData::GetData() const +uint8_t * DummyImageData::GetData() const { return pixels; } diff --git a/src/image/codec/gdiplus-image.cpp b/src/image/codec/gdiplus-image.cpp index dc639cdb..c662ce7a 100644 --- a/src/image/codec/gdiplus-image.cpp +++ b/src/image/codec/gdiplus-image.cpp @@ -37,7 +37,7 @@ public: virtual bool Open(char const *); virtual bool Close(); - virtual void *GetData() const; + virtual uint8_t *GetData() const; private: Gdiplus::Bitmap *m_bitmap; @@ -147,7 +147,7 @@ bool GdiPlusImageData::Close() return true; } -void * GdiPlusImageData::GetData() const +uint8_t * GdiPlusImageData::GetData() const { return m_bdata.Scan0; } diff --git a/src/image/codec/ios-image.cpp b/src/image/codec/ios-image.cpp index 075fddab..18a517e1 100644 --- a/src/image/codec/ios-image.cpp +++ b/src/image/codec/ios-image.cpp @@ -34,7 +34,7 @@ public: virtual bool Open(char const *); virtual bool Close(); - virtual void *GetData() const; + virtual uint8_t *GetData() const; private: uint8_t *pixels; @@ -90,7 +90,7 @@ bool IosImageData::Close() return true; } -void * IosImageData::GetData() const +uint8_t * IosImageData::GetData() const { return pixels; } diff --git a/src/image/codec/ps3-image.cpp b/src/image/codec/ps3-image.cpp index 0cabc870..f129fe84 100644 --- a/src/image/codec/ps3-image.cpp +++ b/src/image/codec/ps3-image.cpp @@ -35,7 +35,7 @@ public: virtual bool Open(char const *); virtual bool Close(); - virtual void *GetData() const; + virtual uint8_t *GetData() const; private: static void* Malloc(uint32_t size, void* data) { return malloc(size); }; @@ -179,7 +179,7 @@ bool Ps3ImageData::Close() return true; } -void * Ps3ImageData::GetData() const +uint8_t * Ps3ImageData::GetData() const { return pixels; } diff --git a/src/image/codec/sdl-image.cpp b/src/image/codec/sdl-image.cpp index a401fc8f..335fe956 100644 --- a/src/image/codec/sdl-image.cpp +++ b/src/image/codec/sdl-image.cpp @@ -43,7 +43,7 @@ public: virtual bool Open(char const *); virtual bool Close(); - virtual void *GetData() const; + virtual uint8_t *GetData() const; static SDL_Surface *Create32BppSurface(ivec2 size); @@ -96,9 +96,9 @@ bool SdlImageData::Close() return true; } -void * SdlImageData::GetData() const +uint8_t * SdlImageData::GetData() const { - return m_img->pixels; + return (uint8_t *)m_img->pixels; } SDL_Surface *SdlImageData::Create32BppSurface(ivec2 size) diff --git a/src/image/image-private.h b/src/image/image-private.h index 781e8b20..77a7b60d 100644 --- a/src/image/image-private.h +++ b/src/image/image-private.h @@ -76,7 +76,7 @@ public: virtual bool Open(char const *) = 0; virtual bool Close() = 0; - virtual void *GetData() const = 0; + virtual uint8_t *GetData() const = 0; protected: ivec2 m_size; diff --git a/src/image/image.cpp b/src/image/image.cpp index 16ad945c..68430817 100644 --- a/src/image/image.cpp +++ b/src/image/image.cpp @@ -22,13 +22,16 @@ namespace lol static bool RegisterAllLoaders() { - /* We cannot make this an ImageLoader member function, because the + /* HACK: We cannot make this an ImageLoader member function, because the * REGISTER_IMAGE_LOADER macro forward-declares free functions from * the "lol" namespace. An apparent bug in Visual Studio's compiler * makes it think these functions are actually in the top-level * namespace when the forward declaration is in a class member function. * To avoid the problem, we make the forward declaration in a free - * function. */ + * function. + * The bug was reported to Microsoft and fixed by them, but the fix + * is not yet available. + * https://connect.microsoft.com/VisualStudio/feedback/details/730878/ */ #if defined __ANDROID__ REGISTER_IMAGE_LOADER(AndroidImageData) #endif @@ -71,7 +74,7 @@ PixelFormat Image::GetFormat() const return m_data->m_format; } -void * Image::GetData() const +uint8_t * Image::GetData() const { return m_data->GetData(); } diff --git a/src/lol/image/image.h b/src/lol/image/image.h index a2a39d40..756823d9 100644 --- a/src/lol/image/image.h +++ b/src/lol/image/image.h @@ -29,7 +29,7 @@ public: ivec2 GetSize() const; PixelFormat GetFormat() const; - void *GetData() const; + uint8_t *GetData() const; private: class ImageData *m_data; diff --git a/src/tileset.cpp b/src/tileset.cpp index 805812d3..b9072d7d 100644 --- a/src/tileset.cpp +++ b/src/tileset.cpp @@ -133,10 +133,10 @@ void TileSet::TickDraw(float seconds) int w = PotUp(data->isize.x); int h = PotUp(data->isize.y); - uint8_t *pixels = (uint8_t *)data->img->GetData(); + uint8_t *pixels = data->img->GetData(); if (w != data->isize.x || h != data->isize.y) { - uint8_t *tmp = (uint8_t *)malloc(planes * w * h); + uint8_t *tmp = new uint8_t[planes * w * h]; for (int line = 0; line < data->isize.y; line++) memcpy(tmp + planes * w * line, pixels + planes * data->isize.x * line, @@ -148,7 +148,7 @@ void TileSet::TickDraw(float seconds) data->m_texture->SetData(pixels); if (pixels != data->img->GetData()) - free(pixels); + delete[] pixels; delete data->img; data->img = nullptr; } diff --git a/test/unit/image.cpp b/test/unit/image.cpp index 08971080..920f0463 100644 --- a/test/unit/image.cpp +++ b/test/unit/image.cpp @@ -30,7 +30,7 @@ LOLUNIT_FIXTURE(ImageTest) LOLUNIT_ASSERT_EQUAL(size.x, 256); LOLUNIT_ASSERT_EQUAL(size.y, 16); - uint8_t *data = (uint8_t *)image.GetData(); + uint8_t *data = image.GetData(); LOLUNIT_ASSERT(data); LOLUNIT_ASSERT_EQUAL((int)data[0], 0x00);