changeset 3:e8957085fe8a

working
author Peter Meerwald <pmeerw@pmeerw.net>
date Sun, 08 May 2011 23:08:45 +0200
parents bac8ed8d6eb9
children 5ff6a67e1421
files Makefile fbt.c jpg.c
diffstat 3 files changed, 52 insertions(+), 14 deletions(-) [+]
line wrap: on
line diff
--- a/Makefile	Sun May 08 18:22:22 2011 +0200
+++ b/Makefile	Sun May 08 23:08:45 2011 +0200
@@ -3,8 +3,8 @@
 minimon: minimon.c jpg.c
 	$(CC) $(CFLAGS) -Wall -g -o $@ minimon.c jpg.c fb.c -lusb -ljpeg
 
-fbt: fbt.c
-	$(CC) $(CFLAGS) -Wall -g -o $@ fbt.c 
+fbt: fbt.c jpg.c
+	$(CC) $(CFLAGS) -O0 -Wall -g -o $@ fbt.c jpg.c -ljpeg
 	
 clean:
 	rm -f minimon
--- a/fbt.c	Sun May 08 18:22:22 2011 +0200
+++ b/fbt.c	Sun May 08 23:08:45 2011 +0200
@@ -1,13 +1,21 @@
 #include <stdlib.h>
 #include <stdio.h>
 #include <fcntl.h>
+#include <unistd.h>
 #include <sys/ioctl.h>
 
 #include "linux/fb.h"
 
+#include "common.h"
+
 int main(int argc, char *argv[]) {
 
-  int fd = open("/dev/fb0", O_RDWR);
+  if (argc != 2) {
+    fprintf(stderr, "usage: fbt /dev/fbX\n");
+    exit(EXIT_FAILURE);
+  }
+
+  int fd = open(argv[1], O_RDWR);
   if (fd < 0) {
     perror("");
     exit(EXIT_FAILURE);
@@ -34,5 +42,26 @@
   printf("res x %d y %d\n", siv.xres, siv.yres);
   printf("bpp %d\n", siv.bits_per_pixel);
 
+  printf("-------------\n");
+  fflush(stdout);
+
+  jpg_buf_t jpg_buf = build_jpg_from_fb(fd, siv.xres, siv.yres);
+
+  close(fd);
+
+  printf("jpg %ld\n", jpg_buf.size);
+
+  FILE *f = fopen("test.jpg", "wb");
+  if (!f) {
+	fprintf(stderr, "fopen failed\n");
+    exit(EXIT_FAILURE);
+  }
+
+  fwrite(jpg_buf.ptr, 1, jpg_buf.size, f);
+
+  fclose(f);
+
+  free(jpg_buf.ptr);
+
   exit(EXIT_SUCCESS);
 }
--- a/jpg.c	Sun May 08 18:22:22 2011 +0200
+++ b/jpg.c	Sun May 08 23:08:45 2011 +0200
@@ -1,9 +1,12 @@
 #include <stdlib.h>
 #include <stdio.h>
+#include <unistd.h>
 #include <string.h>
 #include "jpeglib.h"
 #include "jerror.h"
 
+#include "common.h"
+
 typedef struct {
   struct jpeg_destination_mgr pub; /* public fields */
 
@@ -16,10 +19,10 @@
 
 typedef mem_dest_mgr * mem_dest_mgr_ptr;
 
-void init_mem_destination (j_compress_ptr cinfo) {
+static void init_mem_destination (j_compress_ptr cinfo) {
 }
 
-boolean empty_mem_output_buffer(j_compress_ptr cinfo) {
+static boolean empty_mem_output_buffer(j_compress_ptr cinfo) {
   size_t nextsize;
   JOCTET * nextbuffer;
   mem_dest_mgr_ptr dest = (mem_dest_mgr_ptr) cinfo->dest;
@@ -47,14 +50,14 @@
   return TRUE;
 }
 
-void term_mem_destination(j_compress_ptr cinfo) {
+static void term_mem_destination(j_compress_ptr cinfo) {
   mem_dest_mgr_ptr dest = (mem_dest_mgr_ptr) cinfo->dest;
 
   *dest->outbuffer = dest->buffer;
   *dest->outsize = dest->bufsize - dest->pub.free_in_buffer;
 }
 
-void jpeg_mem_dest(j_compress_ptr cinfo, unsigned char ** outbuffer, unsigned long * outsize) {
+static void jpeg_mem_dest(j_compress_ptr cinfo, unsigned char ** outbuffer, unsigned long * outsize) {
   mem_dest_mgr_ptr dest;
 
   if (outbuffer == NULL || outsize == NULL)	/* sanity check */
@@ -90,7 +93,8 @@
 }
 
 
-int build_jpg(JSAMPLE *image_buf, int width, int height) {
+
+jpg_buf_t build_jpg_from_fb(int fb, int width, int height) {
   int scanline;
   struct jpeg_compress_struct cinfo;
   struct jpeg_error_mgr jerr;
@@ -98,9 +102,11 @@
   cinfo.err = jpeg_std_error(&jerr);
   jpeg_create_compress(&cinfo);
 
-  unsigned long out_size = 1<<20;
-  unsigned char *out_buf = NULL;
-  jpeg_mem_dest(&cinfo, &out_buf, &out_size);
+  jpg_buf_t jpg_buf;
+  jpg_buf.size = 1 << 20;
+  jpg_buf.ptr = NULL;
+
+  jpeg_mem_dest(&cinfo, &jpg_buf.ptr, &jpg_buf.size);
 
   cinfo.image_width = width;
   cinfo.image_height = height;
@@ -110,15 +116,18 @@
 
   jpeg_start_compress(&cinfo, TRUE);
 
+  JSAMPLE *buf = malloc(width * 4);
+  JSAMPROW row_ptr[1];
+  row_ptr[0] = buf;
   for (scanline = 0; scanline < height; scanline++) {
-    JSAMPROW *row_ptr;
-    row_ptr = &image_buf[scanline * width];
+    read(fb, buf, width*4);
     jpeg_write_scanlines(&cinfo, row_ptr, 1);
   }
+  free(buf);
 
   jpeg_finish_compress(&cinfo);
 
   jpeg_destroy_compress(&cinfo);
 
-  return 0;
+  return jpg_buf;
 }

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