JS_EXPORT_API void dock_draw_window_preview(JSValueRef canvas, double xid, double dest_width, double dest_height) { GdkWindow* win = gdk_x11_window_foreign_new_for_display(gdk_display_get_default(), (long)xid); if (win == NULL) { return; } if (JSValueIsNull(get_global_context(), canvas)) { g_debug("draw_window_preview with null canvas!"); return; } cairo_t* cr = fetch_cairo_from_html_canvas(get_global_context(), canvas); if (cr == NULL) { return; } cairo_save(cr); //clear preview content to prevent translucency window problem cairo_set_operator(cr, CAIRO_OPERATOR_CLEAR); cairo_paint(cr); cairo_restore(cr); int width = gdk_window_get_width(win); int height = gdk_window_get_height(win); cairo_save(cr); double scale = 0; if (width > height) { scale = dest_width/width; cairo_scale(cr, scale, scale); gdk_cairo_set_source_window(cr, win, 0, 0.5*(dest_height/scale-height)); } else { scale = dest_height/height; cairo_scale(cr, scale, scale); gdk_cairo_set_source_window(cr, win, 0.5*(dest_width/scale-width), 0); } cairo_paint(cr); cairo_restore(cr); canvas_custom_draw_did(cr, NULL); }
JS_EXPORT_API void dominantcolor_draw1(JSValueRef canvas, const char* path, double size, JSData* data) { if (_mask && _board) { cairo_t* cr = fetch_cairo_from_html_canvas(data->ctx, canvas); GdkPixbuf* pixbuf = gdk_pixbuf_new_from_file_at_size(path, size, size, NULL); if (pixbuf) { double r, g, b; calc_dominant_color_by_path(path, &r, &g, &b, clamp1); cairo_save(cr); cairo_scale(cr, _board_scale, _board_scale); draw_board(cr, _board, _mask, r, g, b); cairo_restore(cr); gdk_cairo_set_source_pixbuf(cr, pixbuf, 4, 4); cairo_paint(cr); g_object_unref(pixbuf); } canvas_custom_draw_did(cr, NULL); } }
void _draw(JSValueRef canvas, double dest_width, double dest_height) { static gboolean not_draw = FALSE; if (recognition_info.reco_state == RECOGNIZING) { /* g_debug("[%s] recognizing", __func__); */ return; } if (!recognition_info.has_data) { g_debug("[%s] get no data from camera", __func__); return; } if (JSValueIsNull(get_global_context(), canvas)) { g_debug("[%s] draw with null canvas!", __func__); return; } if (recognition_info.source_data == NULL) { g_debug("[%s] source_data is null", __func__); return; } g_debug("[%s]", __func__); cairo_t* cr = fetch_cairo_from_html_canvas(get_global_context(), canvas); g_assert(cr != NULL); cairo_save(cr); gulong len = recognition_info.length; guchar* data = g_slice_copy(len, recognition_info.source_data); GdkPixbuf* pixbuf = gdk_pixbuf_new_from_data(data, GDK_COLORSPACE_RGB, // color space FALSE, // has alpha 8, // bits per sample CAMERA_WIDTH, // width CAMERA_HEIGHT, // height 3*CAMERA_WIDTH, // row stride destroy_pixels, // destroy function GINT_TO_POINTER(len) // destroy function data ); double scale = 0; if (CAMERA_WIDTH > CAMERA_HEIGHT) { scale = dest_height/CAMERA_HEIGHT; cairo_scale(cr, scale, scale); gdk_cairo_set_source_pixbuf(cr, pixbuf, 0.5 * (dest_width / scale - CAMERA_WIDTH), 0); } else { scale = dest_width/CAMERA_WIDTH; cairo_scale(cr, scale, scale); gdk_cairo_set_source_pixbuf(cr, pixbuf, 0, 0.5 * (dest_height / scale - CAMERA_HEIGHT)); } cairo_paint(cr); cairo_restore(cr); canvas_custom_draw_did(cr, NULL); g_object_unref(pixbuf); recognition_info.has_data = FALSE; }