]> rtime.felk.cvut.cz Git - eurobot/public.git/commitdiff
Project clarification, display function generalisation, display acceleration.
authorroot <root@petr.(none)>
Wed, 24 Mar 2010 19:27:08 +0000 (20:27 +0100)
committerroot <root@petr.(none)>
Wed, 24 Mar 2010 19:27:08 +0000 (20:27 +0100)
src/camera/rozkuk/display.c
src/camera/rozkuk/display.h
src/camera/rozkuk/rozkuk.c

index ac0b4cf0458304c10df9184f154982848cb65d0f..7da88a505f48a0626c20efd61fb98a12c4bf13c8 100644 (file)
@@ -1,38 +1,67 @@
+/*******************************************************************************
+ * 
+ * SDL based minimalistic GUI for rozkuk
+ *
+ * Based on SDL_DisplayBitmap example
+ *   (http://gpwiki.org/index.php/SDL:Tutorials:Displaying_a_Bitmap).
+ *
+ * Petr Kubizňák (kubiznak.petr@gmail.com), 2010
+ * 
+ ******************************************************************************/
+
 #include <stdlib.h>
 #include <SDL/SDL.h>
 #include <SDL/SDL_image.h>
 #include <SDL/SDL_rwops.h>
 
+/******************************************************************************/
 
-SDL_Surface *image = NULL;     //This pointer will reference our bitmap sprite
+SDL_Surface *image = NULL;                                                                                             //Bitmap sprite
 
