Begin implementation of C++ Image data methods again

This commit is contained in:
Joshua Granick
2015-07-21 09:07:52 -07:00
parent 1354a5106b
commit c0ef737af0
17 changed files with 566 additions and 131 deletions

View File

@@ -28,8 +28,9 @@ namespace lime {
Bytes *data;
PixelFormat format;
int height;
int width;
bool premultiplied;
bool transparent;
int width;
private:

View File

@@ -7,9 +7,9 @@ namespace lime {
enum PixelFormat {
RGBA,
ARGB,
BGRA
RGBA32,
ARGB32,
BGRA32
};

View File

@@ -37,6 +37,32 @@ namespace lime {
};
class ImageDataView {
public:
ImageDataView (Image* image, Rectangle* rect);
void Clip (int x, int y, int width, int height);
int Row (int y);
int x;
int y;
int width;
int height;
private:
Image* image;
int offset;
Rectangle* rect;
int stride;
};
}

View File

@@ -4,6 +4,7 @@
#include <hx/CFFI.h>
#include <system/System.h>
#include <stdint.h>
namespace lime {
@@ -20,16 +21,24 @@ namespace lime {
float GetAlphaMultiplier ();
float GetAlphaOffset ();
void GetAlphaTable (unsigned char* table);
float GetBlueMultiplier ();
float GetBlueOffset ();
int GetColor ();
void GetBlueTable (unsigned char* table);
int32_t GetColor ();
float GetGreenMultiplier ();
float GetGreenOffset ();
void GetGreenTable (unsigned char* table);
float GetRedMultiplier ();
float GetRedOffset ();
void GetRedTable (unsigned char* table);
float data[20];
private:
void GetDataTable (unsigned char* table, float multiplier, float offset);
};

View File

@@ -17,6 +17,8 @@ namespace lime {
Rectangle (double x, double y, double width, double height);
Rectangle (value rect);
void Contract (double x, double y, double width, double height);
double height;
double width;
double x;

View File

@@ -0,0 +1,40 @@
#ifndef LIME_MATH_COLOR_MATRIX_H
#define LIME_MATH_COLOR_MATRIX_H
#include <hx/CFFI.h>
#include <system/System.h>
namespace lime {
class ColorMatrix {
public:
ColorMatrix ();
ColorMatrix (value colorMatrix);
~ColorMatrix ();
float GetAlphaMultiplier ();
float GetAlphaOffset ();
float GetBlueMultiplier ();
float GetBlueOffset ();
int GetColor ();
float GetGreenMultiplier ();
float GetGreenOffset ();
float GetRedMultiplier ();
float GetRedOffset ();
float data[20];
};
}
#endif

View File

@@ -0,0 +1,40 @@
#ifndef LIME_MATH_COLOR_MATRIX_H
#define LIME_MATH_COLOR_MATRIX_H
#include <hx/CFFI.h>
#include <system/System.h>
namespace lime {
class ColorMatrix {
public:
ColorMatrix ();
ColorMatrix (value colorMatrix);
~ColorMatrix ();
float GetAlphaMultiplier ();
float GetAlphaOffset ();
float GetBlueMultiplier ();
float GetBlueOffset ();
int GetColor ();
float GetGreenMultiplier ();
float GetGreenOffset ();
float GetRedMultiplier ();
float GetRedOffset ();
float data[20];
};
}
#endif

View File

@@ -0,0 +1,204 @@
#ifndef LIME_MATH_COLOR_RGBA_H
#define LIME_MATH_COLOR_RGBA_H
#include <graphics/PixelFormat.h>
#include <stdint.h>
namespace lime {
int __alpha16[0xFF + 1];
int __clamp[0xFF + 0xFF + 1];
static int a16;
static double unmult;
int initValues () {
for (int i = 0; i < 256; i++) {
// Seem to need +1 to get the same results as Haxe in multiplyAlpha
__alpha16[i] = (i + 1) * ((1 << 16) / 0xFF);
}
for (int i = 0; i < 0xFF; i++) {
__clamp[i] = i;
}
for (int i = 0xFF; i < (0xFF + 0xFF + 1); i++) {
__clamp[i] = 0xFF;
}
return 0;
}
static int initValues_ = initValues ();
struct RGBA {
public:
inline RGBA () {
r = 0;
g = 0;
b = 0;
a = 0;
}
inline RGBA (int32_t rgba) {
r = (rgba >> 24) & 0xFF;
g = (rgba >> 16) & 0xFF;
b = (rgba >> 8) & 0xFF;
a = rgba & 0xFF;
}
inline RGBA (unsigned char r, unsigned char g, unsigned char b, unsigned char a) {
Set (r, g, b, a);
}
inline int32_t Get () {
int32_t value = ((r & 0xFF) << 24) | ((g & 0xFF) << 16) | ((b & 0xFF) << 8) | (a & 0xFF);
return value;
}
inline void MultiplyAlpha () {
if (a == 0) {
Set (0, 0, 0, 0);
} else if (a != 0xFF) {
a16 = __alpha16[a];
Set ((r * a16) >> 16, (g * a16) >> 16, (b * a16) >> 16, a);
}
}
inline void UnmultiplyAlpha () {
if (a != 0 && a != 0xFF) {
unmult = 255.0 / a;
Set (__clamp[(int)(r * unmult)], __clamp[(int)(g * unmult)], __clamp[(int)(b * unmult)], a);
}
}
inline void ReadUInt8 (const unsigned char* data, int offset, PixelFormat format, bool premultiplied) {
switch (format) {
case BGRA32:
Set (data[offset + 2], data[offset + 1], data[offset], data[offset + 3]);
break;
case RGBA32:
Set (data[offset], data[offset + 1], data[offset + 2], data[offset + 3]);
break;
case ARGB32:
Set (data[offset + 1], data[offset + 2], data[offset + 3], data[offset]);
break;
}
if (premultiplied) {
UnmultiplyAlpha ();
}
}
inline void Set (unsigned char r, unsigned char g, unsigned char b, unsigned char a) {
this->r = r;
this->g = g;
this->b = b;
this->a = a;
}
inline void WriteUInt8 (unsigned char* data, int offset, PixelFormat format, bool premultiplied) {
if (premultiplied) {
MultiplyAlpha ();
}
switch (format) {
case BGRA32:
data[offset] = b;
data[offset + 1] = g;
data[offset + 2] = r;
data[offset + 3] = a;
break;
case RGBA32:
data[offset] = r;
data[offset + 1] = g;
data[offset + 2] = b;
data[offset + 3] = a;
break;
case ARGB32:
data[offset] = a;
data[offset + 1] = r;
data[offset + 2] = g;
data[offset + 3] = b;
break;
}
}
unsigned char r;
unsigned char g;
unsigned char b;
unsigned char a;
};
}
#endif