Beatmup
Beatmup::GDIBitmap::Impl Class Reference

A simple wrapper of IL image. More...

Public Member Functions

 Impl (Context &ctx, const wchar_t *filename)
 
 Impl (Context &ctx, int width, int height, PixelFormat format)
 
const PixelFormat getPixelFormat () const
 
const int getWidth () const
 
const int getHeight () const
 
int getStride () const
 
void lockPixelData ()
 
void unlockPixelData ()
 
const pixbytegetData (int x, int y) const
 
void save (const WCHAR *filename)
 

Static Public Member Functions

static void init ()
 
static int GetEncoderClsid (const WCHAR *format, CLSID *pClsid)
 

Public Attributes

Gdiplus::Bitmap * bitmap
 
Gdiplus::Rect size
 
Gdiplus::BitmapData data
 

Detailed Description

A simple wrapper of IL image.

Definition at line 32 of file gdi_bitmap.cpp.

Constructor & Destructor Documentation

◆ Impl() [1/2]

Beatmup::GDIBitmap::Impl::Impl ( Context ctx,
const wchar_t *  filename 
)
inline

Definition at line 81 of file gdi_bitmap.cpp.

81  {
82  init();
83  bitmap = new Gdiplus::Bitmap(filename, 1);
84  size.X = size.Y = 0;
85  size.Width = bitmap->GetWidth();
86  size.Height = bitmap->GetHeight();
87  }
Gdiplus::Bitmap * bitmap
Definition: gdi_bitmap.cpp:34
JNIEnv jlong jstring filename

◆ Impl() [2/2]

Beatmup::GDIBitmap::Impl::Impl ( Context ctx,
int  width,
int  height,
PixelFormat  format 
)
inline

Definition at line 90 of file gdi_bitmap.cpp.

90  {
91  init();
93  switch (format) {
94  case TripleByte:
95  pf = PixelFormat24bppRGB;
96  break;
97 
98  case QuadByte:
99  pf = PixelFormat32bppARGB;
100  break;
101 
102  default:
103  throw RuntimeError("Unsupported pixel format");
104  }
105  bitmap = new Gdiplus::Bitmap(width, height, pf);
106  size.X = size.Y = 0;
107  size.Width = bitmap->GetWidth();
108  size.Height = bitmap->GetHeight();
109  }
@ QuadByte
4 channels of 8 bits per pixel (like RGBA), unsigned integer values
@ TripleByte
3 channels of 8 bits per pixel (like RGB), unsigned integer values
JNIEnv jobject jint format
jlong jint width
jlong jint jint height

Member Function Documentation

◆ init()

static void Beatmup::GDIBitmap::Impl::init ( )
inlinestatic

Definition at line 39 of file gdi_bitmap.cpp.

39  {
40  static bool gdiStartedUp = false;
41  if (!gdiStartedUp) {
42  // Start Gdiplus
43  Gdiplus::GdiplusStartupInput gdiplusStartupInput;
44  ULONG_PTR gdiplusToken;
45  Gdiplus::GdiplusStartup(&gdiplusToken, &gdiplusStartupInput, NULL);
46  }
47  }

◆ GetEncoderClsid()

static int Beatmup::GDIBitmap::Impl::GetEncoderClsid ( const WCHAR *  format,
CLSID *  pClsid 
)
inlinestatic

Definition at line 50 of file gdi_bitmap.cpp.

50  {
51  UINT num = 0; // number of image encoders
52  UINT size = 0; // size of the image encoder array in bytes
53 
54  Gdiplus::ImageCodecInfo* pImageCodecInfo = NULL;
55 
56  Gdiplus::GetImageEncodersSize(&num, &size);
57  if (size == 0)
58  return -1; // Failure
59 
60  pImageCodecInfo = (Gdiplus::ImageCodecInfo*)(malloc(size));
61  if (pImageCodecInfo == NULL)
62  return -1; // Failure
63 
64  Gdiplus::GetImageEncoders(num, size, pImageCodecInfo);
65 
66  for (UINT j = 0; j < num; ++j)
67  {
68  if (wcscmp(pImageCodecInfo[j].MimeType, format) == 0)
69  {
70  *pClsid = pImageCodecInfo[j].Clsid;
71  free(pImageCodecInfo);
72  return j; // Success
73  }
74  }
75 
76  free(pImageCodecInfo);
77  return -1; // Failure
78  }

