changeset 8:66c77f9ba9b9

convert rgba -> rgb
author Peter Meerwald <pmeerw@pmeerw.net>
date Sat, 14 May 2011 21:05:59 +0200
parents 5c00b239e1c8
children c7af696b6221
files common.h fbt.c jpg.c
diffstat 3 files changed, 25 insertions(+), 8 deletions(-) [+]
line wrap: on
line diff
--- a/common.h	Tue May 10 19:22:13 2011 +0200
+++ b/common.h	Sat May 14 21:05:59 2011 +0200
@@ -6,6 +6,6 @@
   unsigned char *ptr;
 } jpg_buf_t;
 
-jpg_buf_t build_jpg_from_fb(int fb, int width, int height);
+jpg_buf_t build_jpg_from_fb(unsigned char *fb_mem, int width, int height, int bits_per_pixel);
 
 #endif
--- a/fbt.c	Tue May 10 19:22:13 2011 +0200
+++ b/fbt.c	Sat May 14 21:05:59 2011 +0200
@@ -3,6 +3,7 @@
 #include <fcntl.h>
 #include <unistd.h>
 #include <sys/ioctl.h>
+#include <sys/mman.h>
 
 #include "linux/fb.h"
 
@@ -32,6 +33,7 @@
   printf("visual %d\n", sif.visual);
   printf("accel %d\n", sif.accel);
   printf("line length %d\n", sif.line_length);
+  printf("mem %d\n", sif.smem_len);
 
   struct fb_var_screeninfo siv;
   if (ioctl(fd, FBIOGET_VSCREENINFO, &siv) < 0) {
@@ -42,11 +44,19 @@
   printf("res x %d y %d\n", siv.xres, siv.yres);
   printf("bpp %d\n", siv.bits_per_pixel);
 
+  size_t fb_mem_size = siv.xres * siv.yres * siv.bits_per_pixel / 8;
+  void *fb_mem = mmap(NULL, fb_mem_size, PROT_READ | PROT_WRITE, MAP_SHARED, fd, 0);
+  if (fb_mem == MAP_FAILED) {
+    perror("");
+    exit(EXIT_FAILURE);
+  }
+
   printf("-------------\n");
   fflush(stdout);
 
-  jpg_buf_t jpg_buf = build_jpg_from_fb(fd, siv.xres, siv.yres);
+  jpg_buf_t jpg_buf = build_jpg_from_fb((unsigned char *)fb_mem, siv.xres, siv.yres, siv.bits_per_pixel);
 
+  munmap(fb_mem, fb_mem_size);
   close(fd);
 
   printf("jpg %ld\n", jpg_buf.size);
--- a/jpg.c	Tue May 10 19:22:13 2011 +0200
+++ b/jpg.c	Sat May 14 21:05:59 2011 +0200
@@ -92,9 +92,17 @@
   dest->pub.free_in_buffer = dest->bufsize = *outsize;
 }
 
-
+static void convert_rgba_to_rgb(unsigned char *buf, unsigned char *fb_mem, int width) {
+  int i;
+  for (i = 0; i < width; i++) {
+    *buf++ = *fb_mem++;
+    *buf++ = *fb_mem++;
+    *buf++ = *fb_mem++;
+    fb_mem++;
+  }
+}
 
-jpg_buf_t build_jpg_from_fb(int fb, int width, int height) {
+jpg_buf_t build_jpg_from_fb(unsigned char *fb_mem, int width, int height, int bits_per_pixel) {
   int scanline;
   struct jpeg_compress_struct cinfo;
   struct jpeg_error_mgr jerr;
@@ -116,11 +124,10 @@
 
   jpeg_start_compress(&cinfo, TRUE);
 
-  JSAMPLE *buf = malloc(width * 4);
-  JSAMPROW row_ptr[1];
-  row_ptr[0] = buf;
+  JSAMPLE *buf = (JSAMPLE *) malloc(width * 3);
+  JSAMPROW row_ptr[1] = {buf};
   for (scanline = 0; scanline < height; scanline++) {
-    read(fb, buf, width*4);
+    convert_rgba_to_rgb(buf, &fb_mem[scanline * width * 4], width);
     jpeg_write_scanlines(&cinfo, row_ptr, 1);
   }
   free(buf);

Repositories maintained by Peter Meerwald, pmeerw@pmeerw.net.