Browse Source

image: GetData returns uint8_t* instead of void*, it's a lot safer.

legacy
Sam Hocevar sam 11 years ago
parent
commit
f16704ecb0
11 changed files with 28 additions and 25 deletions
  1. +3
    -3
      src/image/codec/android-image.cpp
  2. +4
    -4
      src/image/codec/dummy-image.cpp
  3. +2
    -2
      src/image/codec/gdiplus-image.cpp
  4. +2
    -2
      src/image/codec/ios-image.cpp
  5. +2
    -2
      src/image/codec/ps3-image.cpp
  6. +3
    -3
      src/image/codec/sdl-image.cpp
  7. +1
    -1
      src/image/image-private.h
  8. +6
    -3
      src/image/image.cpp
  9. +1
    -1
      src/lol/image/image.h
  10. +3
    -3
      src/tileset.cpp
  11. +1
    -1
      test/unit/image.cpp

+ 3
- 3
src/image/codec/android-image.cpp View File

@@ -38,7 +38,7 @@ public:
virtual bool Open(char const *); virtual bool Open(char const *);
virtual bool Close(); virtual bool Close();


virtual void *GetData() const;
virtual uint8_t *GetData() const;


private: private:
jobject bmp; jobject bmp;
@@ -130,9 +130,9 @@ bool AndroidImageData::Close()
return true; return true;
} }


void * AndroidImageData::GetData() const
uint8_t *AndroidImageData::GetData() const
{ {
return pixels;
return (uint8_t *)pixels;
} }


} /* namespace lol */ } /* namespace lol */


+ 4
- 4
src/image/codec/dummy-image.cpp View File

@@ -30,7 +30,7 @@ public:
virtual bool Open(char const *); virtual bool Open(char const *);
virtual bool Close(); virtual bool Close();


virtual void *GetData() const;
virtual uint8_t *GetData() const;


private: private:
uint8_t *pixels; uint8_t *pixels;
@@ -46,7 +46,7 @@ bool DummyImageData::Open(char const *path)


m_size = ivec2(256); m_size = ivec2(256);
m_format = PixelFormat::RGBA_8; 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; uint8_t *parser = pixels;
for (int j = 0; j < 256; j++) for (int j = 0; j < 256; j++)
for (int i = 0; i < 256; i++) for (int i = 0; i < 256; i++)
@@ -62,12 +62,12 @@ bool DummyImageData::Open(char const *path)


bool DummyImageData::Close() bool DummyImageData::Close()
{ {
free(pixels);
delete[] pixels;


return true; return true;
} }


void * DummyImageData::GetData() const
uint8_t * DummyImageData::GetData() const
{ {
return pixels; return pixels;
} }


+ 2
- 2
src/image/codec/gdiplus-image.cpp View File

@@ -37,7 +37,7 @@ public:
virtual bool Open(char const *); virtual bool Open(char const *);
virtual bool Close(); virtual bool Close();


virtual void *GetData() const;
virtual uint8_t *GetData() const;


private: private:
Gdiplus::Bitmap *m_bitmap; Gdiplus::Bitmap *m_bitmap;
@@ -147,7 +147,7 @@ bool GdiPlusImageData::Close()
return true; return true;
} }


void * GdiPlusImageData::GetData() const
uint8_t * GdiPlusImageData::GetData() const
{ {
return m_bdata.Scan0; return m_bdata.Scan0;
} }


+ 2
- 2
src/image/codec/ios-image.cpp View File

@@ -34,7 +34,7 @@ public:
virtual bool Open(char const *); virtual bool Open(char const *);
virtual bool Close(); virtual bool Close();


virtual void *GetData() const;
virtual uint8_t *GetData() const;


private: private:
uint8_t *pixels; uint8_t *pixels;
@@ -90,7 +90,7 @@ bool IosImageData::Close()
return true; return true;
} }


void * IosImageData::GetData() const
uint8_t * IosImageData::GetData() const
{ {
return pixels; return pixels;
} }


+ 2
- 2
src/image/codec/ps3-image.cpp View File

@@ -35,7 +35,7 @@ public:
virtual bool Open(char const *); virtual bool Open(char const *);
virtual bool Close(); virtual bool Close();


virtual void *GetData() const;
virtual uint8_t *GetData() const;