-int display(uint8_t *bitmap, int size) {
-       SDL_Surface *screen;    //This pointer will reference the backbuffer
-       SDL_Surface *temp;      //This pointer will temporarily reference our bitmap sprite
-       SDL_Rect src, dest;     //These rectangles will describe the source and destination regions of our blit
-       
+/******************************************************************************/
+
+/** Initializes SDL, call before calling display(...). */
+SDL_Surface *initSDL(int width, int height) {
        //We must first initialize the SDL video component, and check for success
        if (SDL_Init(SDL_INIT_VIDEO) != 0) {
                printf("Unable to initialize SDL: %s\n", SDL_GetError());
-               return 1;
+               return NULL;
        }
-       
-       //When this program exits, SDL_Quit must be called (registered only once)
+
+       //When this program exits, SDL_Quit must be called (register only once)
        if(!image) atexit(SDL_Quit);
        
-       //Set the video mode to fullscreen 640x480 with 16bit colour and double-buffering
-       screen = SDL_SetVideoMode(640, 480, 16, SDL_DOUBLEBUF);
+       //Set the video mode to specified size with 16bit colour and double-buffering
+       return SDL_SetVideoMode(width, height, 16, SDL_DOUBLEBUF);
+}
+
+/******************************************************************************/
+
+/** Displays bitmap on screen on specified position. */
+int display(uint8_t *bitmap, int size, SDL_Surface *screen, int x, int y, int w, int h) {
+       SDL_Surface *temp;                                                                                                                              //temporary reference to the bitmap
+       SDL_Rect src, dest;                                                                                                                             //source and destination regions of our blit
+       
        if (screen == NULL) {
                printf("Unable to set video mode: %s\n", SDL_GetError());
                return 1;
        }
-       
-       char buf[640*480*3+100] = "P6\n640 480\n255\n";
-       memcpy(buf+15, bitmap, size);
+
+       // creates a SDL required structure out of the bitmap
+       char header[100];
+       sprintf(header, "P6\n%d %d\n255\n", w, h);
+       char *buf = (char *)malloc(size + strlen(header) + 1);
+       memcpy(buf, header, strlen(header));
+       memcpy(buf+strlen(header), bitmap, size);
+       // create a RWop loader hung on the created structure
        SDL_RWops *rwop = SDL_RWFromMem(buf, size+15);
-       if(!rwop) { printf("rwop==NULL !!!\n"); return 1; }
+       if(!rwop) {
+               printf("Unable to load picture\n");
+               return 1;
+       }
+       
+       // load picture from memory (just created "file" structure)
        temp = IMG_Load_RW(rwop, 1);
-       //Load the bitmap into a temporary surface, and check for success
        if (temp == NULL) {
                printf("Unable to load bitmap: %s\n", SDL_GetError());
                return 1;
@@ -54,8 +83,8 @@ int display(uint8_t *bitmap, int size) {
        src.h = image->h;       //Use image->h to display the entire height of the image
        
        //Construct the destination rectangle for our blit
-//     dest.x = 100;           //Display the image at the (X,Y) coordinates (100,100)
-//     dest.y = 100;
+       dest.x = x;             //Display the image at the (X,Y) coordinates (100,100)
+       dest.y = y;
        dest.w = image->w;      //Ensure the destination is large enough for the image's entire width/height
        dest.h = image->h;
        
@@ -65,6 +94,10 @@ int display(uint8_t *bitmap, int size) {
        //Flip the backbuffer to the primary
        SDL_Flip(screen);
        
+       // free alloced resources
+       SDL_FreeSurface(screen);
+       free(buf);
+       
        //Return success!
        return 0;
 }
index 4fee3a59325bbda9de75aabad571cefe5fb777e6..1a082c3c216ce28543665608cef9f46ac5069488 100644 (file)
@@ -1,7 +1,27 @@
+/*******************************************************************************
+ * 
+ * SDL based minimalistic GUI for rozkuk
+ *
+ * Based on SDL_DisplayBitmap example
+ *   (http://gpwiki.org/index.php/SDL:Tutorials:Displaying_a_Bitmap).
+ *
+ * Petr Kubizňák (kubiznak.petr@gmail.com), 2010
+ * 
+ ******************************************************************************/
+
 #ifndef __DISPLAY__H__
 #define __DISPLAY__H__
 
-int display(uint8_t *bitmap, int size);
+#include <SDL/SDL.h>
+
+/******************************************************************************/
+/** Initializes SDL, call before calling display(...). */
+SDL_Surface *initSDL(int width, int height);
+
+/** Displays bitmap on screen on specified position. */
+int display(uint8_t *bitmap, int size, SDL_Surface *screen, int x, int y, int w, int h);
+
+/******************************************************************************/
 
 #endif /* __DISPLAY__H__ */
 
index c89e756126c9f959cbfdc775a4626e48245da347..418849b7caf4927569204fe812e84f226cc61027 100644 (file)
 #define N_LOTS                 10
 #define DEFAUL_THR_QUANTILE   0.9
 
+// camera resolution
+#define CAM_WIDTH              640
+#define CAM_HEIGHT     480
+#define CAM_SIZE               (CAM_WIDTH * CAM_HEIGHT)
+
+
 #define THR_SIZE (IMG_WIDTH*IMG_HEIGHT)
 #define RGB_SIZE (2*THR_SIZE*3)
 /* I don't (want to) know actual size of JPEGs from camera */
@@ -77,12 +83,17 @@ int recognize_lot(pdfmask_t *pdf) {
 void rozkuk() {
   int lot;
   int i;
-       SDL_Event event;
-
+       SDL_Event event;                                                        // event handler
+       SDL_Surface *scr;                                                       // screen buffer
+       
 //  sem_init(&running_sem, 0, 0);
+       if(!(scr = initSDL(2*CAM_WIDTH, 2*CAM_HEIGHT))) {
+               perror("Cannot initialize SDL.");
+               return;
+       }
 
   for (;;) {
-    while (camera_on("/dev/video0", 640, 480, V4L2_PIX_FMT_YUYV)) {
+    while (camera_on("/dev/video0", CAM_WIDTH, CAM_HEIGHT, V4L2_PIX_FMT_YUYV)) {
       perror("camera_on");
       usleep(500*1000);
     }
@@ -94,17 +105,20 @@ void rozkuk() {
 //      camera_get_frame(process_jpeg_frame, 1000 /* msec */);
 
 
-                 camera_get_frame(process_jpeg_frame, 0);
-      sub_colors(rgb, thr, 320*480, game_color);
-      img_threshold(thr, bitmap, 320*480, thr_quantile);
+                       
+                       camera_get_frame(process_jpeg_frame, 0);                        // loads jpeg from camera; process_jpeg_frame transforms it to bitmap and stores to "rgb"
+                 //threshold(rgb
+                 
+//      sub_colors(rgb, thr, 320*480, game_color);
+//      img_threshold(thr, bitmap, 320*480, thr_quantile);
 //      lot = recognize_lot((game_color == 0) ? pdf_green : pdf_red);
 //      orte_send(lot);
 //      printf("(%s) %2d\n", game_color?"RED":"GREEN", 1+lot);
                        
-                       display(rgb, RGB_SIZE);
+                       display(rgb, RGB_SIZE, scr, 0, 0, CAM_WIDTH, CAM_HEIGHT);
 
                        //Wait for 250ms (0.25 seconds) so we can see the image
-                       SDL_Delay(250);
+                       //SDL_Delay(250);
                        
                        // catch keyboard (and other) events
                        while(SDL_PollEvent(&event)) {
@@ -123,13 +137,12 @@ void rozkuk() {
 
     camera_off();
 return;
-    while (!running)
-      sem_wait(&running_sem);
+//    while (!running) sem_wait(&running_sem);
   }
 }
 
 /* debug: grab/process image */
-void show() {
+/*void show() {
   int i, j, k, tmp, lot;
 
   while (camera_on("/dev/video0", 640, 480, V4L2_PIX_FMT_YUYV)) {
@@ -160,11 +173,11 @@ void show() {
 
   lot = recognize_lot((game_color == 0) ? pdf_green : pdf_red);
   fprintf(stderr, "%2d\n", 1+lot);
-}
+}*/
 
 int main(int argc, char *argv[]) {
   FILE *f;
-  int opt, do_show;
+  int opt, do_show=0;
   float tmp;
 
   while ((opt = getopt(argc, argv, "c:s:rp")) != -1)
@@ -190,7 +203,7 @@ int main(int argc, char *argv[]) {
       thr_quantile = tmp;
     fprintf(stderr, "thr quantile: %f\n", thr_quantile);
   }
-  do_show = show_rgb || show_proc;
+//  do_show = show_rgb || show_proc;
 
 /*  if (!show_rgb) {
       while (((f = fopen("pdfmask.bin", "rb")) == NULL) ||
@@ -207,9 +220,9 @@ int main(int argc, char *argv[]) {
     usleep(500*1000);
   }
 
-  if (do_show)
+/*  if (do_show)
     show();
-  else
+  else */
     rozkuk();
 
   return 0;