Skip to content
This repository

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP
Browse code

fbgrab remove the alpha channel

  • Loading branch information...
commit 6d1b786b02b7d8516e4061a2dbdce4c96d65c4a5 1 parent e4c34db
authored April 13, 2011
35  src/fbgrab.c
@@ -49,24 +49,21 @@ static char *get_name()
49 49
 	return filename;
50 50
 }
51 51
 
52  
-static void convert565to32(int width, int height,
  52
+static void convert565to24(int width, int height,
53 53
 			   unsigned char *inbuffer,
54 54
 			   unsigned char *outbuffer)
55 55
 {
56 56
 	unsigned int i;
57 57
 
58  
-	for (i=0; i < height*width*2; i+=2)
59  
-	{
  58
+	for (i=0; i < height*width*2; i+=2) {
60 59
 		/* BLUE  = 0 */
61  
-		outbuffer[(i<<1)+0] = (inbuffer[i+1] & 0x1f) << 3;
  60
+		outbuffer[(i/2*3)+0] = (inbuffer[i+1] & 0x1f) << 3;
62 61
 		/* GREEN = 1 */
63  
-		outbuffer[(i<<1)+1] = 
  62
+		outbuffer[(i/2*3)+1] =
64 63
 			(((inbuffer[i] & 0x7) << 3) | (inbuffer[i+1] & 0xE0) >> 5) 
65 64
 			<< 2;
66 65
 		/* RED   = 2 */
67  
-		outbuffer[(i<<1)+2] = (inbuffer[i] & 0xF8);
68  
-		/* ALPHA = 3 */
69  
-		outbuffer[(i<<1)+3] = '\0';
  66
+		outbuffer[(i/2*3)+2] = (inbuffer[i] & 0xF8);
70 67
 	}
71 68
 }
72 69
 
@@ -74,22 +71,20 @@ static int convert_and_write(unsigned char *inbuffer, char *filename,
74 71
 			     int width, int height, int bits, int interlace)
75 72
 {
76 73
 	int ret = 0;
77  
-
78  
-	size_t bufsize = width * height * 4;
  74
+	size_t bufsize = width * height * 3;
79 75
 
80 76
 	unsigned char *outbuffer = malloc(bufsize);
81 77
 	if (outbuffer == NULL) {
82 78
 		fprintf(stderr, "Not enough memory");
83 79
 		return -1;
84 80
 	}
85  
-
86 81
 	memset(outbuffer, 0, bufsize);
87 82
 
88 83
 	printf("grab screen to %s\n", filename);
89 84
 
90 85
 	switch (bits) {
91 86
 	case 16:
92  
-		convert565to32(width, height, inbuffer, outbuffer);
  87
+		convert565to24(width, height, inbuffer, outbuffer);
93 88
 		ret = png_write(outbuffer, filename, width, height, interlace);
94 89
 		break;
95 90
 	case 15:
@@ -107,21 +102,20 @@ static int convert_and_write(unsigned char *inbuffer, char *filename,
107 102
 int fbgrab(char *fn)
108 103
 {
109 104
 	int fd;
110  
-	unsigned char *buf_p;
111  
-	int ret = 0;
  105
+
112 106
 	struct fb_var_screeninfo fb_var;
113  
-	unsigned int bitdepth = 16;
114 107
 	size_t width;
115 108
 	size_t height;
116 109
 	size_t buf_size;
117  
-	int interlace = PNG_INTERLACE_NONE;
  110
+	unsigned char *buf_p;
118 111
 
119 112
 	char *device = "/dev/fb";
120 113
 	char *outfile = NULL;
  114
+	int ret = 0;
121 115
 
122  
-	if(fn == NULL) {
  116
+	if(fn == NULL)
123 117
 		outfile = get_name();
124  
-	} else
  118
+	else
125 119
 		outfile = fn;
126 120
 
127 121
 	fd = open(device, O_RDONLY);
@@ -149,8 +143,9 @@ int fbgrab(char *fn)
149 143
 	}
150 144
 
151 145
 	ret = convert_and_write(buf_p, outfile,
152  
-			  width, height, bitdepth,
153  
-			  interlace);
  146
+				width, height, 16, /* bit depth */
  147
+				PNG_INTERLACE_NONE);
  148
+
154 149
 free0:
155 150
 	free(buf_p);
156 151
 close0:
28  src/pngload.c
@@ -201,14 +201,13 @@ int png_write(unsigned char *outbuffer, const char *filename,
201 201
 	     int width, int height, int interlace)
202 202
 {
203 203
 	int i;
204  
-	int bit_depth = 0, color_type;
205 204
 	png_bytep row_pointers[height];
206 205
 	png_structp png_ptr;
207 206
 	png_infop info_ptr;
208 207
 	FILE *outfile;
209 208
 
210 209
 	for (i=0; i<height; i++)
211  
-		row_pointers[i] = outbuffer + i * 4 * width;
  210
+		row_pointers[i] = outbuffer + i * width * 3;
212 211
 
213 212
 	outfile = fopen(filename, "w");
214 213
 	if (outfile == NULL) {
@@ -217,40 +216,35 @@ int png_write(unsigned char *outbuffer, const char *filename,
217 216
 	}
218 217
 
219 218
 	png_ptr = png_create_write_struct(PNG_LIBPNG_VER_STRING, NULL, NULL, NULL);
220  
-
221 219
 	if (!png_ptr) {
222  
-		fprintf(stderr, "Error: Couldn't create PNG write struct");
223  
-		return -1;
  220
+		fprintf(stderr, "Error: Couldn't create PNG write struct\n");
  221
+		goto free0;
224 222
 	}
225 223
 
226 224
 	info_ptr = png_create_info_struct(png_ptr);
227 225
 	if (!info_ptr) {
228  
-		png_destroy_write_struct(&png_ptr, (png_infopp) NULL);
229  
-		fprintf(stderr, "Error: Couldn't create PNG info struct");
230  
-		return -1;
  226
+		fprintf(stderr, "Error: Couldn't create PNG info struct\n");
  227
+		goto free1;
231 228
 	}
232 229
 
  230
+	if (setjmp(png_jmpbuf(png_ptr))) goto free1;
  231
+
233 232
 	png_init_io(png_ptr, outfile);
234 233
 
235 234
 	png_set_compression_level(png_ptr, Z_DEFAULT_COMPRESSION);
236  
-
237  
-	bit_depth = 8;
238  
-	color_type = PNG_COLOR_TYPE_RGB_ALPHA;
239  
-	png_set_invert_alpha(png_ptr);
240 235
 	png_set_bgr(png_ptr);
241  
-
242 236
 	png_set_IHDR(png_ptr, info_ptr, width, height,
243  
-		     bit_depth, color_type, interlace,
  237
+		     8, PNG_COLOR_TYPE_RGB, interlace,
244 238
 		     PNG_COMPRESSION_TYPE_DEFAULT, PNG_FILTER_TYPE_DEFAULT);
245 239
 
246 240
 	png_write_info(png_ptr, info_ptr);
247  
-
248 241
 	png_write_image(png_ptr, row_pointers);
249  
-
250 242
 	png_write_end(png_ptr, info_ptr);
251  
-	/* puh, done, now freeing memory... */
  243
+
  244
+free1:
252 245
 	png_destroy_write_struct(&png_ptr, &info_ptr);
253 246
 
  247
+free0:
254 248
 	fclose(outfile);
255 249
 
256 250
 	return 0;

0 notes on commit 6d1b786

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