private: private:
static void* Malloc(uint32_t size, void* data) { return malloc(size); }; static void* Malloc(uint32_t size, void* data) { return malloc(size); };
@@ -179,7 +179,7 @@ bool Ps3ImageData::Close()
return true; return true;
} }


void * Ps3ImageData::GetData() const
uint8_t * Ps3ImageData::GetData() const
{ {
return pixels; return pixels;
} }


+ 3
- 3
src/image/codec/sdl-image.cpp View File

@@ -43,7 +43,7 @@ public:
virtual bool Open(char const *); virtual bool Open(char const *);
virtual bool Close(); virtual bool Close();


virtual void *GetData() const;
virtual uint8_t *GetData() const;


static SDL_Surface *Create32BppSurface(ivec2 size); static SDL_Surface *Create32BppSurface(ivec2 size);


@@ -96,9 +96,9 @@ bool SdlImageData::Close()
return true; 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) SDL_Surface *SdlImageData::Create32BppSurface(ivec2 size)


+ 1
- 1
src/image/image-private.h View File

@@ -76,7 +76,7 @@ public:
virtual bool Open(char const *) = 0; virtual bool Open(char const *) = 0;
virtual bool Close() = 0; virtual bool Close() = 0;


virtual void *GetData() const = 0;
virtual uint8_t *GetData() const = 0;


protected: protected:
ivec2 m_size; ivec2 m_size;


+ 6
- 3
src/image/image.cpp View File

@@ -22,13 +22,16 @@ namespace lol


static bool RegisterAllLoaders() 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 * REGISTER_IMAGE_LOADER macro forward-declares free functions from
* the "lol" namespace. An apparent bug in Visual Studio's compiler * the "lol" namespace. An apparent bug in Visual Studio's compiler
* makes it think these functions are actually in the top-level * makes it think these functions are actually in the top-level
* namespace when the forward declaration is in a class member function. * namespace when the forward declaration is in a class member function.
* To avoid the problem, we make the forward declaration in a free * 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__ #if defined __ANDROID__
REGISTER_IMAGE_LOADER(AndroidImageData) REGISTER_IMAGE_LOADER(AndroidImageData)
#endif #endif
@@ -71,7 +74,7 @@ PixelFormat Image::GetFormat() const
return m_data->m_format; return m_data->m_format;
} }


void * Image::GetData() const
uint8_t * Image::GetData() const
{ {
return m_data->GetData(); return m_data->GetData();
} }


+ 1
- 1
src/lol/image/image.h View File

@@ -29,7 +29,7 @@ public:


ivec2 GetSize() const; ivec2 GetSize() const;
PixelFormat GetFormat() const; PixelFormat GetFormat() const;
void *GetData() const;
uint8_t *GetData() const;


private: private:
class ImageData *m_data; class ImageData *m_data;


+ 3
- 3
src/tileset.cpp View File

@@ -133,10 +133,10 @@ void TileSet::TickDraw(float seconds)
int w = PotUp(data->isize.x); int w = PotUp(data->isize.x);
int h = PotUp(data->isize.y); 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) 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++) for (int line = 0; line < data->isize.y; line++)
memcpy(tmp + planes * w * line, memcpy(tmp + planes * w * line,
pixels + planes * data->isize.x * line, pixels + planes * data->isize.x * line,
@@ -148,7 +148,7 @@ void TileSet::TickDraw(float seconds)
data->m_texture->SetData(pixels); data->m_texture->SetData(pixels);


if (pixels != data->img->GetData()) if (pixels != data->img->GetData())
free(pixels);
delete[] pixels;
delete data->img; delete data->img;
data->img = nullptr; data->img = nullptr;
} }


+ 1
- 1
test/unit/image.cpp View File

@@ -30,7 +30,7 @@ LOLUNIT_FIXTURE(ImageTest)
LOLUNIT_ASSERT_EQUAL(size.x, 256); LOLUNIT_ASSERT_EQUAL(size.x, 256);
LOLUNIT_ASSERT_EQUAL(size.y, 16); LOLUNIT_ASSERT_EQUAL(size.y, 16);


uint8_t *data = (uint8_t *)image.GetData();
uint8_t *data = image.GetData();
LOLUNIT_ASSERT(data); LOLUNIT_ASSERT(data);


LOLUNIT_ASSERT_EQUAL((int)data[0], 0x00); LOLUNIT_ASSERT_EQUAL((int)data[0], 0x00);


Loading…
Cancel
Save