Skip to content

Commit 031740c

Browse files
author
Sebastien Bourdeauducq
committedNov 29, 2011
png: enable loading of RGBA images
1 parent f03c8c5 commit 031740c

File tree

4 files changed

+24
-12
lines changed

4 files changed

+24
-12
lines changed
 

‎src/pixbuf/dither.c

+20-8
Original file line numberDiff line numberDiff line change
@@ -82,7 +82,7 @@ static void floyd_steinberg(int *pic, int width, int height)
8282
}
8383
}
8484

85-
int pixbuf_dither(unsigned short *ret, unsigned char **row_pointers, int width, int height)
85+
int pixbuf_dither(unsigned short *ret, unsigned char **row_pointers, int width, int height, int has_alpha)
8686
{
8787
int x, y;
8888
unsigned char *row;
@@ -92,13 +92,25 @@ int pixbuf_dither(unsigned short *ret, unsigned char **row_pointers, int width,
9292
pic = malloc(width*height*3*sizeof(int));
9393
if(pic == NULL) return 0;
9494

95-
for(y=0;y<height;y++) {
96-
row = row_pointers[y];
97-
for(x=0;x<width;x++) {
98-
offset = 3*(width*y+x);
99-
pic[offset] = ((unsigned int)row[3*x]) << 16;
100-
pic[offset+1] = ((unsigned int)row[3*x+1]) << 16;
101-
pic[offset+2] = ((unsigned int)row[3*x+2]) << 16;
95+
if(has_alpha) {
96+
for(y=0;y<height;y++) {
97+
row = row_pointers[y];
98+
for(x=0;x<width;x++) {
99+
offset = 3*(width*y+x);
100+
pic[offset] = ((unsigned int)row[4*x]) << 16;
101+
pic[offset+1] = ((unsigned int)row[4*x+1]) << 16;
102+
pic[offset+2] = ((unsigned int)row[4*x+2]) << 16;
103+
}
104+
}
105+
} else {
106+
for(y=0;y<height;y++) {
107+
row = row_pointers[y];
108+
for(x=0;x<width;x++) {
109+
offset = 3*(width*y+x);
110+
pic[offset] = ((unsigned int)row[3*x]) << 16;
111+
pic[offset+1] = ((unsigned int)row[3*x+1]) << 16;
112+
pic[offset+2] = ((unsigned int)row[3*x+2]) << 16;
113+
}
102114
}
103115
}
104116

‎src/pixbuf/dither.h

+1-1
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,6 @@
1818
#ifndef __PIXBUF_DITHER_H
1919
#define __PIXBUF_DITHER_H
2020

21-
int pixbuf_dither(unsigned short *ret, unsigned char **row_pointers, int width, int height);
21+
int pixbuf_dither(unsigned short *ret, unsigned char **row_pointers, int width, int height, int has_alpha);
2222

2323
#endif /* __PIXBUF_DITHER_H */

‎src/pixbuf/loaderjpeg.c

+1-1
Original file line numberDiff line numberDiff line change
@@ -75,7 +75,7 @@ struct pixbuf *pixbuf_load_jpeg(char *filename)
7575

7676
ret = pixbuf_new(cinfo.image_width, cinfo.image_height);
7777
if(ret == NULL) goto free5;
78-
if(!pixbuf_dither(ret->pixels, row_pointers, cinfo.image_width, cinfo.image_height)) {
78+
if(!pixbuf_dither(ret->pixels, row_pointers, cinfo.image_width, cinfo.image_height, 0)) {
7979
pixbuf_dec_ref(ret);
8080
ret = NULL;
8181
goto free5;

‎src/pixbuf/loaderpng.c

+2-2
Original file line numberDiff line numberDiff line change
@@ -64,7 +64,7 @@ struct pixbuf *pixbuf_load_png(char *filename)
6464
color_type = png_get_color_type(png_ptr, info_ptr);
6565
bit_depth = png_get_bit_depth(png_ptr, info_ptr);
6666

67-
if(color_type != PNG_COLOR_TYPE_RGB) goto free3;
67+
if((color_type != PNG_COLOR_TYPE_RGB) && (color_type != PNG_COLOR_TYPE_RGBA)) goto free3;
6868
if(bit_depth != 8) goto free3;
6969

7070
row_pointers = calloc(sizeof(png_bytep), height);
@@ -82,7 +82,7 @@ struct pixbuf *pixbuf_load_png(char *filename)
8282
ret = pixbuf_new(width, height);
8383
if(ret == NULL) goto free4;
8484
ret->filename = strdup(filename);
85-
if(!pixbuf_dither(ret->pixels, row_pointers, width, height)) {
85+
if(!pixbuf_dither(ret->pixels, row_pointers, width, height, color_type == PNG_COLOR_TYPE_RGBA)) {
8686
pixbuf_dec_ref(ret);
8787
ret = NULL;
8888
goto free4;

0 commit comments

Comments
 (0)
Please sign in to comment.