improve implementation and actually add docs for colour re-order. Fix #217

This commit is contained in:
Gordon Williams 2015-04-14 16:48:56 +01:00
parent 5e3bac7456
commit 1181f166a5
2 changed files with 50 additions and 46 deletions

View File

@ -33,15 +33,16 @@ typedef enum {
JSGRAPHICSFLAGS_SWAP_XY = 8, //< All devices: swap X and Y over
JSGRAPHICSFLAGS_INVERT_X = 16, //< All devices: x = getWidth() - (x+1) - where x is DEVICE X
JSGRAPHICSFLAGS_INVERT_Y = 32, //< All devices: y = getHeight() - (y+1) - where y is DEVICE Y
JSGRAPHICSFLAGS_COLOR_RGB = 0,
JSGRAPHICSFLAGS_COLOR_BRG = 64, //< All devices: color order is BRG
JSGRAPHICSFLAGS_COLOR_BGR = 128, //< All devices: color order is BGR
JSGRAPHICSFLAGS_COLOR_GBR = 256, //< All devices: color order is GBR
JSGRAPHICSFLAGS_COLOR_GRB = 512, //< All devices: color order is GRB
JSGRAPHICSFLAGS_COLOR_RBG = 1024, //< All devices: color order is RBG
JSGRAPHICSFLAGS_COLOR_GBR = 64+128, //< All devices: color order is GBR
JSGRAPHICSFLAGS_COLOR_GRB = 256, //< All devices: color order is GRB
JSGRAPHICSFLAGS_COLOR_RBG = 256+64, //< All devices: color order is RBG
JSGRAPHICSFLAGS_COLOR_MASK = 64+128+256, //< All devices: color order is BRG
} JsGraphicsFlags;
#define JSGRAPHICSFLAGS_COLOR_MASK (JSGRAPHICSFLAGS_COLOR_BRG | JSGRAPHICSFLAGS_COLOR_BGR | JSGRAPHICSFLAGS_COLOR_GBR | JSGRAPHICSFLAGS_COLOR_GRB | JSGRAPHICSFLAGS_COLOR_RBG)
#define JSGRAPHICS_FONTSIZE_4X6 (-1) // a bitmap font
#define JSGRAPHICS_FONTSIZE_CUSTOM (-2) // a custom bitmap font made from fields in the graphics object (See below)
// Positive font sizes are Vector fonts

View File

