+/*******************************************************************************
+ *
+ * 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;
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;
//Flip the backbuffer to the primary
SDL_Flip(screen);
+ // free alloced resources
+ SDL_FreeSurface(screen);
+ free(buf);
+
//Return success!
return 0;
}
+/*******************************************************************************
+ *
+ * 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__ */
#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 */
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);
}
// 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)) {
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)) {
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)
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) ||
usleep(500*1000);
}
- if (do_show)
+/* if (do_show)
show();
- else
+ else */
rozkuk();
return 0;