refactor rgb565
This commit is contained in:
@@ -8,9 +8,9 @@
|
|||||||
static VALUE rb_decode_rgb565(VALUE self, VALUE rb_data, VALUE size, VALUE big) {
|
static VALUE rb_decode_rgb565(VALUE self, VALUE rb_data, VALUE size, VALUE big) {
|
||||||
if (RSTRING_LEN(rb_data) < FIX2LONG(size) * 2)
|
if (RSTRING_LEN(rb_data) < FIX2LONG(size) * 2)
|
||||||
rb_raise(rb_eStandardError, "Data size is not enough.");
|
rb_raise(rb_eStandardError, "Data size is not enough.");
|
||||||
uint8_t *image = (uint8_t*)malloc(FIX2LONG(size) * 3);
|
uint8_t *image = (uint8_t*)malloc(FIX2LONG(size) * 4);
|
||||||
decode_rgb565((uint16_t*)RSTRING_PTR(rb_data), FIX2INT(size), RTEST(big), image);
|
decode_rgb565((uint16_t*)RSTRING_PTR(rb_data), FIX2INT(size), RTEST(big), image);
|
||||||
VALUE ret = rb_str_new((char*)image, FIX2LONG(size) * 3);
|
VALUE ret = rb_str_new((char*)image, FIX2LONG(size) * 4);
|
||||||
free(image);
|
free(image);
|
||||||
return ret;
|
return ret;
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -7,24 +7,20 @@ static inline int is_system_little() {
|
|||||||
|
|
||||||
void decode_rgb565(const uint16_t* data, const int size, const int is_big_endian, uint8_t* image) {
|
void decode_rgb565(const uint16_t* data, const int size, const int is_big_endian, uint8_t* image) {
|
||||||
const uint16_t *d = data;
|
const uint16_t *d = data;
|
||||||
uint8_t *p = image;
|
|
||||||
if (is_big_endian == is_system_little()) {
|
if (is_big_endian == is_system_little()) {
|
||||||
for (int i = 0; i < size; i++, d++, p += 3) {
|
uint8_t *p = image;
|
||||||
uint8_t r = *d & 0x00f8;
|
for (int i = 0; i < size; i++, d++, p += 4) {
|
||||||
uint8_t g = (*d & 0x0007) << 5 | (*d & 0xe000) >> 11;
|
uint_fast8_t r = *d & 0x00f8;
|
||||||
uint8_t b = (*d & 0x1f00) >> 5;
|
uint_fast8_t g = (*d & 0x0007) << 5 | (*d & 0xe000) >> 11;
|
||||||
|
uint_fast8_t b = (*d & 0x1f00) >> 5;
|
||||||
p[0] = r | r >> 5;
|
p[0] = r | r >> 5;
|
||||||
p[1] = g | g >> 6;
|
p[1] = g | g >> 6;
|
||||||
p[2] = b | b >> 5;
|
p[2] = b | b >> 5;
|
||||||
|
p[3] = 255;
|
||||||
}
|
}
|
||||||
} else {
|
} else {
|
||||||
for (int i = 0; i < size; i++, d++, p += 3) {
|
uint32_t *p = (uint32_t*)image;
|
||||||
uint8_t r = (*d & 0xf800) >> 8;
|
for (int i = 0; i < size; i++, d++, p++)
|
||||||
uint8_t g = (*d & 0x07e0) >> 3;
|
*p = (*d & 0xf800) >> 8 | *d >> 13 | (*d & 0x7e0) << 5 | (*d & 0x60) << 3 | *d << 19 | (*d & 0x1c) << 14 | 0xff000000;
|
||||||
uint8_t b = (*d & 0x001f) << 3;
|
|
||||||
p[0] = r | r >> 5;
|
|
||||||
p[1] = g | g >> 6;
|
|
||||||
p[2] = b | b >> 5;
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -126,7 +126,7 @@ module Mikunyan
|
|||||||
# @param [Symbol] endian endianness of binary
|
# @param [Symbol] endian endianness of binary
|
||||||
# @return [ChunkyPNG::Image] decoded image
|
# @return [ChunkyPNG::Image] decoded image
|
||||||
def self.decode_rgb565(width, height, bin, endian = :big)
|
def self.decode_rgb565(width, height, bin, endian = :big)
|
||||||
ChunkyPNG::Image.from_rgb_stream(width, height, DecodeHelper.decode_rgb565(bin, width * height, endian == :big)).flip
|
ChunkyPNG::Image.from_rgba_stream(width, height, DecodeHelper.decode_rgb565(bin, width * height, endian == :big)).flip
|
||||||
end
|
end
|
||||||
|
|
||||||
# Decode image from A8 binary
|
# Decode image from A8 binary
|
||||||
|
|||||||
Reference in New Issue
Block a user