Author: vince
Date: Thu Dec 22 18:48:21 2011
New Revision: 13330
URL:
http://source.netsurf-browser.org?rev=13330&view=rev
Log:
fix opacity testing and setting
Modified:
branches/vince/netsurf-cairo/gtk/bitmap.c
branches/vince/netsurf-cairo/gtk/bitmap.h
branches/vince/netsurf-cairo/gtk/plotters.c
branches/vince/netsurf-cairo/gtk/thumbnail.c
branches/vince/netsurf-cairo/image/image.c
Modified: branches/vince/netsurf-cairo/gtk/bitmap.c
URL:
http://source.netsurf-browser.org/branches/vince/netsurf-cairo/gtk/bitmap...
==============================================================================
--- branches/vince/netsurf-cairo/gtk/bitmap.c (original)
+++ branches/vince/netsurf-cairo/gtk/bitmap.c Thu Dec 22 18:48:21 2011
@@ -48,37 +48,76 @@
void *bitmap_create(int width, int height, unsigned int state)
{
- cairo_surface_t *surface;
-
- if ((state & BITMAP_OPAQUE) != 0) {
- surface = cairo_image_surface_create(CAIRO_FORMAT_RGB24, width, height);
+ struct bitmap *gbitmap;
+
+ gbitmap = calloc(1, sizeof(struct bitmap));
+ if (gbitmap != NULL) {
+ if ((state & BITMAP_OPAQUE) != 0) {
+ gbitmap->surface = cairo_image_surface_create(CAIRO_FORMAT_RGB24, width, height);
+ } else {
+ gbitmap->surface = cairo_image_surface_create(CAIRO_FORMAT_ARGB32, width, height);
+ }
+
+ if (cairo_surface_status(gbitmap->surface) != CAIRO_STATUS_SUCCESS) {
+ cairo_surface_destroy(gbitmap->surface);
+ free(gbitmap);
+ gbitmap = NULL;
+ }
+ }
+
+ return gbitmap;
+}
+
+
+/**
+ * Sets whether a bitmap should be plotted opaque
+ *
+ * \param vbitmap a bitmap, as returned by bitmap_create()
+ * \param opaque whether the bitmap should be plotted opaque
+ */
+void bitmap_set_opaque(void *vbitmap, bool opaque)
+{
+ struct bitmap *gbitmap = (struct bitmap *)vbitmap;
+ cairo_format_t fmt;
+ cairo_surface_t *nsurface = NULL;
+
+ assert(gbitmap);
+
+ fmt = cairo_image_surface_get_format(gbitmap->surface);
+ if (fmt == CAIRO_FORMAT_RGB24) {
+ if (opaque == false) {
+ /* opaque to transparent */
+ nsurface = cairo_image_surface_create(CAIRO_FORMAT_ARGB32,
+ cairo_image_surface_get_width(gbitmap->surface),
+ cairo_image_surface_get_height(gbitmap->surface));
+
+ }
+
} else {
- surface = cairo_image_surface_create(CAIRO_FORMAT_ARGB32, width, height);
- }
-
- if (cairo_surface_status(surface) != CAIRO_STATUS_SUCCESS) {
- cairo_surface_destroy(surface);
- surface = NULL;
- }
-
- return surface;
-}
-
-
-/**
- * Sets whether a bitmap should be plotted opaque
- *
- * \param vbitmap a bitmap, as returned by bitmap_create()
- * \param opaque whether the bitmap should be plotted opaque
- */
-void bitmap_set_opaque(void *vbitmap, bool opaque)
-{
- cairo_surface_t *surface = (cairo_surface_t *)vbitmap;
- assert(surface);
- /* gtk image surface cannot change format. but its harmless to
- * plot with alpha. this does not cope with opaque to
- * transparent conversions but currently those never happen
- */
+ if (opaque == true) {
+ /* transparent to opaque */
+ nsurface = cairo_image_surface_create(CAIRO_FORMAT_RGB24,
+ cairo_image_surface_get_width(gbitmap->surface),
+ cairo_image_surface_get_height(gbitmap->surface));
+
+ }
+ }
+
+ if (nsurface != NULL) {
+ if (cairo_surface_status(nsurface) != CAIRO_STATUS_SUCCESS) {
+ cairo_surface_destroy(nsurface);
+ } else {
+ memcpy(cairo_image_surface_get_data(nsurface),
+ cairo_image_surface_get_data(gbitmap->surface),
+ cairo_image_surface_get_stride(gbitmap->surface) *
cairo_image_surface_get_height(gbitmap->surface));
+ cairo_surface_destroy(gbitmap->surface);
+ gbitmap->surface = nsurface;
+
+ cairo_surface_mark_dirty(gbitmap->surface);
+
+ }
+
+ }
}
@@ -90,10 +129,24 @@
*/
bool bitmap_test_opaque(void *vbitmap)
{
- cairo_surface_t *surface = (cairo_surface_t *)vbitmap;
- assert(surface);
- /* todo: test if bitmap is opaque */
- return false;
+ struct bitmap *gbitmap = (struct bitmap *)vbitmap;
+ unsigned char *pixels;
+ int pcount;
+ int ploop;
+ assert(gbitmap);
+
+ pixels = cairo_image_surface_get_data(gbitmap->surface);
+
+ pcount = cairo_image_surface_get_stride(gbitmap->surface) *
+ cairo_image_surface_get_height(gbitmap->surface);
+
+ for (ploop=3; ploop < pcount; ploop+=4) {
+ if (pixels[ploop] != 0xff) {
+ return false;
+ }
+ }
+
+ return true;
}
@@ -104,12 +157,12 @@
*/
bool bitmap_get_opaque(void *vbitmap)
{
- cairo_surface_t *surface = (cairo_surface_t *)vbitmap;
+ struct bitmap *gbitmap = (struct bitmap *)vbitmap;
cairo_format_t fmt;
- assert(surface);
-
- fmt = cairo_image_surface_get_format(surface);
+ assert(gbitmap);
+
+ fmt = cairo_image_surface_get_format(gbitmap->surface);
if (fmt == CAIRO_FORMAT_RGB24) {
return true;
}
@@ -130,12 +183,12 @@
unsigned char *bitmap_get_buffer(void *vbitmap)
{
- cairo_surface_t *surface = (cairo_surface_t *)vbitmap;
- assert(surface);
+ struct bitmap *gbitmap = (struct bitmap *)vbitmap;
+ assert(gbitmap);
- cairo_surface_flush (surface);
+ cairo_surface_flush(gbitmap->surface);
- return cairo_image_surface_get_data(surface);
+ return cairo_image_surface_get_data(gbitmap->surface);
}
@@ -148,9 +201,10 @@
size_t bitmap_get_rowstride(void *vbitmap)
{
- cairo_surface_t *surface = (cairo_surface_t *)vbitmap;
- assert(surface);
- return cairo_image_surface_get_stride(surface);
+ struct bitmap *gbitmap = (struct bitmap *)vbitmap;
+ assert(gbitmap);
+
+ return cairo_image_surface_get_stride(gbitmap->surface);
}
@@ -163,8 +217,9 @@
size_t bitmap_get_bpp(void *vbitmap)
{
- cairo_surface_t *surface = (cairo_surface_t *)vbitmap;
- assert(surface);
+ struct bitmap *gbitmap = (struct bitmap *)vbitmap;
+ assert(gbitmap);
+
return 4;
}
@@ -178,10 +233,11 @@
void bitmap_destroy(void *vbitmap)
{
- cairo_surface_t *surface = (cairo_surface_t *)vbitmap;
- assert(surface);
-
- cairo_surface_destroy(surface);
+ struct bitmap *gbitmap = (struct bitmap *)vbitmap;
+ assert(gbitmap);
+
+ cairo_surface_destroy(gbitmap->surface);
+ free(gbitmap);
}
@@ -196,8 +252,8 @@
bool bitmap_save(void *vbitmap, const char *path, unsigned flags)
{
- cairo_surface_t *surface = (cairo_surface_t *)vbitmap;
- assert(surface);
+ struct bitmap *gbitmap = (struct bitmap *)vbitmap;
+ assert(gbitmap);
return false;
}
@@ -209,20 +265,20 @@
* \param bitmap a bitmap, as returned by bitmap_create()
*/
void bitmap_modified(void *vbitmap) {
- cairo_surface_t *surface = (cairo_surface_t *)vbitmap;
+ struct bitmap *gbitmap = (struct bitmap *)vbitmap;
int pixel_loop;
int pixel_count;
uint32_t *pixels;
uint32_t pixel;
cairo_format_t fmt;
- assert(surface);
-
- fmt = cairo_image_surface_get_format(surface);
-
- pixel_count = cairo_image_surface_get_width(surface) *
- cairo_image_surface_get_height(surface);
- pixels = (uint32_t *)cairo_image_surface_get_data(surface);
+ assert(gbitmap);
+
+ fmt = cairo_image_surface_get_format(gbitmap->surface);
+
+ pixel_count = cairo_image_surface_get_width(gbitmap->surface) *
+ cairo_image_surface_get_height(gbitmap->surface);
+ pixels = (uint32_t *)cairo_image_surface_get_data(gbitmap->surface);
if (fmt == CAIRO_FORMAT_RGB24) {
for (pixel_loop=0; pixel_loop < pixel_count; pixel_loop++) {
@@ -252,7 +308,9 @@
}
}
- cairo_surface_mark_dirty (surface);
+ cairo_surface_mark_dirty(gbitmap->surface);
+
+ gbitmap->converted = true;
}
@@ -268,13 +326,17 @@
}
int bitmap_get_width(void *vbitmap){
- cairo_surface_t *surface = (cairo_surface_t *)vbitmap;
- return cairo_image_surface_get_width(surface);
+ struct bitmap *gbitmap = (struct bitmap *)vbitmap;
+ assert(gbitmap);
+
+ return cairo_image_surface_get_width(gbitmap->surface);
}
int bitmap_get_height(void *vbitmap){
- cairo_surface_t *surface = (cairo_surface_t *)vbitmap;
- return cairo_image_surface_get_height(surface);
-}
-
-
+ struct bitmap *gbitmap = (struct bitmap *)vbitmap;
+ assert(gbitmap);
+
+ return cairo_image_surface_get_height(gbitmap->surface);
+}
+
+
Modified: branches/vince/netsurf-cairo/gtk/bitmap.h
URL:
http://source.netsurf-browser.org/branches/vince/netsurf-cairo/gtk/bitmap...
==============================================================================
--- branches/vince/netsurf-cairo/gtk/bitmap.h (original)
+++ branches/vince/netsurf-cairo/gtk/bitmap.h Thu Dec 22 18:48:21 2011
@@ -22,6 +22,9 @@
#include <cairo.h>
#include "image/bitmap.h"
-cairo_surface_t *gtk_bitmap_get_surface(struct bitmap *bitmap);
+struct bitmap {
+ cairo_surface_t *surface;
+ bool converted; /** set if the surface data has been converted */
+};
#endif /* NS_GTK_BITMAP_H */
Modified: branches/vince/netsurf-cairo/gtk/plotters.c
URL:
http://source.netsurf-browser.org/branches/vince/netsurf-cairo/gtk/plotte...
==============================================================================
--- branches/vince/netsurf-cairo/gtk/plotters.c (original)
+++ branches/vince/netsurf-cairo/gtk/plotters.c Thu Dec 22 18:48:21 2011
@@ -391,7 +391,7 @@
bool repeat_x = (flags & BITMAPF_REPEAT_X);
bool repeat_y = (flags & BITMAPF_REPEAT_Y);
- cairo_surface_t *bmsurface = (cairo_surface_t *)bitmap;
+ cairo_surface_t *bmsurface = bitmap->surface;
if (!(repeat_x || repeat_y)) {
/* Not repeating at all, so just pass it on */
Modified: branches/vince/netsurf-cairo/gtk/thumbnail.c
URL:
http://source.netsurf-browser.org/branches/vince/netsurf-cairo/gtk/thumbn...
==============================================================================
--- branches/vince/netsurf-cairo/gtk/thumbnail.c (original)
+++ branches/vince/netsurf-cairo/gtk/thumbnail.c Thu Dec 22 18:48:21 2011
@@ -52,7 +52,7 @@
bool thumbnail_create(hlcache_handle *content, struct bitmap *bitmap,
const char *url)
{
- cairo_surface_t *dsurface = (cairo_surface_t *)bitmap;
+ cairo_surface_t *dsurface = bitmap->surface;
cairo_surface_t *surface;
cairo_t *old_cr;
gint dwidth, dheight;
Modified: branches/vince/netsurf-cairo/image/image.c
URL:
http://source.netsurf-browser.org/branches/vince/netsurf-cairo/image/imag...
==============================================================================
--- branches/vince/netsurf-cairo/image/image.c (original)
+++ branches/vince/netsurf-cairo/image/image.c Thu Dec 22 18:48:21 2011
@@ -154,7 +154,7 @@
fill_style.stroke_type = PLOT_OP_TYPE_NONE;
fill_style.fill_type = PLOT_OP_TYPE_SOLID;
- LOG(("area %d,%d -> %d,%d", area.x0, area.y0, area.x1, area.y1));
+ /* LOG(("area %d,%d -> %d,%d", area.x0, area.y0, area.x1, area.y1)); */
return ctx->plot->rectangle(area.x0, area.y0,
area.x1, area.y1,
&fill_style);