◆ getPixelFormat()

const PixelFormat Beatmup::GDIBitmap::Impl::getPixelFormat ( ) const
inline

Definition at line 112 of file gdi_bitmap.cpp.

112  {
113  switch (bitmap->GetPixelFormat()) {
114  case PixelFormat8bppIndexed: return SingleByte;
115  case PixelFormat24bppRGB: return TripleByte;
116  case PixelFormat32bppARGB: return QuadByte;
117  }
118  throw RuntimeError("Unsupported pixel format");
119  }
@ SingleByte
single channel of 8 bits per pixel (like grayscale), unsigned integer values

◆ getWidth()

const int Beatmup::GDIBitmap::Impl::getWidth ( ) const
inline

Definition at line 122 of file gdi_bitmap.cpp.

122  {
123  return size.Width;
124  }

◆ getHeight()

const int Beatmup::GDIBitmap::Impl::getHeight ( ) const
inline

Definition at line 127 of file gdi_bitmap.cpp.

127  {
128  return size.Height;
129  }

◆ getStride()

int Beatmup::GDIBitmap::Impl::getStride ( ) const
inline

Definition at line 132 of file gdi_bitmap.cpp.

132  {
133  return ceili(getBitsPerPixel() * getWidth(), 32) / 8;
134  }
const int getWidth() const
Definition: gdi_bitmap.cpp:122
#define ceili(x, y)
integer division x/y with ceiling
Definition: utils.hpp:21

◆ lockPixelData()

void Beatmup::GDIBitmap::Impl::lockPixelData ( )
inline

Definition at line 137 of file gdi_bitmap.cpp.

137  {
138  bitmap->LockBits(&size, Gdiplus::ImageLockModeRead | Gdiplus::ImageLockModeWrite, bitmap->GetPixelFormat(), &data);
139  }
Gdiplus::BitmapData data
Definition: gdi_bitmap.cpp:36

◆ unlockPixelData()

void Beatmup::GDIBitmap::Impl::unlockPixelData ( )
inline

Definition at line 142 of file gdi_bitmap.cpp.

142  {
143  bitmap->UnlockBits(&data);
144  }

◆ getData()

const pixbyte* Beatmup::GDIBitmap::Impl::getData ( int  x,
int  y 
) const
inline

Definition at line 147 of file gdi_bitmap.cpp.

147  {
148  return (const pixbyte*)data.Scan0 + x + y * data.Stride;
149  }
uint8_t pixbyte
Definition: basic_types.h:34
jobject jlong jint jint y
jobject jlong jint x

◆ save()

void Beatmup::GDIBitmap::Impl::save ( const WCHAR *  filename)
inline

Definition at line 152 of file gdi_bitmap.cpp.

152  {
153  CLSID clsid;
154  if (wcsstr(filename, L".jpg") || wcsstr(filename, L".jpeg")) {
155  if (GetEncoderClsid(L"image/jpeg", &clsid) < 0)
156  throw Beatmup::RuntimeError("Unable to get encoder class id");
157  }
158  else if (wcsstr(filename, L".png")) {
159  if (GetEncoderClsid(L"image/png", &clsid) < 0)
160  throw Beatmup::RuntimeError("Unable to get encoder class id");
161  }
162  bitmap->Save(filename, &clsid);
163  }
static int GetEncoderClsid(const WCHAR *format, CLSID *pClsid)
Definition: gdi_bitmap.cpp:50

Member Data Documentation

◆ bitmap

Gdiplus::Bitmap* Beatmup::GDIBitmap::Impl::bitmap

Definition at line 34 of file gdi_bitmap.cpp.

◆ size

Gdiplus::Rect Beatmup::GDIBitmap::Impl::size

Definition at line 35 of file gdi_bitmap.cpp.

◆ data

Gdiplus::BitmapData Beatmup::GDIBitmap::Impl::data

Definition at line 36 of file gdi_bitmap.cpp.


The documentation for this class was generated from the following file: