Skip to content
This repository

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP
Browse code

png: enable loading of RGBA images

  • Loading branch information...
commit 031740c39fd11b7d3ac4ebd51efb31e2d2ff0b6b 1 parent f03c8c5
Sébastien Bourdeauducq authored November 29, 2011
28  src/pixbuf/dither.c
@@ -82,7 +82,7 @@ static void floyd_steinberg(int *pic, int width, int height)
82 82
 		}
83 83
 }
84 84
 
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)
86 86
 {
87 87
 	int x, y;
88 88
 	unsigned char *row;
@@ -92,13 +92,25 @@ int pixbuf_dither(unsigned short *ret, unsigned char **row_pointers, int width,
92 92
 	pic = malloc(width*height*3*sizeof(int));
93 93
 	if(pic == NULL) return 0;
94 94
 	
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
+			}
102 114
 		}
103 115
 	}
104 116
 	
2  src/pixbuf/dither.h
@@ -18,6 +18,6 @@
18 18
 #ifndef __PIXBUF_DITHER_H
19 19
 #define __PIXBUF_DITHER_H
20 20
 
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);
22 22
 
23 23
 #endif /* __PIXBUF_DITHER_H */
2  src/pixbuf/loaderjpeg.c
@@ -75,7 +75,7 @@ struct pixbuf *pixbuf_load_jpeg(char *filename)
75 75
 
76 76
 	ret = pixbuf_new(cinfo.image_width, cinfo.image_height);
77 77
 	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)) {
79 79
 		pixbuf_dec_ref(ret);
80 80
 		ret = NULL;
81 81
 		goto free5;
4  src/pixbuf/loaderpng.c
@@ -64,7 +64,7 @@ struct pixbuf *pixbuf_load_png(char *filename)
64 64
 	color_type = png_get_color_type(png_ptr, info_ptr);
65 65
 	bit_depth = png_get_bit_depth(png_ptr, info_ptr);
66 66
 
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;
68 68
 	if(bit_depth != 8) goto free3;
69 69
 
70 70
 	row_pointers = calloc(sizeof(png_bytep), height);
@@ -82,7 +82,7 @@ struct pixbuf *pixbuf_load_png(char *filename)
82 82
 	ret = pixbuf_new(width, height);
83 83
 	if(ret == NULL) goto free4;
84 84
 	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)) {
86 86
 		pixbuf_dec_ref(ret);
87 87
 		ret = NULL;
88 88
 		goto free4;

0 notes on commit 031740c

Please sign in to comment.
Something went wrong with that request. Please try again.