static void setup_sample (FilterData *filter_data, gint sample_num) { g_autofree gchar *name = NULL; gint width; gint height; GError *error = NULL; g_assert (filter_data != NULL); name = g_strdup_printf ("%s/sample%i.jpg", TEST_DATA_DIR, sample_num); g_clear_object (&filter_data->src_pixbuf); filter_data->src_pixbuf = gdk_pixbuf_new_from_file (name, &error); g_assert (GDK_IS_PIXBUF (filter_data->src_pixbuf)); if (!gdk_pixbuf_get_has_alpha (filter_data->src_pixbuf)) filter_data->src_pixbuf = gdk_pixbuf_add_alpha (filter_data->src_pixbuf, FALSE, 0.0, 0.0, 0.0); gtk_image_set_from_pixbuf (GTK_IMAGE (filter_data->src_img), filter_data->src_pixbuf); width = gdk_pixbuf_get_width (filter_data->src_pixbuf); height = gdk_pixbuf_get_height (filter_data->src_pixbuf); g_clear_object (&filter_data->dst_pixbuf); filter_data->dst_pixbuf = gdk_pixbuf_new (GDK_COLORSPACE_RGB, TRUE, 8, width, height); filter_pixbuf (filter_data->src_pixbuf, filter_data->dst_pixbuf, filter_data->filter_func); gtk_image_set_from_pixbuf (GTK_IMAGE (filter_data->dst_img), filter_data->dst_pixbuf); }
GdkPixbuf* UIManager::getLocalPixbufWithMask(const std::string& fileName) { // Try to find a cached pixbuf before loading from disk PixBufMap::iterator i = _localPixBufsWithMask.find(fileName); if (i != _localPixBufsWithMask.end()) { return i->second; } // Not cached yet, load afresh std::string fullFileName(GlobalRegistry().get(RKEY_BITMAPS_PATH) + fileName); GdkPixbuf* rgb = gdk_pixbuf_new_from_file(fullFileName.c_str(), 0); if (rgb != NULL) { // File load successful, add alpha channel GdkPixbuf* rgba = gdk_pixbuf_add_alpha(rgb, TRUE, 255, 0, 255); gdk_pixbuf_unref(rgb); _localPixBufsWithMask.insert(PixBufMap::value_type(fileName, rgba)); // Avoid destruction of this pixbuf g_object_ref(rgba); return rgba; } else { // File load failed globalErrorStream() << "Couldn't load pixbuf " << fullFileName << std::endl; return NULL; } }
/** * Reduce the alpha value of the specified pixbuf by alpha / 255 */ GdkPixbuf *ui_pixbuf_scale_alpha ( GdkPixbuf *pixbuf, guint8 alpha ) { guchar *pixels; gint width, height, iii, jjj; if ( ! gdk_pixbuf_get_has_alpha ( pixbuf ) ) { GdkPixbuf *tmp = gdk_pixbuf_add_alpha(pixbuf,FALSE,0,0,0); g_object_unref(G_OBJECT(pixbuf)); pixbuf = tmp; if ( !pixbuf ) return NULL; } pixels = gdk_pixbuf_get_pixels(pixbuf); width = gdk_pixbuf_get_width(pixbuf); height = gdk_pixbuf_get_height(pixbuf); /* r,g,b,a,r,g,b,a.... */ for (iii = 0; iii < width; iii++) for (jjj = 0; jjj < height; jjj++) { pixels += 3; if ( *pixels != 0 ) *pixels = (guint8)(((guint16)*pixels * (guint16)alpha) / 255); pixels++; } return pixbuf; }
static GdkPixbuf * set_transparency (const GdkPixbuf *pixbuf, gdouble alpha_percent) { GdkPixbuf *target; guchar *data, *current; guint x, y, rowstride, height, width; g_return_val_if_fail (pixbuf != NULL, NULL); g_return_val_if_fail (GDK_IS_PIXBUF (pixbuf), NULL); /* Returns a copy of pixbuf with it's non-completely-transparent pixels to have an alpha level "alpha_percent" of their original value. */ target = gdk_pixbuf_add_alpha (pixbuf, FALSE, 0, 0, 0); if (alpha_percent == 1.0) return target; width = gdk_pixbuf_get_width (target); height = gdk_pixbuf_get_height (target); rowstride = gdk_pixbuf_get_rowstride (target); data = gdk_pixbuf_get_pixels (target); for (y = 0; y < height; y++) { for (x = 0; x < width; x++) { /* The "4" is the number of chars per pixel, in this case, RGBA, the 3 means "skip to the alpha" */ current = data + (y * rowstride) + (x * 4) + 3; *(current) = (guchar) (*(current) * alpha_percent); } } return target; }
static gboolean fb_button_enter (GtkImage *widget, GdkEventCrossing *event) { GdkPixbuf *dark, *light; int i; guint hicolor; guchar *src, *up, extra[3]; ENTER; if (gtk_image_get_storage_type(widget) != GTK_IMAGE_PIXBUF) RET(TRUE); light = g_object_get_data(G_OBJECT(widget), "light"); dark = gtk_image_get_pixbuf(widget); if (!light) { hicolor = (gint) g_object_get_data(G_OBJECT(widget), "hicolor"); light = gdk_pixbuf_add_alpha(dark, FALSE, 0, 0, 0); if (!light) RET(TRUE); src = gdk_pixbuf_get_pixels (light); for (i = 2; i >= 0; i--, hicolor >>= 8) extra[i] = hicolor & 0xFF; for (up = src + gdk_pixbuf_get_height(light) * gdk_pixbuf_get_rowstride (light); src < up; src+=4) { if (src[3] == 0) continue; for (i = 0; i < 3; i++) { if (src[i] + extra[i] >= 255) src[i] = 255; else src[i] += extra[i]; } } g_object_set_data_full (G_OBJECT(widget), "light", light, g_object_unref); }
static GdkPixbuf * matewnck_selector_dimm_icon (GdkPixbuf *pixbuf) { int x, y, pixel_stride, row_stride; guchar *row, *pixels; int w, h; GdkPixbuf *dimmed; w = gdk_pixbuf_get_width (pixbuf); h = gdk_pixbuf_get_height (pixbuf); if (gdk_pixbuf_get_has_alpha (pixbuf)) dimmed = gdk_pixbuf_copy (pixbuf); else dimmed = gdk_pixbuf_add_alpha (pixbuf, FALSE, 0, 0, 0); pixel_stride = 4; row = gdk_pixbuf_get_pixels (dimmed); row_stride = gdk_pixbuf_get_rowstride (dimmed); for (y = 0; y < h; y++) { pixels = row; for (x = 0; x < w; x++) { pixels[3] /= 2; pixels += pixel_stride; } row += row_stride; } return dimmed; }
void Pixbuf::_forceAlpha() { if (gdk_pixbuf_get_has_alpha(_pixbuf)) return; GdkPixbuf *old = _pixbuf; _pixbuf = gdk_pixbuf_add_alpha(old, FALSE, 0, 0, 0); g_object_unref(old); }
void gp_image_set_mask ( GpImage *image, GdkBitmap *mask ) { GdkPixbuf *pixbuf; GdkPixbuf *m_pixbuf; guchar *pixels, *m_pixels; guchar *p, *m_p; gint w, h; gint n_channels, rowstride; g_return_if_fail ( GP_IS_IMAGE (image) ); pixbuf = image->priv->pixbuf; if(!gdk_pixbuf_get_has_alpha ( pixbuf ) ) { /*add alpha*/ GdkPixbuf *tmp ; tmp = gdk_pixbuf_add_alpha(pixbuf, FALSE, 0, 0, 0); g_object_unref(pixbuf); pixbuf = tmp; } m_pixbuf = gdk_pixbuf_copy ( pixbuf ); gdk_pixbuf_get_from_drawable ( m_pixbuf, mask, gdk_drawable_get_colormap (mask), 0,0, 0,0, -1,-1); n_channels = gdk_pixbuf_get_n_channels ( pixbuf ); rowstride = gdk_pixbuf_get_rowstride ( pixbuf ); w = gdk_pixbuf_get_width ( pixbuf ); h = gdk_pixbuf_get_height ( pixbuf ); pixels = gdk_pixbuf_get_pixels ( pixbuf ); m_pixels = gdk_pixbuf_get_pixels ( m_pixbuf ); while (h--) { guint i = w; p = pixels; m_p = m_pixels; while (i--) { if(m_p[0] == 0) { p[0] = 0; p[1] = 0; p[2] = 0; p[3] = 0; } p += n_channels; m_p += n_channels; } pixels += rowstride; m_pixels += rowstride; } g_object_unref (m_pixbuf); }
static jobject dnd_target_get_image(JNIEnv *env) { GdkPixbuf *buf; GInputStream *stream; jobject result = NULL; GdkAtom targets[] = { TARGET_MIME_PNG_ATOM, TARGET_MIME_JPEG_ATOM, TARGET_MIME_TIFF_ATOM, TARGET_MIME_BMP_ATOM, 0}; GdkAtom *cur_target = targets; selection_data_ctx ctx; while(*cur_target != 0 && result == NULL) { if (dnd_target_receive_data(env, *cur_target, &ctx)) { stream = g_memory_input_stream_new_from_data(ctx.data, ctx.length * (ctx.format / 8), (GDestroyNotify)g_free); buf = gdk_pixbuf_new_from_stream(stream, NULL, NULL); if (buf) { int w; int h; int stride; guchar *data; jbyteArray data_array; jobject buffer; if (!gdk_pixbuf_get_has_alpha(buf)) { GdkPixbuf *tmp_buf = gdk_pixbuf_add_alpha(buf, FALSE, 0, 0, 0); g_object_unref(buf); buf = tmp_buf; } w = gdk_pixbuf_get_width(buf); h = gdk_pixbuf_get_height(buf); stride = gdk_pixbuf_get_rowstride(buf); data = gdk_pixbuf_get_pixels(buf); //Actually, we are converting RGBA to BGRA, but that's the same operation data = (guchar*) convert_BGRA_to_RGBA((int*) data, stride, h); data_array = env->NewByteArray(stride * h); env->SetByteArrayRegion(data_array, 0, stride*h, (jbyte*) data); buffer = env->CallStaticObjectMethod(jByteBufferCls, jByteBufferWrap, data_array); result = env->NewObject(jGtkPixelsCls, jGtkPixelsInit, w, h, buffer); g_object_unref(buf); g_free(data); // data from convert_BGRA_to_RGBA } g_object_unref(stream); } ++cur_target; } return result; }
/* gdk_cursor_new_from_pixmap is broken on Windows. this is a workaround using gdk_cursor_new_from_pixbuf. */ GdkCursor* fixed_gdk_cursor_new_from_pixmap(GdkPixmap *source, GdkPixmap *mask, const GdkColor *fg, const GdkColor *bg, gint x, gint y) { GdkPixmap *rgb_pixmap; GdkGC *gc; GdkPixbuf *rgb_pixbuf, *rgba_pixbuf; GdkCursor *cursor; int width, height; /* HACK! It seems impossible to work with RGBA pixmaps directly in GDK-Win32. Instead we pick some third color, different from fg and bg, and use that as the 'transparent color'. We do this using colors_too_similar (see above) because two colors could be unequal in GdkColor's 16-bit/sample, but equal in GdkPixbuf's 8-bit/sample. */ GdkColor candidates[3] = {{0,65535,0,0}, {0,0,65535,0}, {0,0,0,65535}}; GdkColor *trans = &candidates[0]; if (colors_too_similar(trans, fg) || colors_too_similar(trans, bg)) { trans = &candidates[1]; if (colors_too_similar(trans, fg) || colors_too_similar(trans, bg)) { trans = &candidates[2]; } } /* trans is now guaranteed to be unique from fg and bg */ /* create an empty pixmap to hold the cursor image */ gdk_drawable_get_size(source, &width, &height); rgb_pixmap = gdk_pixmap_new(NULL, width, height, 24); /* blit the bitmaps defining the cursor onto a transparent background */ gc = gdk_gc_new(rgb_pixmap); gdk_gc_set_fill(gc, GDK_SOLID); gdk_gc_set_rgb_fg_color(gc, trans); gdk_draw_rectangle(rgb_pixmap, gc, TRUE, 0, 0, width, height); gdk_gc_set_fill(gc, GDK_OPAQUE_STIPPLED); gdk_gc_set_stipple(gc, source); gdk_gc_set_clip_mask(gc, mask); gdk_gc_set_rgb_fg_color(gc, fg); gdk_gc_set_rgb_bg_color(gc, bg); gdk_draw_rectangle(rgb_pixmap, gc, TRUE, 0, 0, width, height); gdk_gc_unref(gc); /* create a cursor out of the created pixmap */ rgb_pixbuf = gdk_pixbuf_get_from_drawable( NULL, rgb_pixmap, gdk_colormap_get_system(), 0, 0, 0, 0, width, height); gdk_pixmap_unref(rgb_pixmap); rgba_pixbuf = gdk_pixbuf_add_alpha( rgb_pixbuf, TRUE, trans->red, trans->green, trans->blue); gdk_pixbuf_unref(rgb_pixbuf); cursor = gdk_cursor_new_from_pixbuf(gdk_display_get_default(), rgba_pixbuf, x, y); gdk_pixbuf_unref(rgba_pixbuf); return cursor; }
GdkPixbuf* pixbuf_new_from_file_with_mask( const char* filename ){ GdkPixbuf* rgb = gdk_pixbuf_new_from_file( filename, 0 ); if ( rgb == 0 ) { return 0; } else { GdkPixbuf* rgba = gdk_pixbuf_add_alpha( rgb, FALSE, 255, 0, 255 ); gdk_pixbuf_unref( rgb ); return rgba; } }
static void setup_default_icon (void) { GdkPixbuf *pixbuf; char *filename; GError *err; err = NULL; pixbuf = NULL; filename = demo_find_file ("gtk-logo-rgb.gif", &err); if (filename) { pixbuf = gdk_pixbuf_new_from_file (filename, &err); g_free (filename); } /* Ignoring this error (passing NULL instead of &err above) * would probably be reasonable for most apps. We're just * showing off. */ if (err) { GtkWidget *dialog; dialog = gtk_message_dialog_new (NULL, 0, GTK_MESSAGE_ERROR, GTK_BUTTONS_CLOSE, "Failed to read icon file: %s", err->message); g_error_free (err); g_signal_connect (dialog, "response", G_CALLBACK (gtk_widget_destroy), NULL); } if (pixbuf) { GList *list; GdkPixbuf *transparent; /* The gtk-logo-rgb icon has a white background, make it transparent */ transparent = gdk_pixbuf_add_alpha (pixbuf, TRUE, 0xff, 0xff, 0xff); list = NULL; list = g_list_append (list, transparent); gtk_window_set_default_icon_list (list); g_list_free (list); g_object_unref (pixbuf); g_object_unref (transparent); } }
static Image* LoadImageGDK (ArchiveFile& file) { // Allocate a new GdkPixBuf and create an alpha-channel with alpha=1.0 GdkPixbuf* rawPixbuf = gdk_pixbuf_new_from_file(file.getName().c_str(), NULL); // Only create an alpha channel if the other rawPixbuf could be loaded GdkPixbuf* img = (rawPixbuf != NULL) ? gdk_pixbuf_add_alpha(rawPixbuf, TRUE, 255, 0, 255) : NULL; if (img != NULL) { // Allocate a new image RGBAImage* image = new RGBAImage(gdk_pixbuf_get_width(img), gdk_pixbuf_get_height(img), false); // Initialise the source buffer pointers guchar* gdkStart = gdk_pixbuf_get_pixels(img); int rowstride = gdk_pixbuf_get_rowstride(img); int numChannels = gdk_pixbuf_get_n_channels(img); // Set the target buffer pointer to the first RGBAPixel RGBAPixel* targetPixel = image->pixels; // Now do an unelegant cycle over all the pixels and move them into the target for (unsigned int y = 0; y < image->height; y++) { for (unsigned int x = 0; x < image->width; x++) { guchar* gdkPixel = gdkStart + y * rowstride + x * numChannels; // Copy the values from the GdkPixel targetPixel->red = gdkPixel[0]; targetPixel->green = gdkPixel[1]; targetPixel->blue = gdkPixel[2]; targetPixel->alpha = gdkPixel[3]; if (targetPixel->alpha != 255) image->setHasAlpha(true); // Increase the pointer targetPixel++; } } // Free the GdkPixbufs from the memory g_object_unref(G_OBJECT(img)); g_object_unref(G_OBJECT(rawPixbuf)); return image; } else { g_warning("image could not get loaded: '%s'\n", file.getName().c_str()); } // No image could be loaded, return NULL return NULL; }
/* {EV_PIXEL_BUFFER_IMP}.set_gdkpixbuf */ void F1053_13760 (EIF_REFERENCE Current, EIF_POINTER arg1) { GTCX EIF_POINTER tp1; EIF_POINTER tp2; EIF_NATURAL_8 tu1_1; EIF_NATURAL_8 tu1_2; EIF_NATURAL_8 tu1_3; RTLD; RTLI(1); RTLR(0,Current); RTGC; { /* INLINED CODE (default_pointer) */ tp1 = (EIF_POINTER) 0; /* END INLINED CODE */ } if ((EIF_BOOLEAN)(*(EIF_POINTER *)(Current+ _PTROFF_4_2_0_0_0_0_) != tp1)) { tp1 = *(EIF_POINTER *)(Current+ _PTROFF_4_2_0_0_0_0_); g_object_unref((gpointer) tp1); } { /* INLINED CODE (default_pointer) */ tp1 = (EIF_POINTER) 0; /* END INLINED CODE */ } if ((EIF_BOOLEAN)(arg1 != tp1)) { if ((EIF_BOOLEAN) !(EIF_BOOLEAN) EIF_TEST(gdk_pixbuf_get_has_alpha((GdkPixbuf*) arg1))) { tu1_1 = (EIF_NATURAL_8) ((EIF_INTEGER_32) 0L); tu1_2 = (EIF_NATURAL_8) ((EIF_INTEGER_32) 0L); tu1_3 = (EIF_NATURAL_8) ((EIF_INTEGER_32) 0L); tp1 = (EIF_POINTER) gdk_pixbuf_add_alpha((GdkPixbuf*) arg1, (gboolean) (EIF_BOOLEAN) 0, (guchar) tu1_1, (guchar) tu1_2, (guchar) tu1_3); *(EIF_POINTER *)(Current+ _PTROFF_4_2_0_0_0_0_) = (EIF_POINTER) tp1; g_object_unref((gpointer) arg1); } else { *(EIF_POINTER *)(Current+ _PTROFF_4_2_0_0_0_0_) = (EIF_POINTER) arg1; } } else { { /* INLINED CODE (default_pointer) */ tp1 = (EIF_POINTER) 0; /* END INLINED CODE */ } tp2 = tp1; *(EIF_POINTER *)(Current+ _PTROFF_4_2_0_0_0_0_) = (EIF_POINTER) tp2; } RTLE; }
static void update_image(Osd * self) { GdkPixmap *shape; GdkGC *shape_gc; GdkPixbuf *shape_pixbuf; GdkPixbuf *pixbuf; // make shape pixmap from pango layout shape = gdk_pixmap_new(GTK_WIDGET(self)->window, self->width, self->height, -1); shape_gc = gdk_gc_new(shape); gdk_gc_set_rgb_fg_color(shape_gc, &self->mask_color); gdk_draw_rectangle(shape, shape_gc, TRUE, 0, 0, self->width + 1, self->height + 1); gdk_gc_set_rgb_fg_color(shape_gc, &self->shadow_color); gdk_draw_layout(shape, shape_gc, self->shadow_offset_x, self->shadow_offset_y, self->layout); gdk_gc_set_rgb_fg_color(shape_gc, &self->text_color); gdk_draw_layout(shape, shape_gc, 0, 0, self->layout); gtk_image_set_from_pixmap(GTK_IMAGE(self->image), shape, NULL); // make shape pixbuf from shape pixmap shape_pixbuf = gdk_pixbuf_get_from_drawable(NULL, shape, NULL, 0, 0, 0, 0, self->width, self->height); g_object_unref(shape); g_object_unref(shape_gc); // make alpha enabled pixbuf from shape pixbuf pixbuf = gdk_pixbuf_add_alpha(shape_pixbuf, TRUE, self->mask_color.red, self->mask_color.green, self->mask_color.blue); // finally, i got the mask bitmap! // it's needed to update_shape(). // FIXME: somewhat clumsy :( need better/easy/efficient way. gdk_pixbuf_render_pixmap_and_mask(pixbuf, NULL, &self->mask, 1); g_object_unref(pixbuf); g_object_unref(shape_pixbuf); }
// cv_set_color_fg ( &GdkColor ); gboolean button_press ( GdkEventButton *event ) { guint color = 0; GdkPixbuf *pixbuf = NULL; if ( event->type == GDK_BUTTON_PRESS ) { if ( event->button == LEFT_BUTTON ) { m_priv->gc = m_priv->cv->gc_fg; } else if ( event->button == RIGHT_BUTTON ) { m_priv->gc = m_priv->cv->gc_bg; } m_priv->is_draw = !m_priv->is_draw; if( m_priv->is_draw ) m_priv->button = event->button; m_priv->x0 = (gint)event->x; m_priv->y0 = (gint)event->y; pixbuf = cv_get_pixbuf ( ); if( GDK_IS_PIXBUF( pixbuf ) ) { if(!gdk_pixbuf_get_has_alpha ( pixbuf ) ) { GdkPixbuf *tmp ; tmp = gdk_pixbuf_add_alpha( pixbuf, FALSE, 0, 0, 0 ); g_object_unref(pixbuf); pixbuf = tmp; } if(get_pixel_from_pixbuf( pixbuf, &color, m_priv->x0, m_priv->y0) ) { foreground_set_color_from_rgb ( color ); } g_object_unref ( pixbuf ); } if( !m_priv->is_draw ) gtk_widget_queue_draw ( m_priv->cv->widget ); } return TRUE; }
static void render_simple ( cairo_t *cr, int width, int height, MetaGradientType type, gboolean with_alpha) { GdkPixbuf *pixbuf; GdkRGBA from, to; gdk_rgba_parse (&from, "blue"); gdk_rgba_parse (&to, "green"); pixbuf = meta_gradient_create_simple (width, height, &from, &to, type); if (with_alpha) { const unsigned char alphas[] = { 0xff, 0xaa, 0x2f, 0x0, 0xcc, 0xff, 0xff }; if (!gdk_pixbuf_get_has_alpha (pixbuf)) { GdkPixbuf *new_pixbuf; new_pixbuf = gdk_pixbuf_add_alpha (pixbuf, FALSE, 0, 0, 0); g_object_unref (G_OBJECT (pixbuf)); pixbuf = new_pixbuf; } meta_gradient_add_alpha (pixbuf, alphas, G_N_ELEMENTS (alphas), META_GRADIENT_HORIZONTAL); draw_checkerboard (cr, width, height); } gdk_cairo_set_source_pixbuf (cr, pixbuf, 0, 0); cairo_rectangle (cr, 0, 0, width, height); cairo_fill (cr); g_object_unref (G_OBJECT (pixbuf)); }
static jobject get_data_image(JNIEnv* env) { GdkPixbuf* pixbuf; guchar *data; jbyteArray data_array; jobject buffer, result; int w,h,stride; pixbuf = gtk_clipboard_wait_for_image(get_clipboard()); if (pixbuf == NULL) { return NULL; } if (!gdk_pixbuf_get_has_alpha(pixbuf)) { GdkPixbuf *tmp_buf = gdk_pixbuf_add_alpha(pixbuf, FALSE, 0, 0, 0); g_object_unref(pixbuf); pixbuf = tmp_buf; } w = gdk_pixbuf_get_width(pixbuf); h = gdk_pixbuf_get_height(pixbuf); stride = gdk_pixbuf_get_rowstride(pixbuf); data = gdk_pixbuf_get_pixels(pixbuf); //Actually, we are converting RGBA to BGRA, but that's the same operation data = (guchar*) convert_BGRA_to_RGBA((int*)data, stride, h); data_array = env->NewByteArray(stride*h); EXCEPTION_OCCURED(env); env->SetByteArrayRegion(data_array, 0, stride*h, (jbyte*)data); EXCEPTION_OCCURED(env); buffer = env->CallStaticObjectMethod(jByteBufferCls, jByteBufferWrap, data_array); result = env->NewObject(jGtkPixelsCls, jGtkPixelsInit, w, h, buffer); EXCEPTION_OCCURED(env); g_free(data); g_object_unref(pixbuf); return result; }
/* Handler for "enter-notify-event" signal on image that has highlighting requested. */ static gboolean fb_button_enter(GtkImage * widget, GdkEventCrossing * event) { if (gtk_image_get_storage_type(widget) == GTK_IMAGE_PIXBUF) { ImgData * data = (ImgData *) g_object_get_qdata(G_OBJECT(widget), img_data_id); if (data != NULL) { if (data->hilight == NULL) { GdkPixbuf * dark = data->pixbuf; int height = gdk_pixbuf_get_height(dark); int rowstride = gdk_pixbuf_get_rowstride(dark); gulong hicolor = data->hicolor; GdkPixbuf * light = gdk_pixbuf_add_alpha(dark, FALSE, 0, 0, 0); if (light != NULL) { guchar extra[3]; int i; for (i = 2; i >= 0; i--, hicolor >>= 8) extra[i] = hicolor & 0xFF; guchar * src = gdk_pixbuf_get_pixels(light); guchar * up; for (up = src + height * rowstride; src < up; src += 4) { if (src[3] != 0) { for (i = 0; i < 3; i++) { int value = src[i] + extra[i]; if (value > 255) value = 255; src[i] = value; } } } data->hilight = light; } }
void wxCursor::InitFromImage( const wxImage & image ) { const int w = image.GetWidth(); const int h = image.GetHeight(); const guchar* alpha = image.GetAlpha(); const bool hasMask = image.HasMask(); int hotSpotX = image.GetOptionInt(wxIMAGE_OPTION_CUR_HOTSPOT_X); int hotSpotY = image.GetOptionInt(wxIMAGE_OPTION_CUR_HOTSPOT_Y); if (hotSpotX < 0 || hotSpotX > w) hotSpotX = 0; if (hotSpotY < 0 || hotSpotY > h) hotSpotY = 0; GdkPixbuf* pixbuf = gdk_pixbuf_new_from_data(image.GetData(), GDK_COLORSPACE_RGB, false, 8, w, h, w * 3, NULL, NULL); if (alpha || hasMask) { guchar r = 0, g = 0, b = 0; if (hasMask) { r = image.GetMaskRed(); g = image.GetMaskGreen(); b = image.GetMaskBlue(); } GdkPixbuf* pixbuf0 = pixbuf; pixbuf = gdk_pixbuf_add_alpha(pixbuf, hasMask, r, g, b); g_object_unref(pixbuf0); if (alpha) { guchar* d = gdk_pixbuf_get_pixels(pixbuf); const int stride = gdk_pixbuf_get_rowstride(pixbuf); for (int j = 0; j < h; j++, d += stride) for (int i = 0; i < w; i++, alpha++) if (d[4 * i + 3]) d[4 * i + 3] = *alpha; } } m_refData = new wxCursorRefData; M_CURSORDATA->m_cursor = gdk_cursor_new_from_pixbuf(gtk_widget_get_display(wxGetRootWindow()), pixbuf, hotSpotX, hotSpotY); g_object_unref(pixbuf); }
static gboolean gst_gdk_pixbuf_overlay_load_image (GstGdkPixbufOverlay * overlay, GError ** err) { GdkPixbuf *pixbuf; guint8 *pixels, *p; gint width, height, stride, w, h; pixbuf = gdk_pixbuf_new_from_file (overlay->location, err); if (pixbuf == NULL) return FALSE; if (!gdk_pixbuf_get_has_alpha (pixbuf)) { GdkPixbuf *alpha_pixbuf; /* FIXME: we could do this much more efficiently ourselves below, but * we're lazy for now */ /* FIXME: perhaps expose substitute_color via properties */ alpha_pixbuf = gdk_pixbuf_add_alpha (pixbuf, FALSE, 0, 0, 0); g_object_unref (pixbuf); pixbuf = alpha_pixbuf; } width = gdk_pixbuf_get_width (pixbuf); height = gdk_pixbuf_get_height (pixbuf); stride = gdk_pixbuf_get_rowstride (pixbuf); pixels = gdk_pixbuf_get_pixels (pixbuf); /* the memory layout in GdkPixbuf is R-G-B-A, we want: * - B-G-R-A on little-endian platforms * - A-R-G-B on big-endian platforms */ for (h = 0; h < height; ++h) { p = pixels + (h * stride); for (w = 0; w < width; ++w) { guint8 tmp; /* R-G-B-A ==> B-G-R-A */ tmp = p[0]; p[0] = p[2]; p[2] = tmp; if (G_BYTE_ORDER == G_BIG_ENDIAN) { /* B-G-R-A ==> A-R-G-B */ /* we can probably assume sane alignment */ *((guint32 *) p) = GUINT32_SWAP_LE_BE (*((guint32 *) p)); } p += 4; } } overlay->pixels = gst_buffer_new (); GST_BUFFER_DATA (overlay->pixels) = pixels; /* assume we have row padding even for the last row */ GST_BUFFER_SIZE (overlay->pixels) = height * stride; /* transfer ownership of pixbuf to buffer */ GST_BUFFER_MALLOCDATA (overlay->pixels) = (guint8 *) pixbuf; GST_BUFFER_FREE_FUNC (overlay->pixels) = (GFreeFunc) g_object_unref; overlay->pixels_width = width; overlay->pixels_height = height; overlay->pixels_stride = stride; overlay->update_composition = TRUE; GST_INFO_OBJECT (overlay, "Loaded image, %d x %d", width, height); return TRUE; }
gboolean clock_check(gpointer dummy) { if (dummy) {}; int xneur_pid = -1; char *ps_command = (char *) malloc(1024 * sizeof(char)); if (xneur_old_pid == -1) xneur_old_pid = 1; snprintf(ps_command, 1024, "ps -p %d | grep xneur", xneur_old_pid); FILE *fp = popen(ps_command, "r"); free (ps_command); if (fp != NULL) { char buffer[NAME_MAX]; if (fgets(buffer, NAME_MAX, fp) != NULL) xneur_pid = xneur_old_pid; pclose(fp); } if (xneur_pid == -1) xneur_pid = xconfig->get_pid(xconfig); int xneur_state = xconfig->manual_mode; int xneur_group = get_active_kbd_group(dpy); if (get_kbd_group_count(dpy) != xconfig->handle->total_languages) { for (int i = 0; i < MAX_LAYOUTS; i++) { if (tray->images[i] != NULL) g_free(tray->images[i]); } gtk_widget_destroy(GTK_WIDGET(tray->menu)); tray->menu = NULL; g_spawn_command_line_async(PACKAGE, NULL); gtk_main_quit(); } if (xneur_pid == xneur_old_pid && xneur_state == xneur_old_state && xneur_group == xneur_old_group && force_update == FALSE) return TRUE; force_update = FALSE; xneur_old_pid = xneur_pid; xneur_old_state = xneur_state; xneur_old_group = xneur_group; int lang = get_active_kbd_group(dpy); gchar *hint; gchar *status_text; //float saturation = 1.0; if (xneur_pid != -1) { //saturation = 1.0; hint = g_strdup_printf("%s%s%s", _("X Neural Switcher running ("), xconfig->handle->languages[lang].dir, ")"); status_text = g_strdup_printf("%s", _("Stop daemon")); } else { //saturation = 0.25; hint = g_strdup_printf("%s%s%s", _("X Neural Switcher stopped ("), xconfig->handle->languages[lang].dir, ")"); status_text = g_strdup_printf("%s", _("Start daemon")); } gtk_menu_item_set_label(GTK_MENU_ITEM(tray->status), status_text); gint kbd_gr = get_active_kbd_group(dpy); const char *icon_name = get_tray_icon_name(tray->images[kbd_gr]); if (tray->tray_icon) { gtk_widget_hide_all(GTK_WIDGET(tray->tray_icon)); gtk_widget_destroy (tray->image); if (strcasecmp(show_in_the_tray, "Text") == 0) { char *layout_name = strdup(xconfig->handle->languages[kbd_gr].dir); for (unsigned int i=0; i < strlen(layout_name); i++) layout_name[i] = toupper(layout_name[i]); tray->image = gtk_label_new ((const gchar *)layout_name); gtk_label_set_justify (GTK_LABEL(tray->image), GTK_JUSTIFY_CENTER); free(layout_name); } else { tray->image = gtk_image_new_from_icon_name(icon_name, GTK_ICON_SIZE_LARGE_TOOLBAR); } gtk_container_add(GTK_CONTAINER(tray->evbox), tray->image); gtk_widget_show_all(GTK_WIDGET(tray->tray_icon)); } else if (tray->status_icon) { if (gtk_status_icon_is_embedded(tray->status_icon)) { if (strcasecmp(show_in_the_tray, "Text") == 0) { char *layout_name = strdup(xconfig->handle->languages[kbd_gr].dir); for (unsigned int i=0; i < strlen(layout_name); i++) layout_name[i] = toupper(layout_name[i]); GdkPixbuf *pb = text_to_gtk_pixbuf (layout_name); free(layout_name); pb = gdk_pixbuf_add_alpha(pb, TRUE, 255, 255, 255); gtk_status_icon_set_from_pixbuf(tray->status_icon, pb); g_object_unref(pb); } else { gtk_status_icon_set_from_icon_name(tray->status_icon, icon_name); } gtk_status_icon_set_tooltip(tray->status_icon, hint); } } #ifdef HAVE_APP_INDICATOR else if (tray->app_indicator) { char *layout_name = strdup(xconfig->handle->languages[kbd_gr].name); if (strcasecmp(show_in_the_tray, "Text") == 0) { #ifdef HAVE_DEPREC_APP_INDICATOR app_indicator_set_icon (tray->app_indicator, icon_name); #else app_indicator_set_label (tray->app_indicator, layout_name, layout_name); app_indicator_set_icon (tray->app_indicator, ""); #endif } else { #ifdef HAVE_DEPREC_APP_INDICATOR app_indicator_set_icon (tray->app_indicator, icon_name); #else app_indicator_set_icon (tray->app_indicator, icon_name); app_indicator_set_label (tray->app_indicator,"", ""); #endif } free(layout_name); } #endif g_free (hint); g_free (status_text); return TRUE; }
void create_tray_icon (void) { dpy = XOpenDisplay(NULL); gxneur_config_read_str("show_in_the_tray", &show_in_the_tray); gxneur_config_read_str("rendering_engine", &rendering_engine); gxneur_config_add_notify("show_in_the_tray", show_in_the_tray_callback, NULL); gxneur_config_add_notify("rendering_engine", rendering_engine_callback, NULL); if (arg_show_in_the_tray) g_free(show_in_the_tray), show_in_the_tray = g_strdup(arg_show_in_the_tray); if (arg_rendering_engine) g_free(rendering_engine), rendering_engine = g_strdup(arg_rendering_engine); if (!show_in_the_tray) show_in_the_tray = g_strdup(DEFAULT_SHOW_IN_THE_TRAY); if (!rendering_engine) rendering_engine = g_strdup(DEFAULT_RENDERING_ENGINE); tray = g_new0(struct _tray_icon, 1); #ifdef HAVE_APP_INDICATOR tray->app_indicator = NULL; #endif tray->status_icon = NULL; tray->tray_icon = NULL; // Init pixbuf array for (int i = 0; i < MAX_LAYOUTS; i++) { tray->images[i] = NULL; } // Load images names for (int i = 0; i < xconfig->handle->total_languages; i++) { char *layout_name = strdup(xconfig->handle->languages[i].dir); tray->images[i] = g_strdup_printf("%s-%s", PACKAGE, layout_name); free(layout_name); } tray->menu = create_menu(tray, xconfig->manual_mode); int tray_icon_created = 0; int tray_icon_failed = 0; if (strcasecmp(rendering_engine, "AppIndicator") == 0) { #ifdef HAVE_APP_INDICATOR // App indicator tray->app_indicator = app_indicator_new ("X Neural Switcher", PACKAGE, APP_INDICATOR_CATEGORY_APPLICATION_STATUS); if (tray->app_indicator) { app_indicator_set_status (tray->app_indicator, APP_INDICATOR_STATUS_ACTIVE); app_indicator_set_menu (tray->app_indicator, tray->menu); tray_icon_created = 1; tray_icon_failed = 0; } else { tray_icon_failed = 1; } #else tray_icon_failed = 1; #endif } gint kbd_gr = get_active_kbd_group(dpy); // Tray icon if (strcasecmp(rendering_engine, "Built-in") == 0 /*|| tray_icon_failed*/) { tray->tray_icon = _gtk_tray_icon_new(_("X Neural Switcher")); if (tray->tray_icon) { g_signal_connect(G_OBJECT(tray->tray_icon), "button_press_event", G_CALLBACK(tray_icon_press), NULL); tray->evbox = gtk_event_box_new(); gtk_event_box_set_visible_window(GTK_EVENT_BOX(tray->evbox), 0); if (strcasecmp(show_in_the_tray, "Text") == 0) { char *layout_name = strdup(xconfig->handle->languages[kbd_gr].dir); for (unsigned int i=0; i < strlen(layout_name); i++) layout_name[i] = toupper(layout_name[i]); tray->image = gtk_label_new ((const gchar *)layout_name); gtk_label_set_justify (GTK_LABEL(tray->image), GTK_JUSTIFY_CENTER); free(layout_name); } else { tray->image = gtk_image_new_from_icon_name(tray->images[kbd_gr], GTK_ICON_SIZE_LARGE_TOOLBAR); } gtk_container_add(GTK_CONTAINER(tray->evbox), tray->image); gtk_container_add(GTK_CONTAINER(tray->tray_icon), tray->evbox); gtk_widget_show_all(GTK_WIDGET(tray->tray_icon)); tray_icon_created = 1; tray_icon_failed = 0; } else { tray_icon_failed = 1; } } // Status Icon if (tray_icon_failed || !tray_icon_created || strcasecmp(rendering_engine, "StatusIcon") == 0 ) { tray->status_icon = gtk_status_icon_new(); g_signal_connect(G_OBJECT(tray->status_icon), "activate", G_CALLBACK(status_icon_on_click), NULL); g_signal_connect(G_OBJECT(tray->status_icon), "popup-menu", G_CALLBACK(status_icon_on_menu), NULL); gtk_status_icon_set_from_icon_name(tray->status_icon, PACKAGE); gtk_status_icon_set_tooltip_text(tray->status_icon, "X Neural Switcher"); gtk_status_icon_set_visible(tray->status_icon, TRUE); if (strcasecmp(show_in_the_tray, "Text") == 0) { char *layout_name = strdup(xconfig->handle->languages[kbd_gr].dir); for (unsigned int i=0; i < strlen(layout_name); i++) layout_name[i] = toupper(layout_name[i]); GdkPixbuf *pb = text_to_gtk_pixbuf (layout_name); free(layout_name); pb = gdk_pixbuf_add_alpha(pb, TRUE, 255, 255, 255); gtk_status_icon_set_from_pixbuf(tray->status_icon, pb); g_object_unref(pb); } else { gtk_status_icon_set_from_icon_name(tray->status_icon, tray->images[kbd_gr]); } } force_update = TRUE; g_timeout_add(1000, clock_check, 0); }
/**************************************************************************** Create a new sprite by cropping and taking only the given portion of the image. source gives the sprite that is to be cropped. x,y, width, height gives the rectangle to be cropped. The pixel at position of the source sprite will be at (0,0) in the new sprite, and the new sprite will have dimensions (width, height). mask gives an additional mask to be used for clipping the new sprite. mask_offset_x, mask_offset_y is the offset of the mask relative to the origin of the source image. The pixel at (mask_offset_x,mask_offset_y) in the mask image will be used to clip pixel (0,0) in the source image which is pixel (-x,-y) in the new image. ****************************************************************************/ struct sprite *crop_sprite(struct sprite *source, int x, int y, int width, int height, struct sprite *mask, int mask_offset_x, int mask_offset_y) { GdkPixbuf *mypixbuf, *sub, *mask_pixbuf; /* First just crop the image. */ if (x < 0) { width += x; x = 0; } if (y < 0) { height += y; y = 0; } width = CLIP(0, width, source->width - x); height = CLIP(0, height, source->height - y); sub = gdk_pixbuf_new_subpixbuf(sprite_get_pixbuf(source), x, y, width, height); mypixbuf = gdk_pixbuf_copy(sub); g_object_unref(sub); /* Now mask. This reduces the alpha of the final image proportional to the * alpha of the mask. Thus if the mask has 50% alpha the final image will * be reduced by 50% alpha. Note that the mask offset is in coordinates * relative to the clipped image not the final image. */ if (mask && (mask_pixbuf = sprite_get_pixbuf(mask)) && gdk_pixbuf_get_has_alpha(mask_pixbuf)) { int x1, y1; /* The mask offset is the offset of the mask relative to the origin * of the original source image. For instance when cropping with * blending sprites the offset is always 0. Here we convert the * coordinates so that they are relative to the origin of the new * (cropped) image. */ mask_offset_x -= x; mask_offset_y -= y; width = CLIP(0, width, mask->width + mask_offset_x); height = CLIP(0, height, mask->height + mask_offset_y); if (!gdk_pixbuf_get_has_alpha(mypixbuf)) { GdkPixbuf *p2 = mypixbuf; mypixbuf = gdk_pixbuf_add_alpha(mypixbuf, FALSE, 0, 0, 0); g_object_unref(p2); } for (x1 = 0; x1 < width; x1++) { for (y1 = 0; y1 < height; y1++) { int mask_x = x1 - mask_offset_x, mask_y = y1 - mask_offset_y; guchar *alpha = gdk_pixbuf_get_pixels(mypixbuf) + y1 * gdk_pixbuf_get_rowstride(mypixbuf) + x1 * gdk_pixbuf_get_n_channels(mypixbuf) + 3; guchar *mask_alpha = gdk_pixbuf_get_pixels(mask_pixbuf) + mask_y * gdk_pixbuf_get_rowstride(mask_pixbuf) + mask_x * gdk_pixbuf_get_n_channels(mask_pixbuf) + 3; *alpha = (*alpha) * (*mask_alpha) / 255; } } } return ctor_sprite(mypixbuf); }
void gp_image_set_diff_pixmap ( GpImage *image, GdkPixmap* pixmap, guint x_offset, guint y_offset ) { GdkPixbuf *pixbuf; GdkPixbuf *m_pixbuf; guchar *pixels, *m_pixels; guchar *p, *m_p; gint w, h; gint n_channels, rowstride; g_return_if_fail ( GP_IS_IMAGE (image) ); pixbuf = image->priv->pixbuf; if(!gdk_pixbuf_get_has_alpha ( pixbuf ) ) { /*add alpha*/ GdkPixbuf *tmp ; tmp = gdk_pixbuf_add_alpha(pixbuf, FALSE, 0, 0, 0); g_object_unref(pixbuf); pixbuf = tmp; } m_pixbuf = gdk_pixbuf_copy ( pixbuf ); w = gdk_pixbuf_get_width ( pixbuf ); h = gdk_pixbuf_get_height ( pixbuf ); gdk_pixbuf_get_from_drawable ( m_pixbuf, pixmap, gdk_drawable_get_colormap (pixmap), x_offset,y_offset, 0,0, w,h); n_channels = gdk_pixbuf_get_n_channels ( pixbuf ); rowstride = gdk_pixbuf_get_rowstride ( pixbuf ); pixels = gdk_pixbuf_get_pixels ( pixbuf ); m_pixels = gdk_pixbuf_get_pixels ( m_pixbuf ); while (h--) { guint i = w; p = pixels; m_p = m_pixels; while (i--) { pixel_union *pu, *m_pu; pu = (pixel_union *)p; m_pu = (pixel_union *)m_p; if(pu->ui32 == m_pu->ui32) { p[0] = 0; p[1] = 0; p[2] = 0; p[3] = 0; } p += n_channels; m_p += n_channels; } pixels += rowstride; m_pixels += rowstride; } g_object_unref (m_pixbuf); }
/* Takes ownership of pixbuf; call with OBJECT_LOCK */ static void gst_gdk_pixbuf_overlay_set_pixbuf (GstGdkPixbufOverlay * overlay, GdkPixbuf * pixbuf) { GstVideoMeta *video_meta; guint8 *pixels, *p; gint width, height, stride, w, h, plane; if (!gdk_pixbuf_get_has_alpha (pixbuf)) { GdkPixbuf *alpha_pixbuf; /* FIXME: we could do this much more efficiently ourselves below, but * we're lazy for now */ /* FIXME: perhaps expose substitute_color via properties */ alpha_pixbuf = gdk_pixbuf_add_alpha (pixbuf, FALSE, 0, 0, 0); g_object_unref (pixbuf); pixbuf = alpha_pixbuf; } width = gdk_pixbuf_get_width (pixbuf); height = gdk_pixbuf_get_height (pixbuf); stride = gdk_pixbuf_get_rowstride (pixbuf); pixels = gdk_pixbuf_get_pixels (pixbuf); /* the memory layout in GdkPixbuf is R-G-B-A, we want: * - B-G-R-A on little-endian platforms * - A-R-G-B on big-endian platforms */ for (h = 0; h < height; ++h) { p = pixels + (h * stride); for (w = 0; w < width; ++w) { guint8 tmp; /* R-G-B-A ==> B-G-R-A */ tmp = p[0]; p[0] = p[2]; p[2] = tmp; if (G_BYTE_ORDER == G_BIG_ENDIAN) { /* B-G-R-A ==> A-R-G-B */ /* we can probably assume sane alignment */ *((guint32 *) p) = GUINT32_SWAP_LE_BE (*((guint32 *) p)); } p += 4; } } /* assume we have row padding even for the last row */ /* transfer ownership of pixbuf to the buffer */ overlay->pixels = gst_buffer_new_wrapped_full (GST_MEMORY_FLAG_READONLY, pixels, height * stride, 0, height * stride, pixbuf, (GDestroyNotify) g_object_unref); video_meta = gst_buffer_add_video_meta (overlay->pixels, GST_VIDEO_FRAME_FLAG_NONE, GST_VIDEO_OVERLAY_COMPOSITION_FORMAT_RGB, width, height); for (plane = 0; plane < video_meta->n_planes; ++plane) video_meta->stride[plane] = stride; overlay->update_composition = TRUE; GST_INFO_OBJECT (overlay, "Updated pixbuf, %d x %d", width, height); }
static void area_updated_cb (GdkPixbufLoader *loader, gint x, gint y, gint width, gint height, jobject *decoder) { JNIEnv *env; union env_union e; jint stride_bytes, stride_pixels, n_channels, n_pixels; jintArray jpixels; jint *java_pixels; guchar *gdk_pixels; GdkPixbuf *pixbuf_no_alpha = NULL; GdkPixbuf *pixbuf = NULL; #ifndef WORDS_BIGENDIAN int i; #endif pixbuf_no_alpha = gdk_pixbuf_loader_get_pixbuf (loader); if (pixbuf_no_alpha == NULL) return; pixbuf = gdk_pixbuf_add_alpha(pixbuf_no_alpha, FALSE, 0, 0, 0); g_assert (gdk_pixbuf_get_has_alpha (pixbuf)); stride_bytes = gdk_pixbuf_get_rowstride (pixbuf); n_channels = gdk_pixbuf_get_n_channels (pixbuf); stride_pixels = stride_bytes / n_channels; n_pixels = height * stride_pixels; gdk_pixels = gdk_pixbuf_get_pixels (pixbuf); e.jni_env = &env; (*vm)->GetEnv (vm, e.void_env, JNI_VERSION_1_1); jpixels = (*env)->NewIntArray (env, n_pixels); java_pixels = (*env)->GetIntArrayElements (env, jpixels, NULL); memcpy (java_pixels, gdk_pixels + (y * stride_bytes), (height * stride_bytes)); #ifndef WORDS_BIGENDIAN /* convert pixels from 0xBBGGRRAA to 0xAARRGGBB */ for (i = 0; i < n_pixels; ++i) { java_pixels[i] = SWAPU32 ((unsigned)java_pixels[i]); } #endif g_object_unref (pixbuf); (*env)->ReleaseIntArrayElements (env, jpixels, java_pixels, 0); (*env)->CallVoidMethod (env, *decoder, areaUpdatedID, (jint) x, (jint) y, (jint) width, (jint) height, jpixels, stride_pixels); (*env)->DeleteLocalRef(env, jpixels); }
static void go_image_set_property (GObject *obj, guint param_id, GValue const *value, GParamSpec *pspec) { GOImage *image = GO_IMAGE (obj); gboolean size_changed = FALSE; guint n; switch (param_id) { case IMAGE_PROP_WIDTH: n = g_value_get_uint (value); if (n != image->width) { image->width = n; size_changed = TRUE; } break; case IMAGE_PROP_HEIGHT: n = g_value_get_uint (value); if (n != image->height) { image->height = n; size_changed = TRUE; } break; #ifdef GOFFICE_WITH_GTK case IMAGE_PROP_PIXBUF: { GdkPixbuf *pixbuf = GDK_PIXBUF (g_value_get_object (value)); if (!GDK_IS_PIXBUF (pixbuf)) break; if (!gdk_pixbuf_get_has_alpha (pixbuf)) pixbuf = gdk_pixbuf_add_alpha (pixbuf, FALSE, 0, 0, 0); else g_object_ref (pixbuf); if (image->pixbuf) g_object_unref (image->pixbuf); image->pixbuf = pixbuf; if (image->data != NULL) { g_free (image->data); image->data = NULL; } image->width = gdk_pixbuf_get_width (pixbuf); image->height = gdk_pixbuf_get_height (pixbuf); image->rowstride = gdk_pixbuf_get_rowstride (pixbuf); image->target_cairo = FALSE; } break; #endif default: G_OBJECT_WARN_INVALID_PROPERTY_ID (obj, param_id, pspec); return; /* NOTE : RETURN */ } if (size_changed) { if (image->pixbuf) { g_object_unref (image->pixbuf); image->pixbuf = NULL; } if (image->data != NULL) g_free (image->data); /* GOImage only supports pixbuf with alpha values at the moment */ image->rowstride = image->width * 4; image->data = g_new0 (guint8, image->height * image->rowstride); image->target_cairo = TRUE; } }
SetIconText(GtkStatusIcon *tray_icon, const char *text, const char *color) { // build background from image GdkPixbuf* special_icon = gdk_pixbuf_new_from_file("message-mail-new.png", NULL); // GError **error); GdkPixbuf *dest = gdk_pixbuf_copy(special_icon); int w=gdk_pixbuf_get_width(special_icon); int h=gdk_pixbuf_get_height(special_icon); // prepare colors/alpha GdkColormap* cmap=gdk_screen_get_system_colormap(gdk_screen_get_default()); int screen_depth=24; GdkVisual* visual = gdk_colormap_get_visual(cmap); screen_depth = visual->depth; GdkColor fore = { 0, 0, 0, 0 }; GdkColor alpha = { 0xFFFF, 0xFFFF, 0xFFFF, 0xFFFF}; gdk_color_parse(color, &fore); if(fore.red==alpha.red && fore.green==alpha.green && fore.blue==alpha.blue) { alpha.red=0; // make sure alpha is different from fore } gdk_colormap_alloc_color (cmap, &fore, TRUE, TRUE); gdk_colormap_alloc_color (cmap, &alpha, TRUE, TRUE); // build pixmap with rectangle GdkPixmap *pm = gdk_pixmap_new (NULL, w, h, screen_depth); cairo_t *cr = gdk_cairo_create(pm); gdk_cairo_set_source_color(cr, &alpha); /* void gdk_cairo_set_source_color (cairo_t *cr, */ /* const GdkColor *color); */ cairo_rectangle(cr, 0, 0, w, h); /* void cairo_rectangle (cairo_t *cr, */ /* double x, */ /* double y, */ /* double width, */ /* double height); */ cairo_set_source_rgb(cr, 1, 1, 1); cairo_fill(cr); // build text GtkWidget *scratch = gtk_window_new(GTK_WINDOW_TOPLEVEL); PangoLayout *layout = gtk_widget_create_pango_layout(scratch, NULL); gtk_widget_destroy(scratch); PangoFontDescription *fnt = pango_font_description_from_string("Sans 18"); pango_font_description_set_weight (fnt,PANGO_WEIGHT_SEMIBOLD); pango_layout_set_spacing (layout,0); pango_layout_set_font_description (layout, fnt); pango_layout_set_text (layout, (gchar *)text,-1); int tw=0; int th=0; int sz; int border=4; pango_layout_get_pixel_size(layout, &tw, &th); while( (tw>w - border || th > h - border)) //fit text to the icon by decreasing font size { sz=pango_font_description_get_size (fnt); if(sz<MIN_FONT_SIZE) { sz=MIN_FONT_SIZE; break; } sz-=PANGO_SCALE; pango_font_description_set_size (fnt,sz); pango_layout_set_font_description (layout, fnt); pango_layout_get_pixel_size(layout, &tw, &th); } pango_font_description_free (fnt); // center text int px, py; px=(w-tw)/2; py=(h-th)/2; // draw text on pixmap gdk_cairo_set_source_color(cr, &fore); cairo_move_to (cr, px, py); pango_cairo_show_layout (cr, layout); cairo_destroy(cr); g_object_unref (layout); GdkPixbuf *buf = gdk_pixbuf_get_from_drawable (NULL, pm, NULL, 0, 0, 0, 0, w, h); g_object_unref (pm); GdkPixbuf *alpha_buf = gdk_pixbuf_add_alpha(buf, TRUE, (guchar)alpha.red, (guchar)alpha.green, (guchar)alpha.blue); g_object_unref (buf); //merge the rendered text on top gdk_pixbuf_composite(alpha_buf,dest,0,0,w,h,0,0,1,1,GDK_INTERP_NEAREST,255); g_object_unref(alpha_buf); /* gdk_pixbuf_composite(buf,dest,0,0,w,h,0,0,1,1,GDK_INTERP_NEAREST,255); */ /* g_object_unref(buf); */ gtk_status_icon_set_from_pixbuf(GTK_STATUS_ICON(tray_icon), GDK_PIXBUF(dest)); }
static enum xshm_map_mode x_tile_set_map_mode(TileTab *t, NhGtkProgressWindow *w) { int i; enum xshm_map_mode mode; GdkPixbuf *copy; guchar *pixels; GdkGC *gc; if (!t->spread && !t->transparent) mode = XSHM_MAP_PIXMAP; else mode = getenv("HACKPIXBUF") ? XSHM_MAP_PIXBUF : XSHM_MAP_IMAGE; /* * Pixbufs use so much memory that it's not unexpected for us to * fail to generate the alpha channel correctly. This is not a * great problem since we carefully avoid using it ourselves * (preferring to use the transparent colour directly), but might * cause gdk_pixbuf_render_to_drawable() to get slightly confused * when it dithers the tile (the transparent colour may bleed into * the glyph). We therefore issue a warning. * * Note: It's not clear that gdk_pixbuf_render_to_drawable() * actually uses this information, but what more can we do? * There's little point using gdk_pixbuf_render_to_drawable_alpha() * since all that does is take care not to draw transparent pixels * (which we don't care about anyway; we'll never read them). */ if (mode == XSHM_MAP_PIXBUF && gdk_pixbuf_get_has_alpha(tile_pixbuf)) { /* We can't trust the XPM alpha channel & it will * overide if we don't get rid of it here */ copy = gdk_pixbuf_new(gdk_pixbuf_get_colorspace(tile_pixbuf), FALSE, gdk_pixbuf_get_bits_per_sample(tile_pixbuf), t->tilemap_width, t->tilemap_height); nh_gtk_progress_window_stage_set_fraction(w, 0.25); if (copy) { gdk_pixbuf_copy_area(tile_pixbuf, 0, 0, t->tilemap_width, t->tilemap_height, copy, 0, 0); gdk_pixbuf_unref(tile_pixbuf); tile_pixbuf = copy; nh_gtk_progress_window_stage_set_fraction(w, 0.5); } else { pline("Warning: Not enough memory: Tiles may be degraded"); mode = XSHM_MAP_IMAGE; nh_gtk_progress_window_stage_set_fraction(w, 0.0); } } if (mode == XSHM_MAP_PIXBUF && !gdk_pixbuf_get_has_alpha(tile_pixbuf)) { pixels = gdk_pixbuf_get_pixels(tile_pixbuf); nh_gtk_progress_window_stage_set_fraction(w, 0.75); copy = gdk_pixbuf_add_alpha(tile_pixbuf, TRUE, pixels[0], pixels[1], pixels[2]); if (copy) { gdk_pixbuf_unref(tile_pixbuf); tile_pixbuf = copy; } else { pline("Warning: Not enough memory: Tiles may be degraded"); mode = XSHM_MAP_IMAGE; nh_gtk_progress_window_stage_set_fraction(w, 0.0); } } tmp_pixbuf = NULL; if (mode != XSHM_MAP_PIXBUF) { tile_pixmap = gdk_pixmap_new(main_window->window, t->tilemap_width, t->tilemap_height, -1); if (!tile_pixmap) panic("Not enough memory to load tiles!"); nh_gtk_progress_window_stage_set_fraction(w, mode == XSHM_MAP_IMAGE ? 0.025 : 0.3); gc = gdk_gc_new(tile_pixmap); gdk_pixbuf_render_to_drawable(tile_pixbuf, tile_pixmap, gc, 0, 0, 0, 0, t->tilemap_width, t->tilemap_height, GDK_RGB_DITHER_NORMAL, 0, 0); gdk_gc_unref(gc); if (mode == XSHM_MAP_IMAGE) { int step = total_tiles_used >= 100 ? total_tiles_used / 100 : 1; nh_gtk_progress_window_stage_set_fraction(w, 0.1); tile_transp = (struct tile_transp *) alloc(total_tiles_used * sizeof(*tile_transp)); for(i = 0; i < total_tiles_used; i++) { calc_tile_transp(t, tile_pixbuf, i); if (i % step == 0) nh_gtk_progress_window_stage_set_fraction(w, 0.1 + (i * 0.8) / total_tiles_used); } calc_tile_transp(t, tile_pixbuf, -1); nh_gtk_progress_window_stage_set_fraction(w, 0.9); /* TODO: Creating an image via a pixmap is very inefficient; * this should be done directly from pixbuf, even if GTK+ doesn't * provide any easy way to do this. */ tile_image = gdk_image_get((GdkWindow *)tile_pixmap, 0, 0, t->tilemap_width, t->tilemap_height); if (!tile_image) panic("Not enough memory to load tiles!"); gdk_pixmap_unref(tile_pixmap); tile_pixmap = NULL; nh_gtk_progress_window_stage_set_fraction(w, 0.975); tmp_img = gdk_image_new(GDK_IMAGE_NORMAL, gdk_visual_get_system(), t->unit_width, t->unit_height); if (!tmp_img) panic("Not enough memory to load tiles!"); #if GTK_CHECK_VERSION(1,3,3) tile_bits_per_pixel = tile_image->bits_per_pixel; #else /* * Technically, this could give an incorrect result. However, as * long as the bitmap_pad is no more than 32 bits (max. X11 allows); * the bytes/line is not larger than necessary; and the width is * at least 32 pixels, then it will be correct. */ tile_bits_per_pixel = tile_image->bpl * 8 / t->tilemap_width; #endif } else tmp_img = tile_image = NULL; gdk_pixbuf_unref(tile_pixbuf); tile_pixbuf = NULL; } nh_gtk_progress_window_stage_set_fraction(w, 1.0); return mode; }