PERFORCE change 214917 for review
Brooks Davis
brooks at FreeBSD.org
Wed Jul 25 19:25:24 UTC 2012
http://p4web.freebsd.org/@@214917?ac=10
Change 214917 by brooks at brooks_ecr_current on 2012/07/25 19:24:28
Add fbdumppng and fbloadpng commands which do what you would expect.
Also add an fbfill command that fills the screen with a specified
color.
Affected files ...
.. //depot/projects/ctsrd/beribsd/src/ctsrd/mtlctl/mtlctl.c#3 edit
Differences ...
==== //depot/projects/ctsrd/beribsd/src/ctsrd/mtlctl/mtlctl.c#3 (text+ko) ====
@@ -45,6 +45,7 @@
#include <fnmatch.h>
#include <libutil.h>
#include <magic.h>
+#include <png.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
@@ -83,7 +84,10 @@
printf("usage: \n");
printf(" mtlclt fbdump <file>\n");
+ printf(" mtlclt fbdumppng <file>\n");
+ printf(" mtlclt fbfill <RRGGBB>\n");
printf(" mtlclt fbloaddump <file>\n");
+ printf(" mtlclt fbloadpng <file>\n");
printf(" mtlclt gesture\n");
printf(" mtlclt gestures\n");
printf(" mtlclt vstripes <width>\n");
@@ -169,19 +173,20 @@
main(int argc, char *argv[] __unused)
{
int b, fd, i, j;
- u_int32_t *image;
+ u_int32_t *image, pixel;
if (argc < 2)
usage();
fb_init();
+ image = malloc(fb_width * fb_height * 4);
+ if (image == NULL)
+ err(1, "malloc");
+
if (strcmp(argv[1], "fbdump") == 0) {
if (argc != 3)
usage();
- image = malloc(fb_width * fb_height * 4);
- if (image == NULL)
- err(1, "malloc");
if ((fd = open(argv[2], O_WRONLY|O_CREAT|O_TRUNC)) == -1)
err(1, "open");
fb_save(image);
@@ -190,12 +195,74 @@
if (b == -1)
err(1, "write");
}
+ } else if (strcmp(argv[1], "fbdumppng") == 0) {
+ FILE * fp;
+ png_structp png_ptr;
+ png_infop info_ptr;
+ png_byte **row_pointers;
+ int pixel_size = 3;
+ int depth = 8;
+
+ if (argc != 3)
+ usage();
+ if ((fd = open(argv[2], O_WRONLY|O_CREAT|O_TRUNC)) == -1)
+ err(1, "open");
+ if ((fp = fdopen(fd, "w")) == NULL)
+ err(1, "fdopen");
+
+ png_ptr = png_create_write_struct(PNG_LIBPNG_VER_STRING,
+ NULL, NULL, NULL);
+ if (png_ptr == NULL)
+ errx(1, "png_create_write_struct");
+ info_ptr = png_create_info_struct(png_ptr);
+ if (info_ptr == NULL)
+ errx(1, "png_create_info_struct");
+ if (setjmp(png_jmpbuf(png_ptr)) != 0)
+ errx(1, "png error");
+ png_set_IHDR (png_ptr, info_ptr, fb_width, fb_height, depth,
+ PNG_COLOR_TYPE_RGB, PNG_INTERLACE_NONE,
+ PNG_COMPRESSION_TYPE_DEFAULT, PNG_FILTER_TYPE_DEFAULT);
+ row_pointers = png_malloc(png_ptr,
+ fb_height * sizeof(png_byte *));
+
+ fb_save(image);
+ for (j = 0; j < fb_height; j++) {
+ row_pointers[j] = png_malloc(png_ptr,
+ sizeof(uint8_t) * fb_width * pixel_size);
+ for (i = 0; i < fb_width; i++) {
+ pixel = image[i + j * fb_width];
+ row_pointers[j][i * 3] = (pixel >> 8) & 0xFF;
+ row_pointers[j][i * 3 + 1] = (pixel >> 16) &
+ 0xFF;
+ row_pointers[j][i * 3 + 2] = (pixel >> 24) &
+ 0xFF;
+ }
+ }
+
+ png_init_io (png_ptr, fp);
+ png_set_rows (png_ptr, info_ptr, row_pointers);
+ png_write_png (png_ptr, info_ptr, PNG_TRANSFORM_IDENTITY, NULL);
+ for (j = 0; j < fb_height; j++)
+ png_free (png_ptr, row_pointers[j]);
+ png_free (png_ptr, row_pointers);
+ fclose(fp);
+ } else if (strcmp(argv[1], "fbfill") == 0) {
+ char *endptr;
+ long color;
+
+ if (argc != 3)
+ usage();
+ color = strtol(argv[2], &endptr, 16);
+ if (*endptr != '\0' || color < 0 || color > 0xFFFFFF) {
+ warnx("invalid color %s", argv[2]);
+ usage();
+ }
+ pixel = fb_colour((color >> 16) & 0xFF, (color >> 16) & 0xFF,
+ color & 0xFF);
+ fb_fill(pixel);
} else if (strcmp(argv[1], "fbloaddump") == 0) {
if (argc != 3)
usage();
- image = malloc(fb_width * fb_height * 4);
- if (image == NULL)
- err(1, "malloc image buf");
if ((fd = open(argv[2], O_RDONLY)) == -1)
err(1, "open");
for (i = 0; i < fb_width * fb_height * 4; i += b) {
@@ -204,6 +271,16 @@
err(1, "read");
}
fb_post(image);
+ close(fd);
+ } else if (strcmp(argv[1], "fbloadpng") == 0) {
+ if (argc != 3)
+ usage();
+ if ((fd = open(argv[2], O_RDONLY)) == -1)
+ err(1, "open");
+ if (read_png_fd(fd, image, fb_width, fb_height) == -1)
+ err(1, "read_png_fd");
+ fb_post(image);
+ close(fd);
} else if (strcmp(argv[1], "gesture") == 0) {
print_gesture();
} else if (strcmp(argv[1], "gestures") == 0) {
@@ -220,9 +297,6 @@
usage();
}
- image = malloc(fb_width * fb_height * 4);
- if (image == NULL)
- err(1, "malloc image buf");
for (i = 0; i < fb_width; i++)
for (j = 0; j < fb_height; j++)
image[i + j * fb_width] =
@@ -233,5 +307,7 @@
} else
usage();
+ free(image);
+
return (0);
}
More information about the p4-projects
mailing list