@ -27,8 +27,6 @@
#endif
#include "bitmap_font_4x6.h"
#include <string.h>
/*JSON{
"type" : "class",
"class" : "Graphics"
@ -89,7 +87,13 @@ static bool isValidBPP(int bpp) {
["width","int32","Pixels wide"],
["height","int32","Pixels high"],
["bpp","int32","Number of bits per pixel"],
["options","JsVar",["An object of other options. ```{ zigzag : true/false(default), vertical_byte : true/false(default), msb : true/false(default) }```","zigzag = whether to alternate the direction of scanlines for rows","vertical_byte = whether to align bits in a byte vertically or not","msb = when bits<8, store pixels msb first"]]
["options","JsVar",[
"An object of other options. ```{ zigzag : true/false(default), vertical_byte : true/false(default), msb : true/false(default), color_order: 'rgb'(default),'bgr',etc }```",
"zigzag = whether to alternate the direction of scanlines for rows",
"vertical_byte = whether to align bits in a byte vertically or not",
"msb = when bits<8, store pixels msb first",
"color_order = re-orders the colour values that are supplied via setColor"
]]
],
"return" : ["JsVar","The new Graphics object"],
"return_object" : "Graphics"
@ -129,31 +133,22 @@ JsVar *jswrap_graphics_createArrayBuffer(int width, int height, int bpp, JsVar *
else
jsWarn("vertical_byte only works for 1bpp ArrayBuffers\n");
}
JsVar *colorv;
char color_order[4];
size_t len;
if ((colorv = jsvObjectGetChild(options, "color_order", 0)) != NULL) {
len = jsvGetString(colorv, color_order, 4);
jsvUnLock(colorv);
if (len != 3)
jsExceptionHere(JSET_ERROR, "color_order must be 3 characters");
if (!strcasecmp(color_order, "rgb"))
; // The default
else if (!strcasecmp(color_order, "brg"))
JsVar *colorv = jsvObjectGetChild(options, "color_order", 0);
if (colorv) {
if (jsvIsStringEqual(colorv, "rgb")) ; // The default
else if (!jsvIsStringEqual(colorv, "brg"))
gfx.data.flags = (JsGraphicsFlags)(gfx.data.flags | JSGRAPHICSFLAGS_COLOR_BRG);
else if (!strcasecmp(color_order, "bgr"))
else if (!jsvIsStringEqual(colorv, "bgr"))
gfx.data.flags = (JsGraphicsFlags)(gfx.data.flags | JSGRAPHICSFLAGS_COLOR_BGR);
else if (!strcasecmp(color_order, "gbr"))
else if (!jsvIsStringEqual(colorv, "gbr"))
gfx.data.flags = (JsGraphicsFlags)(gfx.data.flags | JSGRAPHICSFLAGS_COLOR_GBR);
else if (!strcasecmp(color_order, "grb"))
else if (!jsvIsStringEqual(colorv, "grb"))
gfx.data.flags = (JsGraphicsFlags)(gfx.data.flags | JSGRAPHICSFLAGS_COLOR_GRB);
else if (!strcasecmp(color_order, "RBG"))
else if (!jsvIsStringEqual(colorv, "rbg"))
gfx.data.flags = (JsGraphicsFlags)(gfx.data.flags | JSGRAPHICSFLAGS_COLOR_RBG);
else {
jsExceptionHere(JSET_ERROR, "color_order must be 3 characters");
return 0; // XXX: free parent?
}
else
jsWarn("color_order must be 3 characters");
jsvUnLock(colorv);
}
}
@ -507,28 +502,36 @@ void jswrap_graphics_setColorX(JsVar *parent, JsVar *r, JsVar *g, JsVar *b, bool
if (gi<0) gi=0;
if (bi<0) bi=0;
// Check if we need to twiddle colors
if (gfx.data.flags & JSGRAPHICSFLAGS_COLOR_MASK) {
int colorMask = gfx.data.flags & JSGRAPHICSFLAGS_COLOR_MASK;
if (colorMask) {
int tmpr, tmpg, tmpb;
tmpr = ri;
tmpg = gi;
tmpb = bi;
if (gfx.data.flags & JSGRAPHICSFLAGS_COLOR_BRG) {
ri = tmpb;
gi = tmpr;
bi = tmpg;
} else if (gfx.data.flags & JSGRAPHICSFLAGS_COLOR_BGR) {
ri = tmpb;
bi = tmpr;
} else if (gfx.data.flags & JSGRAPHICSFLAGS_COLOR_GBR) {
ri = tmpg;
gi = tmpb;
bi = tmpr;
} else if (gfx.data.flags & JSGRAPHICSFLAGS_COLOR_GRB) {
ri = tmpg;
gi = tmpr;
} else if (gfx.data.flags & JSGRAPHICSFLAGS_COLOR_RBG) {
gi = tmpb;
bi = tmpg;
switch (colorMask) {
case JSGRAPHICSFLAGS_COLOR_BRG:
ri = tmpb;
gi = tmpr;
bi = tmpg;
break;
case JSGRAPHICSFLAGS_COLOR_BGR:
ri = tmpb;
bi = tmpr;
break;
case JSGRAPHICSFLAGS_COLOR_GBR:
ri = tmpg;
gi = tmpb;
bi = tmpr;
break;
case JSGRAPHICSFLAGS_COLOR_GRB:
ri = tmpg;
gi = tmpr;
break;
case JSGRAPHICSFLAGS_COLOR_RBG:
gi = tmpb;
bi = tmpg;
break;
default: break;
}
}
if (gfx.data.bpp==16) {