static int save_lua( lua_State *L ) { img_t *img = (img_t*)luaL_checkudata( L, 1, MODULE_MT ); const char *path = luaL_checkstring( L, 2 ); ImlibLoadError err = IMLIB_LOAD_ERROR_NONE; Imlib_Image work = imlib_create_image_using_data( img->size.w, img->size.h, img->blob ); // set current image imlib_context_set_image( work ); work = imlib_create_cropped_scaled_image( 0, 0, img->size.w, img->size.h, img->resize.w, img->resize.h ); imlib_free_image_and_decache(); imlib_context_set_image( work ); save2path( img, path, &err ); // failed if( err ){ liberr2errno( err ); lua_pushstring( L, strerror(errno) ); return 2; } // success else { lua_pushnil( L ); } return 1; }
ExcCode screen_cursor_blend(int x, int y, Imlib_Image image) { xcb_xfixes_get_cursor_image_cookie_t cookie = xcb_xfixes_get_cursor_image(display); xcb_xfixes_get_cursor_image_reply_t *reply = xcb_xfixes_get_cursor_image_reply(display, cookie, NULL); if (reply == NULL) return 0; unsigned *cursor_data = xcb_xfixes_get_cursor_image_cursor_image(reply); if (cursor_data == NULL) return 0; Imlib_Image cursor = imlib_create_image_using_data( reply->width, reply->height, cursor_data); if (cursor == NULL) PANIC(ERR_IMAGE); imlib_context_set_image(cursor); imlib_image_set_has_alpha(1); imlib_context_set_image(image); imlib_blend_image_onto_image(cursor, 0, 0, 0, reply->width, reply->height, reply->x - reply->xhot - x, reply->y - reply->yhot - y, reply->width, reply->height); imlib_context_set_image(cursor); imlib_free_image_and_decache(); free(reply); return 0; }
void gib_imlib_render_image_part_on_drawable_at_size_with_rotation(Drawable d, Imlib_Image im, int sx, int sy, int sw, int sh, int dx, int dy, int dw, int dh, double angle, char dither, char blend, char alias) { Imlib_Image new_im; imlib_context_set_image(im); imlib_context_set_drawable(d); imlib_context_set_anti_alias(alias); imlib_context_set_dither(dither); imlib_context_set_angle(angle); imlib_context_set_blend(blend); new_im = imlib_create_rotated_image(angle); imlib_context_set_image(new_im); imlib_render_image_part_on_drawable_at_size(sx, sy, sw, sh, dx, dy, dw, dh); imlib_free_image_and_decache(); }
void remove_icon(TrayWindow *traywin) { if (systray_profile) fprintf(stderr, "[%f] %s:%d win = %lu (%s)\n", profiling_get_time(), __FUNCTION__, __LINE__, traywin->win, traywin->name); Panel *panel = systray.area.panel; // remove from our list systray.list_icons = g_slist_remove(systray.list_icons, traywin); fprintf(stderr, YELLOW "remove_icon: %lu (%s)" RESET "\n", traywin->win, traywin->name); XSelectInput(server.display, traywin->win, NoEventMask); if (traywin->damage) XDamageDestroy(server.display, traywin->damage); // reparent to root XSync(server.display, False); error = FALSE; XErrorHandler old = XSetErrorHandler(window_error_handler); XUnmapWindow(server.display, traywin->win); XReparentWindow(server.display, traywin->win, server.root_win, 0, 0); XDestroyWindow(server.display, traywin->parent); XSync(server.display, False); XSetErrorHandler(old); stop_timeout(traywin->render_timeout); stop_timeout(traywin->resize_timeout); free(traywin->name); if (traywin->image) { imlib_context_set_image(traywin->image); imlib_free_image_and_decache(); } g_free(traywin); // check empty systray int count = 0; GSList *l; for (l = systray.list_icons; l; l = l->next) { count++; } if (count == 0) hide(&systray.area); // Resize and redraw the systray if (systray_profile) fprintf(stderr, BLUE "[%f] %s:%d trigger resize & redraw" RESET "\n", profiling_get_time(), __FUNCTION__, __LINE__); systray.area.resize_needed = TRUE; panel->area.resize_needed = TRUE; schedule_redraw(&systray.area); refresh_systray = TRUE; }
static inline void save2path( img_t *img, const char *path, ImlibLoadError *err ) { // set quality imlib_image_attach_data_value( "quality", NULL, img->quality, NULL ); imlib_image_set_format( img->format ); imlib_save_image_with_error_return( path, err ); imlib_free_image_and_decache(); }
int main(int argc, char** argv) { struct stat sb; if (!(stat(OUTPUT_DIR, &sb) == 0 && S_ISDIR(sb.st_mode))) { if (mkdir(OUTPUT_DIR, S_IRWXU | S_IRGRP | S_IROTH | S_IXGRP | S_IXOTH) != 0) { printf("Error creating directory %s\n",OUTPUT_DIR); return -1; } } char tempFileName[512]; sprintf(tempFileName, "%stest.txt",OUTPUT_DIR); FILE* tempFile = fopen(tempFileName, "w+"); if (tempFile == NULL) { printf("Failed to create file in current folder. Please check permissions.\n"); return -1; } fclose(tempFile); remove(tempFileName); //if we get here, we know the directory exists and we can write to it //now set up, start, and grab image from camera fc2Context context; fc2PGRGuid guid; fc2Image raw_image, converted_image; Imlib_Image temp_image; check_point_grey(fc2CreateContext(&context)); check_point_grey(fc2GetCameraFromIndex(context,0,&guid)); check_point_grey(fc2Connect(context,&guid)); check_point_grey(fc2SetDefaultColorProcessing(FC2_IPP)); check_point_grey(fc2SetVideoModeAndFrameRate(context, FC2_VIDEOMODE_1280x960Y8, FC2_FRAMERATE_15)); PrintCameraInfo(context); check_point_grey(fc2CreateImage(&raw_image)); check_point_grey(fc2CreateImage(&converted_image)); check_point_grey(fc2StartCapture(context)); check_point_grey(fc2RetrieveBuffer(context,&raw_image)); check_point_grey(fc2ConvertImageTo(FC2_PIXEL_FORMAT_BGRU,&raw_image,&converted_image)); temp_image = imlib_create_image_using_copied_data(converted_image.cols, converted_image.rows, (unsigned int *) converted_image.pData); imlib_context_set_image(temp_image); char filename[512]; sprintf(filename,"%stest.ppm",OUTPUT_DIR); imlib_save_image(filename); printf("Saved %s\n",filename); //image saved, now clean up imlib_free_image_and_decache(); check_point_grey(fc2StopCapture(context)); check_point_grey(fc2DestroyContext(context)); check_point_grey(fc2DestroyImage(&raw_image)); check_point_grey(fc2DestroyImage(&converted_image)); printf("finished cleanup\n"); return 0; }
/* free images before exit */ void cleanup(void) { int i; for(i = 0; i < nimage; i++) { imlib_context_set_image(images[i]); imlib_free_image_and_decache(); } }
void SaveImlibImage(Imlib_Image temp_image, char *name, char *mode_str) { imlib_context_set_image(temp_image); char filename[512]; sprintf(filename,"%snewtest_%sfinal.ppm",OUTPUT_DIR,name); imlib_save_image(filename); printf("Saved %s\n",filename); //image saved, now clean up imlib_free_image_and_decache(); }
static void im2_image_remove(struct sequ_image *img) { if(!img) return; imlib_context_set_image((Imlib_Image)img->privdata); imlib_free_image_and_decache(); free(img); return; }
static int save_trim_lua( lua_State *L ) { img_t *img = (img_t*)luaL_checkudata( L, 1, MODULE_MT ); const char *path = luaL_checkstring( L, 2 ); img_bounds_t bounds = (img_bounds_t){ 0, 0, 0, 0 }; double aspect_org = 0; double aspect = 0; ImlibLoadError err = IMLIB_LOAD_ERROR_NONE; Imlib_Image work = NULL; // calculate bounds of image with maintaining aspect ratio. aspect_org = (double)img->size.w/(double)img->size.h; aspect = (double)img->resize.w/(double)img->resize.h; // based on width if( aspect_org > aspect ){ bounds.w = img->resize.w; bounds.h = (int)((double)bounds.w / aspect_org); } // based on height else if( aspect_org < aspect ){ bounds.h = img->resize.h; bounds.w = (int)((double)bounds.h * aspect_org); } // square else { bounds.w = img->resize.w; bounds.h = img->resize.h; } // create image work = imlib_create_image_using_data( img->size.w, img->size.h, img->blob ); // set current image imlib_context_set_image( work ); work = imlib_create_cropped_scaled_image( 0, 0, img->size.w, img->size.h, bounds.w, bounds.h ); imlib_free_image_and_decache(); imlib_context_set_image( work ); save2path( img, path, &err ); // failed if( err ){ liberr2errno( err ); lua_pushstring( L, strerror(errno) ); } // success else { lua_pushnil( L ); } return 1; }
static int img_load( img_t *img, const char *path ) { ImlibLoadError err = IMLIB_LOAD_ERROR_NONE; Imlib_Image imimg = imlib_load_image_with_error_return( path, &err ); if( img ) { imlib_context_set_image( imimg ); img->size.w = imlib_image_get_width(); img->size.h = imlib_image_get_height(); // allocate buffer img->bytes = sizeof( DATA32 ) * (size_t)img->size.w * (size_t)img->size.h; img->blob = malloc( img->bytes ); if( img->blob ) { char *format = imlib_image_format(); if( img_format_copy( img, format, strlen( format ) ) == 0 ){ memcpy( img->blob, imlib_image_get_data_for_reading_only(), img->bytes ); imlib_free_image_and_decache(); img->quality = 100; img->resize = (img_size_t){ 0, 0 }; return 0; } // failed to copy free( img->blob ); } imlib_free_image_and_decache(); } else { liberr2errno( err ); } return -1; }
static void render_checkerboard() { /* create the checkerboard texture, if not already done. */ static Pixmap checks_pmap = None; Imlib_Image checks = NULL; if (checks_pmap == None) { checks = imlib_create_image(16, 16); if (!checks) { fputs("Unable to create a teeny weeny imlib image. I detect problems\n", stderr); } imlib_context_set_image(checks); imlib_context_set_color(144, 144, 144, 255); imlib_image_fill_rectangle(0, 0, 16, 16); imlib_context_set_color(100, 100, 100, 255); imlib_image_fill_rectangle(0, 0, 8, 8); imlib_image_fill_rectangle(8, 8, 8, 8); checks_pmap = XCreatePixmap(x11.display, x11_window, 16, 16, x11.depth); imlib_context_set_drawable(checks_pmap); imlib_render_image_on_drawable(0, 0); imlib_free_image_and_decache(); } /* and plot the checkerboards onto the pixmap */ static GC gc = None; XGCValues gcval; if (gc == None) { gcval.tile = checks_pmap; gcval.fill_style = FillTiled; gc = XCreateGC(x11.display, x11_window, GCTile | GCFillStyle, &gcval); } XFillRectangle(x11.display, window_pixmap, gc, 0, 0, s_view.win_width, s_view.win_height); }
static struct sequ_image *im2_resize(struct sequ_image *old,int w, int h) { if(old != NULL) { if(w == old->width && h == old->height) return old; imlib_context_set_image((Imlib_Image)old->privdata); imlib_free_image_and_decache(); } else old=malloc(sizeof(sequ_image)); old->privdata=imlib_create_image(w,h); old->lib=&im2_lib; img_from_im2(old,(Imlib_Image)old->privdata); return old; }
void img_close(img_t *img, bool decache) { int i; if (img == NULL) return; if (img->multi.cnt > 0) { for (i = 0; i < img->multi.cnt; i++) { imlib_context_set_image(img->multi.frames[i].im); imlib_free_image(); } img->multi.cnt = 0; img->im = NULL; } else if (img->im != NULL) { imlib_context_set_image(img->im); if (decache) imlib_free_image_and_decache(); else imlib_free_image(); img->im = NULL; } }
void gib_imlib_render_image_on_drawable_at_size_with_rotation(Drawable d, Imlib_Image im, int x, int y, int w, int h, double angle, char dither, char blend, char alias) { Imlib_Image new_im; imlib_context_set_image(im); imlib_context_set_drawable(d); imlib_context_set_anti_alias(alias); imlib_context_set_dither(dither); imlib_context_set_blend(blend); imlib_context_set_angle(angle); new_im = imlib_create_rotated_image(angle); imlib_context_set_image(new_im); imlib_render_image_on_drawable_at_size(x, y, w, h); imlib_free_image_and_decache(); }
/* draw background to root */ void drawbg(void) { int i, w, h, nx, ny, nh, nw, tmp; double factor; Pixmap pm; Imlib_Image tmpimg, buffer; pm = XCreatePixmap(dpy, root, sw, sh, DefaultDepth(dpy, DefaultScreen(dpy))); if(!(buffer = imlib_create_image(sw, sh))) die("Error: Cannot allocate buffer.\n"); imlib_context_set_image(buffer); imlib_image_fill_rectangle(0, 0, sw, sh); imlib_context_set_blend(1); for(i = 0; i < nmonitor; i++) { imlib_context_set_image(images[i % nimage]); w = imlib_image_get_width(); h = imlib_image_get_height(); if(!(tmpimg = imlib_clone_image())) die("Error: Cannot clone image.\n"); imlib_context_set_image(tmpimg); if(rotate && ((monitors[i].w > monitors[i].h && w < h) || (monitors[i].w < monitors[i].h && w > h))) { imlib_image_orientate(1); tmp = w; w = h; h = tmp; } imlib_context_set_image(buffer); switch(mode) { case ModeCenter: nw = (monitors[i].w - w) / 2; nh = (monitors[i].h - h) / 2; nx = monitors[i].x + (monitors[i].w - nw) / 2; ny = monitors[i].y + (monitors[i].h - nh) / 2; break; case ModeZoom: nw = monitors[i].w; nh = monitors[i].h; if(w > h && (w / h > (monitors[i].w / monitors[i].h))) { nx = monitors[i].x + (monitors[i].w - nw) / 2; ny = monitors[i].y + (int)ceil(h * nx / w) / 2; } else { ny = monitors[i].y + (monitors[i].h - nh) / 2; nx = monitors[i].x + (int)ceil(w * ny / h) / 2; } break; default: /* ModeScale */ factor = MAX((double)w / monitors[i].w, (double)h / monitors[i].h); nw = w / factor; nh = h / factor; nx = monitors[i].x + (monitors[i].w - nw) / 2; ny = monitors[i].y + (monitors[i].h - nh) / 2; } imlib_blend_image_onto_image(tmpimg, 0, 0, 0, w, h, nx, ny, nw, nh); imlib_context_set_image(tmpimg); imlib_free_image(); } imlib_context_set_blend(0); imlib_context_set_image(buffer); imlib_context_set_drawable(root); imlib_render_image_on_drawable(0, 0); imlib_context_set_drawable(pm); imlib_render_image_on_drawable(0, 0); XSetWindowBackgroundPixmap(dpy, root, pm); imlib_context_set_image(buffer); imlib_free_image_and_decache(); XFreePixmap(dpy, pm); }
void* cdr_viewer_grab(void* args) { struct CamGrab_t* my_args = (struct CamGrab_t*)args; int new_fd; //new connection on new_fd //char recvbuf[k_timestamp_len * 3]; XEvent event; Display *display = XOpenDisplay(0); /* magic to get GUI to run periodically */ event.type = KeyPress; event.xkey.keycode = 9; /* ESC */ event.xkey.state = 0; /* no Mod1Mask */ while(!cdr_viewer_threads_should_die) { // first connect back to seykhl new_fd = ClientConnect(k_Server, my_args->PortNumber); if (new_fd == -1) { // connection not made, so sleep and keep trying cdr_viewer_active = FALSE; usleep(10000); continue; } else //connection made { //cdr_viewer_active = TRUE; printf("Connected to %s on port %s\n", k_Server, my_args->PortNumber); } while(!cdr_viewer_threads_should_die && (new_fd != -1)) { //here's where we do the magic PointGrey_t2* PG = new PointGrey_t2; PG->new_fd = new_fd; Imlib_Image temp_img; int working; while (1) { cdr_viewer_active = TRUE; if (cdr_OpenCV_ReceiveFrame(PG) != 0) { close(PG->new_fd); cdr_viewer_active = FALSE; break; } //convert opencv to imlib for display in viewer temp_img = Convert_OpenCV_to_Imlib(PG); pthread_mutex_lock(&my_args->MostRecentLock); working = ((my_args->MostRecent) + 1) % k_ImgBufSize; pthread_mutex_unlock(&my_args->MostRecentLock); pthread_mutex_lock(&my_args->ImgArrayLock[working]); if (my_args->Set[working] == TRUE) { //clean up memory allocation imlib_context_set_image(*my_args->ImgArray[working]); imlib_free_image_and_decache(); } else { my_args->Set[working] = TRUE; } *my_args->ImgArray[working] = temp_img; pthread_mutex_unlock(&my_args->ImgArrayLock[working]); pthread_mutex_lock(&my_args->MostRecentLock); my_args->MostRecent = working; pthread_mutex_unlock(&my_args->MostRecentLock); XSendEvent(display, cdr_display_pane, FALSE, 0, &event); XFlush(display); } delete PG; printf("cdr_viewer: exiting from loop while(!cdr_viewer_threads_should_die && (new_fd != -1))\n"); break; } printf("closing new_fd\n"); close(new_fd); // accept loop doesn't need this } //do cleanup here printf("exiting cdr_viewer_grab\n"); return NULL; }
int main(int argc, char **argv) { Imlib_Image *im = NULL; char *file = NULL; int no = 1; const char *display_name = getenv("DISPLAY"); if (argc < 2) return 1; file = argv[no]; if (display_name == NULL) display_name = ":0"; disp = XOpenDisplay(display_name); if (disp == NULL) { fprintf(stderr, "Can't open display %s\n", display_name); return 1; } vis = DefaultVisual(disp, DefaultScreen(disp)); depth = DefaultDepth(disp, DefaultScreen(disp)); cm = DefaultColormap(disp, DefaultScreen(disp)); win = XCreateSimpleWindow(disp, DefaultRootWindow(disp), 0, 0, 10, 10, 0, 0, 0); XSelectInput(disp, win, ButtonPressMask | ButtonReleaseMask | ButtonMotionMask | PointerMotionMask); imlib_context_set_display(disp); imlib_context_set_visual(vis); imlib_context_set_colormap(cm); imlib_context_set_progress_function(progress); imlib_context_set_progress_granularity(10); imlib_context_set_drawable(win); im = imlib_load_image(file); while (!im) { no++; if (no == argc) { fprintf(stderr, "Image format not available\n"); exit(0); } file = argv[no]; image_width = 0; im = imlib_load_image(file); imlib_context_set_image(im); } if (!im) { fprintf(stderr, "Image format not available\n"); exit(0); } for (;;) { int x, y, b, count, fdsize, xfd, timeout = 0; XEvent ev; static int zoom_mode = 0, zx, zy; static double zoom = 1.0; struct timeval tval; fd_set fdset; double t1; XFlush(disp); XNextEvent(disp, &ev); switch (ev.type) { case ButtonPress: b = ev.xbutton.button; x = ev.xbutton.x; y = ev.xbutton.y; if (b == 3) { zoom_mode = 1; zx = x; zy = y; imlib_context_set_drawable(pm); imlib_context_set_image(bg_im); imlib_context_set_anti_alias(0); imlib_context_set_dither(0); imlib_context_set_blend(0); imlib_render_image_part_on_drawable_at_size (0, 0, image_width, image_height, 0, 0, image_width, image_height); XSetWindowBackgroundPixmap(disp, win, pm); XClearWindow(disp, win); } break; case ButtonRelease: b = ev.xbutton.button; x = ev.xbutton.x; y = ev.xbutton.y; if (b == 3) zoom_mode = 0; if (b == 1) { no++; if (no == argc) no = argc - 1; file = argv[no]; image_width = 0; zoom = 1.0; zoom_mode = 0; imlib_context_set_image(im); imlib_free_image_and_decache(); im = imlib_load_image(file); while (!im) { no++; if (no == argc) exit(0); file = argv[no]; image_width = 0; im = imlib_load_image(file); } imlib_context_set_image(im); } if (b == 2) { no--; if (no == 0) no = 1; file = argv[no]; image_width = 0; zoom = 1.0; zoom_mode = 0; imlib_context_set_image(im); imlib_free_image_and_decache(); im = imlib_load_image(file); while (!im) { no--; if (no == 0) no = 1; file = argv[no]; image_width = 0; im = imlib_load_image(file); } imlib_context_set_image(im); } break; case MotionNotify: while (XCheckTypedWindowEvent(disp, win, MotionNotify, &ev)); x = ev.xmotion.x; y = ev.xmotion.y; if (zoom_mode) { int sx, sy, sw, sh, dx, dy, dw, dh; zoom = ((double)x - (double)zx) / 32.0; if (zoom < 0) zoom = 1.0 + ((zoom * 32.0) / ((double)(zx + 1))); else zoom += 1.0; if (zoom <= 0.0001) zoom = 0.0001; if (zoom > 1.0) { dx = 0; dy = 0; dw = image_width; dh = image_height; sx = zx - (zx / zoom); sy = zy - (zy / zoom); sw = image_width / zoom; sh = image_height / zoom; } else { dx = zx - (zx * zoom); dy = zy - (zy * zoom); dw = image_width * zoom; dh = image_height * zoom; sx = 0; sy = 0; sw = image_width; sh = image_height; } imlib_context_set_anti_alias(0); imlib_context_set_dither(0); imlib_context_set_blend(0); imlib_context_set_image(bg_im); imlib_render_image_part_on_drawable_at_size (sx, sy, sw, sh, dx, dy, dw, dh); XSetWindowBackgroundPixmap(disp, win, pm); XClearWindow(disp, win); XFlush(disp); timeout = 1; } default: break; } t1 = 0.2; tval.tv_sec = (long)t1; tval.tv_usec = (long)((t1 - ((double)tval.tv_sec)) * 1000000); xfd = ConnectionNumber(disp); fdsize = xfd + 1; FD_ZERO(&fdset); FD_SET(xfd, &fdset); if (timeout) count = select(fdsize, &fdset, NULL, NULL, &tval); else count = select(fdsize, &fdset, NULL, NULL, NULL); if (count < 0) { if ((errno == ENOMEM) || (errno == EINVAL) || (errno == EBADF)) exit(1); } else { if ((count == 0) && (timeout)) { int sx, sy, sw, sh, dx, dy, dw, dh; if (zoom > 1.0) { dx = 0; dy = 0; dw = image_width; dh = image_height; sx = zx - (zx / zoom); sy = zy - (zy / zoom); sw = image_width / zoom; sh = image_height / zoom; } else { dx = zx - (zx * zoom); dy = zy - (zy * zoom); dw = image_width * zoom; dh = image_height * zoom; sx = 0; sy = 0; sw = image_width; sh = image_height; } imlib_context_set_anti_alias(1); imlib_context_set_dither(1); imlib_context_set_blend(0); imlib_context_set_image(bg_im); imlib_render_image_part_on_drawable_at_size (sx, sy, sw, sh, dx, dy, dw, dh); XSetWindowBackgroundPixmap(disp, win, pm); XClearWindow(disp, win); XFlush(disp); timeout = 0; } } } return 0; }
void gib_imlib_free_image_and_decache(Imlib_Image im) { imlib_context_set_image(im); imlib_free_image_and_decache(); }
int main(int argc, char** argv){ int numPics = 100; fc2VideoMode mode1 = FC2_VIDEOMODE_640x480Y8; char *mode1_str = "FC2_VIDEOMODE_640x480Y8"; //fc2FrameRate rate1 = FC2_FRAMERATE_30; //char *rate1_str = "FC2_FRAMERATE_30"; fc2VideoMode mode2 = FC2_VIDEOMODE_1280x960Y8; char *mode2_str = "FC2_VIDEOMODE_1280x960Y8"; fc2FrameRate rate = FC2_FRAMERATE_15; char *rate_str = "FC2_FRAMERATE_15"; fc2VideoMode mode; char *mode_str; //fc2FrameRate rate; //char *rate_str; //int prog_mode; int camOffset = 0; PrintBuildInfo(); /* if (argc != 2) */ /* { */ /* printf("Error: Must chose mode\n"); */ /* printf("Usage: %s {1, 2, 3, 4, 5, 6} \n",argv[0]); */ /* printf("Modes: 1 = first camera at 1280x960Y8\n" */ /* " 2 = second camera at 1280x960Y8\n" */ /* " 3 = both cameras at 1280x960Y8\n" */ /* " 4 = first camera at 640x480Y8\n" */ /* " 5 = second camera at 640x480Y8\n" */ /* " 6 = both cameras at 640x480Y8\n"); */ /* return -1; */ /* } */ /* prog_mode = atoi(argv[1]); */ /* if ((prog_mode > 6) || (prog_mode < 1)) */ /* { */ /* printf("Must chose valid mode, 1 through 6\n"); */ /* } */ if (CheckSaving(OUTPUT_DIR) != 0) { printf("Cannot save to %s, please check permissions\n",OUTPUT_DIR); return -1; } //have correct number of arguments and can save fc2Context tempContext; check_point_grey(fc2CreateContext(&tempContext)); unsigned int numCams; check_point_grey(fc2GetNumOfCameras(tempContext, &numCams)); check_point_grey(fc2DestroyContext(tempContext)); if (numCams == 0) { //no cameras printf("No cameras found, exiting.\n"); return -1; } //if we get here, we know we have at least 1 camera connected /* if (prog_mode < 4) */ /* { */ /* mode = FC2_VIDEOMODE_1280x960Y8; */ /* mode_str = "FC2_VIDEOMODE_1280x960Y8"; */ /* rate = FC2_FRAMERATE_15; */ /* rate_str = "FC2_FRAMERATE_15"; */ /* } */ // printf("Using resolution %s and frame rate %s\n",mode_str,rate_str); /* if ((prog_mode == 1) || (prog_mode == 4)) */ /* { // run only the first camera */ /* numCams = 1; */ /* } */ /* if ((prog_mode == 2) || (prog_mode == 5)) */ /* { // run only the second camera */ /* camOffset = 1; */ /* } */ printf("Taking %i pictures per camera with %i camera(s).\n",numPics, (numCams - camOffset)); struct point_grey *pg_ptr[numCams - camOffset]; for (int i = 0; i < (numCams - camOffset); i++) //initialization loop { if (i == 0) { mode = mode2; mode_str = mode2_str; //rate = rate2; //rate_str = rate2_str; } else { mode = mode1; mode_str = mode1_str; //rate = rate1; //rate_str = rate1_str; } pg_ptr[i] = (struct point_grey *)point_grey_malloc(sizeof(struct point_grey)); check_point_grey(fc2CreateContext(&pg_ptr[i]->context)); check_point_grey(fc2GetCameraFromIndex(pg_ptr[i]->context, (i + camOffset), &pg_ptr[i]->guid)); check_point_grey(fc2Connect(pg_ptr[i]->context, &pg_ptr[i]->guid)); check_point_grey(fc2SetDefaultColorProcessing(FC2_NEAREST_NEIGHBOR_FAST)); check_point_grey(fc2SetVideoModeAndFrameRate(pg_ptr[i]->context, mode, rate)); AssignName(pg_ptr[i]->context, pg_ptr[i]->name); check_point_grey(fc2CreateImage(&pg_ptr[i]->raw_image)); check_point_grey(fc2CreateImage(&pg_ptr[i]->converted_image)); PrintCameraInfo(pg_ptr[i]->context); printf("Using resolution %s and frame rate %s\n",mode_str,rate_str); //**CALLING THIS HERE WILL WORK WITH 2 CAMERAS AT 640X480 BUT NOT AT //1280X960** check_point_grey(fc2StartCapture(pg_ptr[i]->context)) printf("completed initialization loop iteration %d, %s\n",i,pg_ptr[i]->name); } // initialization loop Imlib_Image temp_image; double start = current_time(); for (int j = 0; j < numPics; j++) //loop through numPics { for (int i = 0; i < (numCams - camOffset); i++) //picture taking loop { check_point_grey(fc2RetrieveBuffer(pg_ptr[i]->context, &pg_ptr[i]->raw_image)); check_point_grey(fc2ConvertImageTo(FC2_PIXEL_FORMAT_BGRU, &pg_ptr[i]->raw_image, &pg_ptr[i]->converted_image)); temp_image = imlib_create_image_using_copied_data(pg_ptr[i]->converted_image.cols, pg_ptr[i]->converted_image.rows, (unsigned int *) pg_ptr[i]->converted_image.pData); printf("%simage_%d\n",pg_ptr[i]->name,j); if (j == (numPics - 1)) {//save the final image from each camera SaveImlibImage(temp_image, pg_ptr[i]->name, mode_str); } else { imlib_context_set_image(temp_image); //this is where we would do something else with the image imlib_free_image_and_decache(); } } //picture taking loop } //numPics loop double stop = current_time(); //check elapsed time double elapsed = stop - start; double images_per_sec = (double)numPics / elapsed; printf("%d images per camera taken in %f seconds (%f images/sec/cam)\n", numPics, elapsed, images_per_sec); for (int i = 0; i < (numCams - camOffset); i++) //cleanup loop { //**CALLING THIS HERE WILL WORK WITH 2 CAMERAS AT 640X480 BUT NOT AT //1280X960** check_point_grey(fc2StopCapture(pg_ptr[i]->context)); check_point_grey(fc2DestroyContext(pg_ptr[i]->context)); check_point_grey(fc2DestroyImage(&pg_ptr[i]->raw_image)); check_point_grey(fc2DestroyImage(&pg_ptr[i]->converted_image)); free(pg_ptr[i]); printf("completed cleanup loop iteration %d\n",i); } //cleanup loop printf("Program complete!\n"); return 0; }
static int progress(Imlib_Image im, char percent, int update_x, int update_y, int update_w, int update_h) { /* first time it's called */ imlib_context_set_drawable(pm); imlib_context_set_anti_alias(0); imlib_context_set_dither(0); imlib_context_set_blend(0); if (image_width == 0) { int x, y, onoff; imlib_context_set_image(im); image_width = imlib_image_get_width(); image_height = imlib_image_get_height(); if (pm) XFreePixmap(disp, pm); pm = XCreatePixmap(disp, win, image_width, image_height, depth); imlib_context_set_drawable(pm); if (bg_im) { imlib_context_set_image(bg_im); imlib_free_image_and_decache(); } bg_im = imlib_create_image(image_width, image_height); imlib_context_set_image(bg_im); for (y = 0; y < image_height; y += 8) { onoff = (y / 8) & 0x1; for (x = 0; x < image_width; x += 8) { if (onoff) imlib_context_set_color(144, 144, 144, 255); else imlib_context_set_color(100, 100, 100, 255); imlib_image_fill_rectangle(x, y, 8, 8); onoff++; if (onoff == 2) onoff = 0; } } imlib_render_image_part_on_drawable_at_size(0, 0, image_width, image_height, 0, 0, image_width, image_height); XSetWindowBackgroundPixmap(disp, win, pm); XResizeWindow(disp, win, image_width, image_height); XMapWindow(disp, win); XSync(disp, False); } imlib_context_set_anti_alias(0); imlib_context_set_dither(0); imlib_context_set_blend(1); imlib_blend_image_onto_image(im, 0, update_x, update_y, update_w, update_h, update_x, update_y, update_w, update_h); imlib_context_set_blend(0); imlib_render_image_part_on_drawable_at_size(update_x, update_y, update_w, update_h, update_x, update_y, update_w, update_h); XSetWindowBackgroundPixmap(disp, win, pm); XClearArea(disp, win, update_x, update_y, update_w, update_h, False); XFlush(disp); return 1; }
static int save_crop_lua( lua_State *L ) { img_t *img = (img_t*)luaL_checkudata( L, 1, MODULE_MT ); const char *path = luaL_checkstring( L, 2 ); uint8_t align = IMG_ALIGN_NONE; uint8_t halign = IMG_ALIGN_CENTER; uint8_t valign = IMG_ALIGN_MIDDLE; img_bounds_t bounds = (img_bounds_t){ 0, 0, 0, 0 }; double aspect_org = 0; double aspect = 0; Imlib_Image work = NULL; ImlibLoadError err = IMLIB_LOAD_ERROR_NONE; // check alignment arguments // horizontal if( !lua_isnoneornil( L, 3 ) ) { halign = (uint8_t)luaL_checkint( L, 3 ); if( halign < IMG_ALIGN_LEFT || halign > IMG_ALIGN_RIGHT ){ return luaL_argerror( L, 3, "horizontal align must be LEFT, RIGHT or CENTER" ); } } // vertical if( !lua_isnoneornil( L, 4 ) ) { valign = (uint8_t)luaL_checkinteger( L, 4 ); if( valign < IMG_ALIGN_TOP || valign > IMG_ALIGN_BOTTOM ){ return luaL_argerror( L, 4, "vertical align must be TOP, BOTTOM or MIDDLE" ); } } // calculate bounds of cropped image by aspect ratio aspect_org = (double)img->size.w/(double)img->size.h; aspect = (double)img->resize.w/(double)img->resize.h; // based on height if( aspect_org > aspect ){ bounds.h = img->size.h; bounds.w = (int)((double)img->size.h * aspect); align = (uint8_t)halign; } // based on width else if( aspect_org < aspect ){ bounds.w = img->size.w; bounds.h = (int)((double)img->size.w / aspect); align = (uint8_t)valign; } // square else { bounds.w = img->size.w; bounds.h = img->size.h; } // calculate bounds position BOUNDS_ALIGN( bounds, align, img->size ); // create image work = imlib_create_image_using_data( img->size.w, img->size.h, img->blob ); imlib_context_set_image( work ); work = imlib_create_cropped_scaled_image( bounds.x, bounds.y, bounds.w, bounds.h, img->resize.w, img->resize.h ); imlib_free_image_and_decache(); imlib_context_set_image( work ); save2path( img, path, &err ); // failed if( err ){ liberr2errno( err ); lua_pushstring( L, strerror(errno) ); } // success else { lua_pushnil( L ); } return 1; }
void systray_render_icon_composited(void *t) { // we end up in this function only in real transparency mode or if systray_task_asb != 100 0 0 // we made also sure, that we always have a 32 bit visual, i.e. we can safely create 32 bit pixmaps here TrayWindow *traywin = t; if (systray_profile) fprintf(stderr, "[%f] %s:%d win = %lu (%s)\n", profiling_get_time(), __FUNCTION__, __LINE__, traywin->win, traywin->name); // wine tray icons update whenever mouse is over them, so we limit the updates to 50 ms struct timespec now; clock_gettime(CLOCK_MONOTONIC, &now); struct timespec earliest_render = add_msec_to_timespec(traywin->time_last_render, min_refresh_period); if (compare_timespecs(&earliest_render, &now) > 0) { traywin->num_fast_renders++; if (traywin->num_fast_renders > max_fast_refreshes) { traywin->render_timeout = add_timeout(min_refresh_period, 0, systray_render_icon_composited, traywin, &traywin->render_timeout); if (systray_profile) fprintf(stderr, YELLOW "[%f] %s:%d win = %lu (%s) delaying rendering" RESET "\n", profiling_get_time(), __FUNCTION__, __LINE__, traywin->win, traywin->name); return; } } else { traywin->time_last_render.tv_sec = now.tv_sec; traywin->time_last_render.tv_nsec = now.tv_nsec; traywin->num_fast_renders = 0; } if (traywin->width == 0 || traywin->height == 0) { // reschedule rendering since the geometry information has not yet been processed (can happen on slow cpu) traywin->render_timeout = add_timeout(min_refresh_period, 0, systray_render_icon_composited, traywin, &traywin->render_timeout); if (systray_profile) fprintf(stderr, YELLOW "[%f] %s:%d win = %lu (%s) delaying rendering" RESET "\n", profiling_get_time(), __FUNCTION__, __LINE__, traywin->win, traywin->name); return; } if (traywin->render_timeout) { stop_timeout(traywin->render_timeout); traywin->render_timeout = NULL; } // good systray icons support 32 bit depth, but some icons are still 24 bit. // We create a heuristic mask for these icons, i.e. we get the rgb value in the top left corner, and // mask out all pixel with the same rgb value // Very ugly hack, but somehow imlib2 is not able to get the image from the traywindow itself, // so we first render the tray window onto a pixmap, and then we tell imlib2 to use this pixmap as // drawable. If someone knows why it does not work with the traywindow itself, please tell me ;) Pixmap tmp_pmap = XCreatePixmap(server.display, traywin->win, traywin->width, traywin->height, 32); if (!tmp_pmap) { goto on_systray_error; } XRenderPictFormat *f; if (traywin->depth == 24) { f = XRenderFindStandardFormat(server.display, PictStandardRGB24); } else if (traywin->depth == 32) { f = XRenderFindStandardFormat(server.display, PictStandardARGB32); } else { fprintf(stderr, RED "Strange tray icon found with depth: %d" RESET "\n", traywin->depth); XFreePixmap(server.display, tmp_pmap); return; } XRenderPictFormat *f32 = XRenderFindVisualFormat(server.display, server.visual32); if (!f || !f32) { XFreePixmap(server.display, tmp_pmap); goto on_systray_error; } XSync(server.display, False); error = FALSE; XErrorHandler old = XSetErrorHandler(window_error_handler); // if (server.real_transparency) // Picture pict_image = XRenderCreatePicture(server.display, traywin->parent, f, 0, 0); // reverted Rev 407 because here it's breaking alls icon with systray + xcompmgr Picture pict_image = XRenderCreatePicture(server.display, traywin->win, f, 0, 0); if (!pict_image) { XFreePixmap(server.display, tmp_pmap); XSetErrorHandler(old); goto on_error; } Picture pict_drawable = XRenderCreatePicture(server.display, tmp_pmap, XRenderFindVisualFormat(server.display, server.visual32), 0, 0); if (!pict_drawable) { XRenderFreePicture(server.display, pict_image); XFreePixmap(server.display, tmp_pmap); XSetErrorHandler(old); goto on_error; } XRenderComposite(server.display, PictOpSrc, pict_image, None, pict_drawable, 0, 0, 0, 0, 0, 0, traywin->width, traywin->height); XRenderFreePicture(server.display, pict_image); XRenderFreePicture(server.display, pict_drawable); // end of the ugly hack and we can continue as before imlib_context_set_visual(server.visual32); imlib_context_set_colormap(server.colormap32); imlib_context_set_drawable(tmp_pmap); Imlib_Image image = imlib_create_image_from_drawable(0, 0, 0, traywin->width, traywin->height, 1); imlib_context_set_visual(server.visual); imlib_context_set_colormap(server.colormap); XFreePixmap(server.display, tmp_pmap); if (!image) { imlib_context_set_visual(server.visual); imlib_context_set_colormap(server.colormap); XSetErrorHandler(old); goto on_error; } else { if (traywin->image) { imlib_context_set_image(traywin->image); imlib_free_image_and_decache(); } traywin->image = image; } imlib_context_set_image(traywin->image); // if (traywin->depth == 24) // imlib_save_image("/home/thil77/test.jpg"); imlib_image_set_has_alpha(1); DATA32 *data = imlib_image_get_data(); if (traywin->depth == 24) { create_heuristic_mask(data, traywin->width, traywin->height); } if (systray.alpha != 100 || systray.brightness != 0 || systray.saturation != 0) adjust_asb(data, traywin->width, traywin->height, systray.alpha, (float)systray.saturation / 100, (float)systray.brightness / 100); imlib_image_put_back_data(data); systray_render_icon_from_image(traywin); if (traywin->damage) XDamageSubtract(server.display, traywin->damage, None, None); XSync(server.display, False); XSetErrorHandler(old); if (error) goto on_error; panel_refresh = TRUE; if (systray_profile) fprintf(stderr, "[%f] %s:%d win = %lu (%s)\n", profiling_get_time(), __FUNCTION__, __LINE__, traywin->win, traywin->name); return; on_error: fprintf(stderr, RED "systray %d: rendering error for icon %lu (%s) pid %d" RESET "\n", __LINE__, traywin->win, traywin->name, traywin->pid); return; on_systray_error: fprintf(stderr, RED "systray %d: rendering error for icon %lu (%s) pid %d. " "Disabling compositing and restarting systray..." RESET "\n", __LINE__, traywin->win, traywin->name, traywin->pid); systray_composited = 0; stop_net(); start_net(); return; }
// functions not called from Scheme void* rover_server_grab(void* args) { struct CamGrab_t* my_args = (struct CamGrab_t*)args; int sockfd, new_fd; // listen on sock_fd, new connection on new_fd int cdr_sockfd, cdr_new_fd; //for commander images int retval; char recvbuf[k_timestamp_len * 3]; XEvent event; Display *display = XOpenDisplay(0); /* magic to get GUI to run periodically */ event.type = KeyPress; event.xkey.keycode = 9; /* ESC */ event.xkey.state = 0; /* no Mod1Mask */ sockfd = StartServer(my_args->PortNumber); // cv::VideoWriter output_video; // cv::Size vid_size = cv::Size(640,480); //**HARDCODED** for 640x480 video // int codec = CV_FOURCC('M','J','P','G'); // output_video.open(my_args->video_file_name, codec, 12.0, vid_size, true); printf("Opened video file %s\n", my_args->video_file_name); printf("server: waiting for image connection on port %s...\n", my_args->PortNumber); cdr_sockfd = StartServer(my_args->CdrPortNumber); printf("server: waiting for commander image connection on port %s...\n", my_args->CdrPortNumber); while(!grab_threads_should_die) { // main accept() loop usleep(10000); new_fd = AcceptConnection(sockfd); /* AcceptConnection set to non-blocking, so will spin here until it either gets a valid new_fd (and then goes into while loop below) or grab_threads_should_die becomes TRUE, which will cause the while loop (and function) to exit. */ if (new_fd != -1) { //receive and log start message into log file memset(recvbuf, 0 , sizeof(recvbuf)); retval = recv(new_fd, &recvbuf, sizeof(recvbuf), 0); if (retval < 0) { //what error handling??? printf("rover_server_grab recv retval = %d\n", retval); } else { fprintf(my_args->file_ptr, "%s\n", recvbuf); memset(recvbuf, 0 , sizeof(recvbuf)); } //try to connect to commander viewer // cdr_sockfd = StartServer(my_args->CdrPortNumber); // printf("server: waiting for commander image connection on port %s...\n", // my_args->CdrPortNumber); cdr_new_fd = AcceptConnection(cdr_sockfd); if (cdr_new_fd != -1) { //connection successful cdr_viewer_active = TRUE; printf("Commander viewer CONNECTED on port %s\n", my_args->CdrPortNumber); } else { //connection failed cdr_viewer_active = FALSE; printf("Commander image connection failed for port %s\n", my_args->CdrPortNumber); } } while(!grab_threads_should_die && (new_fd != -1)) { //here's where we do the magic PointGrey_t2* PG = new PointGrey_t2; PG->new_fd = new_fd; PG->cdr_new_fd = cdr_new_fd; Imlib_Image temp_img; int working; while (1) { if (OpenCV_ReceiveFrame(PG, my_args->file_ptr) != 0) { close(PG->cdr_new_fd); cdr_viewer_active = FALSE; // printf("before close cdr_sockfd = %d\n", cdr_sockfd); // close(cdr_sockfd); // printf("after close cdr_sockfd = %d\n", cdr_sockfd); break; } //write frame to output video file my_args->output_video->write(PG->uncompressedImage); //send to commander viewer here?? //convert opencv to imlib for display in viewer temp_img = Convert_OpenCV_to_Imlib(PG); pthread_mutex_lock(&my_args->MostRecentLock); working = ((my_args->MostRecent) + 1) % k_ImgBufSize; pthread_mutex_unlock(&my_args->MostRecentLock); pthread_mutex_lock(&my_args->ImgArrayLock[working]); if (my_args->Set[working] == TRUE) { //clean up memory allocation imlib_context_set_image(*my_args->ImgArray[working]); imlib_free_image_and_decache(); } else { my_args->Set[working] = TRUE; } *my_args->ImgArray[working] = temp_img; pthread_mutex_unlock(&my_args->ImgArrayLock[working]); pthread_mutex_lock(&my_args->MostRecentLock); my_args->MostRecent = working; pthread_mutex_unlock(&my_args->MostRecentLock); XSendEvent(display, display_pane, FALSE, 0, &event); XFlush(display); } delete PG; printf("exiting from loop after AcceptConnection\n"); break; } if (new_fd != -1) {//receive and log stop message into log file //memset(recvbuf, 0 , sizeof(recvbuf)); retval = recv(new_fd, &recvbuf, sizeof(recvbuf), 0); if (retval <= 0) { //what error handling??? printf("rover_server_grab recv (end) retval = %d\n", retval); } else fprintf(my_args->file_ptr, "%s\n", recvbuf); } close(new_fd); // accept loop doesn't need this } //do cleanup here close(sockfd); printf("sockfd closed\n"); return NULL; }