Skip to content

Commit

Permalink
png: enable loading of RGBA images
Browse files Browse the repository at this point in the history
  • Loading branch information
Sebastien Bourdeauducq committed Nov 29, 2011
1 parent f03c8c5 commit 031740c
Show file tree
Hide file tree
Showing 4 changed files with 24 additions and 12 deletions.
28 changes: 20 additions & 8 deletions src/pixbuf/dither.c
Expand Up @@ -82,7 +82,7 @@ static void floyd_steinberg(int *pic, int width, int height)
}
}

int pixbuf_dither(unsigned short *ret, unsigned char **row_pointers, int width, int height)
int pixbuf_dither(unsigned short *ret, unsigned char **row_pointers, int width, int height, int has_alpha)
{
int x, y;
unsigned char *row;
Expand All @@ -92,13 +92,25 @@ int pixbuf_dither(unsigned short *ret, unsigned char **row_pointers, int width,
pic = malloc(width*height*3*sizeof(int));
if(pic == NULL) return 0;

for(y=0;y<height;y++) {
row = row_pointers[y];
for(x=0;x<width;x++) {
offset = 3*(width*y+x);
pic[offset] = ((unsigned int)row[3*x]) << 16;
pic[offset+1] = ((unsigned int)row[3*x+1]) << 16;
pic[offset+2] = ((unsigned int)row[3*x+2]) << 16;
if(has_alpha) {
for(y=0;y<height;y++) {
row = row_pointers[y];
for(x=0;x<width;x++) {
offset = 3*(width*y+x);
pic[offset] = ((unsigned int)row[4*x]) << 16;
pic[offset+1] = ((unsigned int)row[4*x+1]) << 16;
pic[offset+2] = ((unsigned int)row[4*x+2]) << 16;
}
}
} else {
for(y=0;y<height;y++) {
row = row_pointers[y];
for(x=0;x<width;x++) {
offset = 3*(width*y+x);
pic[offset] = ((unsigned int)row[3*x]) << 16;
pic[offset+1] = ((unsigned int)row[3*x+1]) << 16;
pic[offset+2] = ((unsigned int)row[3*x+2]) << 16;
}
}
}

Expand Down
2 changes: 1 addition & 1 deletion src/pixbuf/dither.h
Expand Up @@ -18,6 +18,6 @@
#ifndef __PIXBUF_DITHER_H
#define __PIXBUF_DITHER_H

int pixbuf_dither(unsigned short *ret, unsigned char **row_pointers, int width, int height);
int pixbuf_dither(unsigned short *ret, unsigned char **row_pointers, int width, int height, int has_alpha);

#endif /* __PIXBUF_DITHER_H */
2 changes: 1 addition & 1 deletion src/pixbuf/loaderjpeg.c
Expand Up @@ -75,7 +75,7 @@ struct pixbuf *pixbuf_load_jpeg(char *filename)

ret = pixbuf_new(cinfo.image_width, cinfo.image_height);
if(ret == NULL) goto free5;
if(!pixbuf_dither(ret->pixels, row_pointers, cinfo.image_width, cinfo.image_height)) {
if(!pixbuf_dither(ret->pixels, row_pointers, cinfo.image_width, cinfo.image_height, 0)) {
pixbuf_dec_ref(ret);
ret = NULL;
goto free5;
Expand Down
4 changes: 2 additions & 2 deletions src/pixbuf/loaderpng.c
Expand Up @@ -64,7 +64,7 @@ struct pixbuf *pixbuf_load_png(char *filename)
color_type = png_get_color_type(png_ptr, info_ptr);
bit_depth = png_get_bit_depth(png_ptr, info_ptr);

if(color_type != PNG_COLOR_TYPE_RGB) goto free3;
if((color_type != PNG_COLOR_TYPE_RGB) && (color_type != PNG_COLOR_TYPE_RGBA)) goto free3;
if(bit_depth != 8) goto free3;

row_pointers = calloc(sizeof(png_bytep), height);
Expand All @@ -82,7 +82,7 @@ struct pixbuf *pixbuf_load_png(char *filename)
ret = pixbuf_new(width, height);
if(ret == NULL) goto free4;
ret->filename = strdup(filename);
if(!pixbuf_dither(ret->pixels, row_pointers, width, height)) {
if(!pixbuf_dither(ret->pixels, row_pointers, width, height, color_type == PNG_COLOR_TYPE_RGBA)) {
pixbuf_dec_ref(ret);
ret = NULL;
goto free4;
Expand Down

0 comments on commit 031740c

Please sign in to comment.