Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with
or
.
Download ZIP
Browse files

fbgrab remove the alpha channel

  • Loading branch information...
commit 6d1b786b02b7d8516e4061a2dbdce4c96d65c4a5 1 parent e4c34db
Xiangfu Liu authored
Showing with 26 additions and 37 deletions.
  1. +15 −20 src/fbgrab.c
  2. +11 −17 src/pngload.c
View
35 src/fbgrab.c
@@ -49,24 +49,21 @@ static char *get_name()
return filename;
}
-static void convert565to32(int width, int height,
+static void convert565to24(int width, int height,
unsigned char *inbuffer,
unsigned char *outbuffer)
{
unsigned int i;
- for (i=0; i < height*width*2; i+=2)
- {
+ for (i=0; i < height*width*2; i+=2) {
/* BLUE = 0 */
- outbuffer[(i<<1)+0] = (inbuffer[i+1] & 0x1f) << 3;
+ outbuffer[(i/2*3)+0] = (inbuffer[i+1] & 0x1f) << 3;
/* GREEN = 1 */
- outbuffer[(i<<1)+1] =
+ outbuffer[(i/2*3)+1] =
(((inbuffer[i] & 0x7) << 3) | (inbuffer[i+1] & 0xE0) >> 5)
<< 2;
/* RED = 2 */
- outbuffer[(i<<1)+2] = (inbuffer[i] & 0xF8);
- /* ALPHA = 3 */
- outbuffer[(i<<1)+3] = '\0';
+ outbuffer[(i/2*3)+2] = (inbuffer[i] & 0xF8);
}
}
@@ -74,22 +71,20 @@ static int convert_and_write(unsigned char *inbuffer, char *filename,
int width, int height, int bits, int interlace)
{
int ret = 0;
-
- size_t bufsize = width * height * 4;
+ size_t bufsize = width * height * 3;
unsigned char *outbuffer = malloc(bufsize);
if (outbuffer == NULL) {
fprintf(stderr, "Not enough memory");
return -1;
}
-
memset(outbuffer, 0, bufsize);
printf("grab screen to %s\n", filename);
switch (bits) {
case 16:
- convert565to32(width, height, inbuffer, outbuffer);
+ convert565to24(width, height, inbuffer, outbuffer);
ret = png_write(outbuffer, filename, width, height, interlace);
break;
case 15:
@@ -107,21 +102,20 @@ static int convert_and_write(unsigned char *inbuffer, char *filename,
int fbgrab(char *fn)
{
int fd;
- unsigned char *buf_p;
- int ret = 0;
+
struct fb_var_screeninfo fb_var;
- unsigned int bitdepth = 16;
size_t width;
size_t height;
size_t buf_size;
- int interlace = PNG_INTERLACE_NONE;
+ unsigned char *buf_p;
char *device = "/dev/fb";
char *outfile = NULL;
+ int ret = 0;
- if(fn == NULL) {
+ if(fn == NULL)
outfile = get_name();
- } else
+ else
outfile = fn;
fd = open(device, O_RDONLY);
@@ -149,8 +143,9 @@ int fbgrab(char *fn)
}
ret = convert_and_write(buf_p, outfile,
- width, height, bitdepth,
- interlace);
+ width, height, 16, /* bit depth */
+ PNG_INTERLACE_NONE);
+
free0:
free(buf_p);
close0:
View
28 src/pngload.c
@@ -201,14 +201,13 @@ int png_write(unsigned char *outbuffer, const char *filename,
int width, int height, int interlace)
{
int i;
- int bit_depth = 0, color_type;
png_bytep row_pointers[height];
png_structp png_ptr;
png_infop info_ptr;
FILE *outfile;
for (i=0; i<height; i++)
- row_pointers[i] = outbuffer + i * 4 * width;
+ row_pointers[i] = outbuffer + i * width * 3;
outfile = fopen(filename, "w");
if (outfile == NULL) {
@@ -217,40 +216,35 @@ int png_write(unsigned char *outbuffer, const char *filename,
}
png_ptr = png_create_write_struct(PNG_LIBPNG_VER_STRING, NULL, NULL, NULL);
-
if (!png_ptr) {
- fprintf(stderr, "Error: Couldn't create PNG write struct");
- return -1;
+ fprintf(stderr, "Error: Couldn't create PNG write struct\n");
+ goto free0;
}
info_ptr = png_create_info_struct(png_ptr);
if (!info_ptr) {
- png_destroy_write_struct(&png_ptr, (png_infopp) NULL);
- fprintf(stderr, "Error: Couldn't create PNG info struct");
- return -1;
+ fprintf(stderr, "Error: Couldn't create PNG info struct\n");
+ goto free1;
}
+ if (setjmp(png_jmpbuf(png_ptr))) goto free1;
+
png_init_io(png_ptr, outfile);
png_set_compression_level(png_ptr, Z_DEFAULT_COMPRESSION);
-
- bit_depth = 8;
- color_type = PNG_COLOR_TYPE_RGB_ALPHA;
- png_set_invert_alpha(png_ptr);
png_set_bgr(png_ptr);
-
png_set_IHDR(png_ptr, info_ptr, width, height,
- bit_depth, color_type, interlace,
+ 8, PNG_COLOR_TYPE_RGB, interlace,
PNG_COMPRESSION_TYPE_DEFAULT, PNG_FILTER_TYPE_DEFAULT);
png_write_info(png_ptr, info_ptr);
-
png_write_image(png_ptr, row_pointers);
-
png_write_end(png_ptr, info_ptr);
- /* puh, done, now freeing memory... */
+
+free1:
png_destroy_write_struct(&png_ptr, &info_ptr);
+free0:
fclose(outfile);
return 0;
Please sign in to comment.
Something went wrong with that request. Please try again.