Skip to content

Commit

Permalink
Patch #2 from Peter Meerwald:
Browse files Browse the repository at this point in the history
* allows to use stdout to write the image, and hence pipelining.
  • Loading branch information
Gunnar Monell committed Sep 30, 2013
1 parent 737a48f commit 89a39b9
Showing 1 changed file with 52 additions and 46 deletions.
98 changes: 52 additions & 46 deletions fbgrab.c
Original file line number Diff line number Diff line change
Expand Up @@ -44,31 +44,31 @@ static int Alpha = 3;

static void usage(char *binary)
{
printf("Usage: %s\t[-hi] [-{C|c} vt] [-d dev] [-s n]\n"
fprintf(stderr, "Usage: %s\t[-hi] [-{C|c} vt] [-d dev] [-s n]\n"
"\t\t[-f fromfile -w n -h n -b n] filename.png\n", binary);
}

static void help(char *binary)
{
printf("fbgrab - takes screenshots using the framebuffer, v%s\n", VERSION);
fprintf(stderr, "fbgrab - takes screenshots using the framebuffer, v%s\n", VERSION);

usage(binary);

printf("\nPossible options:\n");
fprintf(stderr, "\nPossible options:\n");
/* please keep this list alphabetical */
printf("\t-b n \tforce use of n bits/pixel, required when reading from file\n");
printf("\t-C n \tgrab from console n, for slower framebuffers\n");
printf("\t-c n \tgrab from console n\n");
printf("\t-d dev\tuse framebuffer device dev instead of default\n");
printf("\t-f file\t read from file instead of framebuffer\n");
printf("\t-h n \tset height to n pixels, required when reading from file\n"
fprintf(stderr, "\t-b n \tforce use of n bits/pixel, required when reading from file\n");
fprintf(stderr, "\t-C n \tgrab from console n, for slower framebuffers\n");
fprintf(stderr, "\t-c n \tgrab from console n\n");
fprintf(stderr, "\t-d dev\tuse framebuffer device dev instead of default\n");
fprintf(stderr, "\t-f file\t read from file instead of framebuffer\n");
fprintf(stderr, "\t-h n \tset height to n pixels, required when reading from file\n"
"\t\tcan be used to force height when reading from framebuffer\n");
printf("\t-i \tturns on interlacing in PNG\n");
printf("\t-s n \tsleep n seconds before making screenshot\n");
printf("\t-v \tverbose, print debug information.\n");
printf("\t-w n \tset width to n pixels, required when reading from file\n"
fprintf(stderr, "\t-i \tturns on interlacing in PNG\n");
fprintf(stderr, "\t-s n \tsleep n seconds before making screenshot\n");
fprintf(stderr, "\t-v \tverbose, print debug information.\n");
fprintf(stderr, "\t-w n \tset width to n pixels, required when reading from file\n"
"\t\tcan be used to force width when reading from framebuffer\n");
printf("\t-? \tprint this usage information\n");
fprintf(stderr, "\t-? \tprint this usage information\n");
}


Expand Down Expand Up @@ -119,7 +119,7 @@ static void get_framebufferdata(char *device, struct fb_var_screeninfo *fb_varin
/* now open framebuffer device */
if(-1 == (fd=open(device, O_RDONLY)))
{
fprintf (stderr, "Error: Couldn't open %s.\n", device);
fprintf(stderr, "Error: Couldn't open %s.\n", device);
exit(EXIT_FAILURE);
}

Expand All @@ -131,42 +131,42 @@ static void get_framebufferdata(char *device, struct fb_var_screeninfo *fb_varin

if (verbose)
{
printf("frame buffer fixed info:\n");
printf("id: \"%s\"\n", fb_fixedinfo.id);
fprintf(stderr, "frame buffer fixed info:\n");
fprintf(stderr, "id: \"%s\"\n", fb_fixedinfo.id);
switch (fb_fixedinfo.type)
{
case FB_TYPE_PACKED_PIXELS:
printf("type: packed pixels\n");
fprintf(stderr, "type: packed pixels\n");
break;
case FB_TYPE_PLANES:
printf("type: non interleaved planes\n");
fprintf(stderr, "type: non interleaved planes\n");
break;
case FB_TYPE_INTERLEAVED_PLANES:
printf("type: interleaved planes\n");
fprintf(stderr, "type: interleaved planes\n");
break;
case FB_TYPE_TEXT:
printf("type: text/attributes\n");
fprintf(stderr, "type: text/attributes\n");
break;
case FB_TYPE_VGA_PLANES:
printf("type: EGA/VGA planes\n");
fprintf(stderr, "type: EGA/VGA planes\n");
break;
default:
printf("type: undefined!\n");
fprintf(stderr, "type: undefined!\n");
break;
}
printf("line length: %i bytes (%i pixels)\n", fb_fixedinfo.line_length, fb_fixedinfo.line_length/(fb_varinfo_p->bits_per_pixel/8));
fprintf(stderr, "line length: %i bytes (%i pixels)\n", fb_fixedinfo.line_length, fb_fixedinfo.line_length/(fb_varinfo_p->bits_per_pixel/8));

printf("\nframe buffer variable info:\n");
printf("resolution: %ix%i\n", fb_varinfo_p->xres, fb_varinfo_p->yres);
printf("virtual resolution: %ix%i\n", fb_varinfo_p->xres_virtual, fb_varinfo_p->yres_virtual);
printf("offset: %ix%i\n", fb_varinfo_p->xoffset, fb_varinfo_p->yoffset);
printf("bits_per_pixel: %i\n", fb_varinfo_p->bits_per_pixel);
printf("grayscale: %s\n", fb_varinfo_p->grayscale ? "true" : "false");
printf("red: offset: %i, length: %i, msb_right: %i\n", fb_varinfo_p->red.offset, fb_varinfo_p->red.length, fb_varinfo_p->red.msb_right);
printf("blue: offset: %i, length: %i, msb_right: %i\n", fb_varinfo_p->blue.offset, fb_varinfo_p->green.length, fb_varinfo_p->green.msb_right);
printf("green: offset: %i, length: %i, msb_right: %i\n", fb_varinfo_p->green.offset, fb_varinfo_p->blue.length, fb_varinfo_p->blue.msb_right);
printf("alpha: offset: %i, length: %i, msb_right: %i\n", fb_varinfo_p->transp.offset, fb_varinfo_p->transp.length, fb_varinfo_p->transp.msb_right);
printf("pixel format: %s\n", fb_varinfo_p->nonstd == 0 ? "standard" : "non-standard");
fprintf(stderr, "\nframe buffer variable info:\n");
fprintf(stderr, "resolution: %ix%i\n", fb_varinfo_p->xres, fb_varinfo_p->yres);
fprintf(stderr, "virtual resolution: %ix%i\n", fb_varinfo_p->xres_virtual, fb_varinfo_p->yres_virtual);
fprintf(stderr, "offset: %ix%i\n", fb_varinfo_p->xoffset, fb_varinfo_p->yoffset);
fprintf(stderr, "bits_per_pixel: %i\n", fb_varinfo_p->bits_per_pixel);
fprintf(stderr, "grayscale: %s\n", fb_varinfo_p->grayscale ? "true" : "false");
fprintf(stderr, "red: offset: %i, length: %i, msb_right: %i\n", fb_varinfo_p->red.offset, fb_varinfo_p->red.length, fb_varinfo_p->red.msb_right);
fprintf(stderr, "blue: offset: %i, length: %i, msb_right: %i\n", fb_varinfo_p->blue.offset, fb_varinfo_p->green.length, fb_varinfo_p->green.msb_right);
fprintf(stderr, "green: offset: %i, length: %i, msb_right: %i\n", fb_varinfo_p->green.offset, fb_varinfo_p->blue.length, fb_varinfo_p->blue.msb_right);
fprintf(stderr, "alpha: offset: %i, length: %i, msb_right: %i\n", fb_varinfo_p->transp.offset, fb_varinfo_p->transp.length, fb_varinfo_p->transp.msb_right);
fprintf(stderr, "pixel format: %s\n", fb_varinfo_p->nonstd == 0 ? "standard" : "non-standard");
}
Blue = fb_varinfo_p->blue.offset >> 3;
Green = fb_varinfo_p->green.offset >> 3;
Expand All @@ -182,7 +182,7 @@ static void read_framebuffer(char *device, size_t bytes, unsigned char *buf_p)

if(-1 == (fd=open(device, O_RDONLY)))
{
fprintf (stderr, "Error: Couldn't open %s.\n", device);
fprintf(stderr, "Error: Couldn't open %s.\n", device);
exit(EXIT_FAILURE);
}

Expand Down Expand Up @@ -277,16 +277,22 @@ static void write_PNG(unsigned char *outbuffer, char *filename,
png_bytep row_pointers[height];
png_structp png_ptr;
png_infop info_ptr;
FILE *outfile = fopen(filename, "wb");
FILE *outfile;

if (strcmp(filename, "-") == 0)
outfile = stdout;
else {
outfile = fopen(filename, "wb");
if (!outfile)
{
fprintf(stderr, "Error: Couldn't fopen %s.\n", filename);
exit(EXIT_FAILURE);
}
}

for (i=0; i<height; i++)
row_pointers[i] = outbuffer + i * 4 * width;

if (!outfile)
{
fprintf (stderr, "Error: Couldn't fopen %s.\n", filename);
exit(EXIT_FAILURE);
}

png_ptr = png_create_write_struct(PNG_LIBPNG_VER_STRING,
(png_voidp) NULL, (png_error_ptr) NULL, (png_error_ptr) NULL);
Expand Down Expand Up @@ -316,7 +322,7 @@ static void write_PNG(unsigned char *outbuffer, char *filename,

png_write_info(png_ptr, info_ptr);

printf ("Now writing PNG file\n");
fprintf(stderr, "Now writing PNG file\n");

png_write_image(png_ptr, row_pointers);

Expand All @@ -341,7 +347,7 @@ static void convert_and_write(unsigned char *inbuffer, char *filename,

memset(outbuffer, 0, bufsize);

printf ("Converting image from %i\n", bits);
fprintf(stderr, "Converting image from %i\n", bits);

switch(bits)
{
Expand Down Expand Up @@ -455,7 +461,7 @@ int main(int argc, char **argv)
{
if (UNDEFINED == bitdepth || UNDEFINED == width || UNDEFINED == height)
{
printf("Width, height and bitdepth are mandatory when reading from file\n");
fprintf(stderr, "Width, height and bitdepth are mandatory when reading from file\n");
exit(EXIT_FAILURE);
}
}
Expand All @@ -481,7 +487,7 @@ int main(int argc, char **argv)
if (UNDEFINED == height)
height = (int) fb_varinfo.yres;

printf("Resolution: %ix%i depth %i\n", width, height, bitdepth);
fprintf(stderr, "Resolution: %ix%i depth %i\n", width, height, bitdepth);

strncpy(infile, device, MAX_LEN - 1);
}
Expand Down

0 comments on commit 89a39b9

Please sign in to comment.