31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
|
# File 'ext/imageruby_bmp_base/imageruby_bmp_base.c', line 31
VALUE rb_decode_bitmap(VALUE recv, VALUE rb_data, VALUE rb_image) {
const char* data_string = RSTRING(rb_data)->ptr;
const char* dib_header = data_string + 14;
const char* header = data_string;
int pixel_data_offset = get_int(header+10);
int width = get_int(dib_header+4);
int height = get_int(dib_header+8);
int bpp = get_int(dib_header+14);
VALUE rb_pixel_data = rb_funcall(rb_image, id_pixel_data, 0);
char* image_pixel_data_string = RSTRING(rb_pixel_data)->ptr;
const char* file_pixel_data_string = data_string+pixel_data_offset;
if (bpp == 24) {
int padding_size = ( 4 - (width * 3 % 4) ) % 4;
int width_array_len = width*3+padding_size;
int offset;
int y;
for (y=0; y<height; y++) {
memcpy(image_pixel_data_string+(y*width)*3, file_pixel_data_string+(height-y-1)*width_array_len, width*3);
}
} else if (bpp == 32) {
int width_array_len = width*3;
int offset;
int x,y;
VALUE rb_alpha_data = rb_funcall(rb_image, id_alpha_data, 0);
char* alpha_data_string = RSTRING(rb_alpha_data)->ptr;
for (y=0; y<height; y++) {
for (x=0; x<width; x++) {
int offset = ((height-y-1)*width+x)*4;
int index = (y*width+x)*3;
image_pixel_data_string[index] = file_pixel_data_string[offset];
image_pixel_data_string[index+1] = file_pixel_data_string[offset+1];
image_pixel_data_string[index+2] = file_pixel_data_string[offset+2];
alpha_data_string[y*width+x] = file_pixel_data_string[offset+3];
}
}
}
return Qnil;
}
|