|
|
531f67 |
|
|
|
531f67 |
#include "surface.h"
|
|
|
531f67 |
|
|
|
531f67 |
|
|
|
531f67 |
Surface::Surface():
|
|
|
531f67 |
m_width(), m_height(), m_pitch(), m_origin() { }
|
|
|
531f67 |
|
|
|
531f67 |
Surface::Surface(const Surface &):
|
|
|
531f67 |
m_width(), m_height(), m_pitch(), m_origin() { }
|
|
|
531f67 |
|
|
|
531f67 |
void
|
|
|
531f67 |
Surface::reset() {
|
|
|
531f67 |
m_width = m_height = m_pitch = 0;
|
|
|
531f67 |
m_origin = NULL;
|
|
|
531f67 |
}
|
|
|
531f67 |
|
|
|
4dc49c |
void
|
|
|
4dc49c |
Surface::mult_alpha() {
|
|
|
4dc49c |
if (empty()) return;
|
|
|
4dc49c |
const int h = height();
|
|
|
4dc49c |
const int w = width();
|
|
|
4dc49c |
const int dr = pitch() - width();
|
|
|
4dc49c |
Color *c = m_origin;
|
|
|
4dc49c |
for(int r = 0; r < h; ++r, c += dr)
|
|
|
4dc49c |
for(Color *end = c + w; c != end; ++c)
|
|
|
4dc49c |
c->mult_alpha();
|
|
|
4dc49c |
}
|
|
|
4dc49c |
|
|
|
4dc49c |
void
|
|
|
4dc49c |
Surface::demult_alpha() {
|
|
|
4dc49c |
if (empty()) return;
|
|
|
4dc49c |
const int h = height();
|
|
|
4dc49c |
const int w = width();
|
|
|
4dc49c |
const int dr = pitch() - width();
|
|
|
4dc49c |
Color *c = m_origin;
|
|
|
4dc49c |
for(int r = 0; r < h; ++r, c += dr)
|
|
|
4dc49c |
for(Color *end = c + w; c != end; ++c)
|
|
|
4dc49c |
c->demult_alpha();
|
|
|
4dc49c |
}
|
|
|
4dc49c |
|
|
|
4dc49c |
Color
|
|
|
4dc49c |
Surface::get_pixel(Real x, Real y) const {
|
|
|
4dc49c |
// linear interpolation
|
|
|
4dc49c |
if ( empty()
|
|
|
4dc49c |
|| x < 0 || x > width() - 1
|
|
|
4dc49c |
|| y < 0 || y > height() - 1 )
|
|
|
4dc49c |
return Color();
|
|
|
4dc49c |
|
|
|
4dc49c |
int x0 = (int)floor(x), x1 = x0 + 1;
|
|
|
4dc49c |
int y0 = (int)floor(y), y1 = y0 + 1;
|
|
|
4dc49c |
Real px = x - x0, ppx = 1.0 - px;
|
|
|
4dc49c |
Real py = y - y0, ppy = 1.0 - py;
|
|
|
4dc49c |
|
|
|
4dc49c |
x0 = iclamp(x0, 0, width() - 1);
|
|
|
4dc49c |
x1 = iclamp(x1, 0, width() - 1);
|
|
|
4dc49c |
y0 = iclamp(y0, 0, height() - 1);
|
|
|
4dc49c |
y1 = iclamp(y1, 0, height() - 1);
|
|
|
4dc49c |
|
|
|
4dc49c |
Real k[] = { ppx*ppy, px*ppy, ppx*py, px*py };
|
|
|
4dc49c |
const Color *corners[] = { &row(y0)[x0], &row(y0)[x1], &row(y1)[x0], &row(y1)[x1] };
|
|
|
4dc49c |
Color sum;
|
|
|
4dc49c |
for(int i = 0; i < 4; ++i) {
|
|
|
4dc49c |
const Color &c = *corners[i];
|
|
|
4dc49c |
Real a = c.a * k[i];
|
|
|
4dc49c |
for(int j = 0; j < 3; ++j)
|
|
|
4dc49c |
sum.channels[j] += c.channels[j]*a;
|
|
|
4dc49c |
sum.a += a;
|
|
|
4dc49c |
}
|
|
|
4dc49c |
|
|
|
4dc49c |
if (sum.a <= real_precision)
|
|
|
4dc49c |
return Color();
|
|
|
4dc49c |
|
|
|
4dc49c |
Real aa = 1.0/sum.a;
|
|
|
4dc49c |
for(int j = 0; j < 3; ++j)
|
|
|
4dc49c |
sum.channels[j] *= aa;
|
|
|
4dc49c |
return sum;
|
|
|
4dc49c |
}
|
|
|
4dc49c |
|
|
|
4dc49c |
Color
|
|
|
4dc49c |
Surface::get_pixel_premulted(Real x, Real y) const {
|
|
|
4dc49c |
// linear interpolation
|
|
|
4dc49c |
if ( empty()
|
|
|
4dc49c |
|| x < 0 || x > width() - 1
|
|
|
4dc49c |
|| y < 0 || y > height() - 1 )
|
|
|
4dc49c |
return Color();
|
|
|
4dc49c |
|
|
|
4dc49c |
int x0 = (int)floor(x), x1 = x0 + 1;
|
|
|
4dc49c |
int y0 = (int)floor(y), y1 = y0 + 1;
|
|
|
4dc49c |
Real px = x - x0, ppx = 1.0 - px;
|
|
|
4dc49c |
Real py = y - y0, ppy = 1.0 - py;
|
|
|
4dc49c |
|
|
|
4dc49c |
x0 = iclamp(x0, 0, width() - 1);
|
|
|
4dc49c |
x1 = iclamp(x1, 0, width() - 1);
|
|
|
4dc49c |
y0 = iclamp(y0, 0, height() - 1);
|
|
|
4dc49c |
y1 = iclamp(y1, 0, height() - 1);
|
|
|
4dc49c |
|
|
|
4dc49c |
Color sum;
|
|
|
4dc49c |
Real k[] = { ppx*ppy, px*ppy, ppx*py, px*py };
|
|
|
4dc49c |
const Color *corners[] = { &row(y0)[x0], &row(y0)[x1], &row(y1)[x0], &row(y1)[x1] };
|
|
|
4dc49c |
for(int i = 0; i < 4; ++i)
|
|
|
4dc49c |
for(int j = 0; j < 4; ++j)
|
|
|
4dc49c |
sum.channels[j] += corners[i]->channels[j]*k[i];
|
|
|
4dc49c |
return sum;
|
|
|
4dc49c |
}
|
|
|
4dc49c |
|
|
|
531f67 |
void Surface::copy(int width, int height, const Color *src_origin, Color *dest_origin, int src_pitch, int dest_pitch) {
|
|
|
531f67 |
#ifndef NDEBUG
|
|
|
531f67 |
{ // check source and destination ranges
|
|
|
531f67 |
assert(dest_origin);
|
|
|
531f67 |
assert(src_origin);
|
|
|
531f67 |
|
|
|
531f67 |
Color *dest_begin = dest_origin + begin_offset(width, height, dest_pitch),
|
|
|
531f67 |
*dest_end = dest_origin + end_offset(width, height, dest_pitch);
|
|
|
531f67 |
const Color *src_begin = src_origin + begin_offset(width, height, src_pitch),
|
|
|
531f67 |
*src_end = src_origin + end_offset(width, height, src_pitch);
|
|
|
531f67 |
assert(src_end <= dest_begin || dest_end <= src_begin);
|
|
|
531f67 |
}
|
|
|
531f67 |
#endif
|
|
|
531f67 |
|
|
|
531f67 |
if (!src_pitch) src_pitch = width;
|
|
|
531f67 |
if (!dest_pitch) dest_pitch = width;
|
|
|
531f67 |
|
|
|
531f67 |
const Color *src_row = src_origin;
|
|
|
531f67 |
for( Color *dest_row = dest_origin, *dest_row_end = dest_row + height*dest_pitch;
|
|
|
531f67 |
dest_row != dest_row_end;
|
|
|
531f67 |
dest_row += dest_pitch, src_row += src_pitch )
|
|
|
531f67 |
{
|
|
|
531f67 |
const Color *src_col = src_row;
|
|
|
531f67 |
for( Color *dest_col = dest_row, *dest_col_end = dest_col + width;
|
|
|
531f67 |
dest_col != dest_col_end;
|
|
|
531f67 |
++dest_col, ++src_col )
|
|
|
531f67 |
{ *dest_col = *src_col; }
|
|
|
531f67 |
}
|
|
|
531f67 |
}
|
|
|
531f67 |
|
|
|
a1942e |
Glib::RefPtr<gdk::pixbuf></gdk::pixbuf>
|
|
|
a1942e |
Surface::to_pixbuf() const {
|
|
|
a1942e |
if (empty())
|
|
|
a1942e |
return Glib::RefPtr<gdk::pixbuf>();</gdk::pixbuf>
|
|
|
a1942e |
Glib::RefPtr<gdk::pixbuf> pixbuf = Gdk::Pixbuf::create(</gdk::pixbuf>
|
|
|
a1942e |
Gdk::COLORSPACE_RGB, true, 8, width(), height() );
|
|
|
a1942e |
guint8 *pixels = pixbuf->get_pixels();
|
|
|
a1942e |
int stride = pixbuf->get_rowstride();
|
|
|
a1942e |
for(int r = 0; r < height(); ++r) {
|
|
|
a1942e |
const Color *src_row = row(r);
|
|
|
a1942e |
guint8 *pixel = pixels;
|
|
|
a1942e |
pixels += stride;
|
|
|
a1942e |
for(int c = 0; c < width(); ++c) {
|
|
|
a1942e |
const Color &color = src_row[c];
|
|
|
a1942e |
*(pixel++) = (guint8)(color.r*255.9);
|
|
|
a1942e |
*(pixel++) = (guint8)(color.g*255.9);
|
|
|
a1942e |
*(pixel++) = (guint8)(color.b*255.9);
|
|
|
a1942e |
*(pixel++) = (guint8)(color.a*255.9);
|
|
|
a1942e |
}
|
|
|
a1942e |
}
|
|
|
a1942e |
return pixbuf;
|
|
|
a1942e |
}
|
|
|
a1942e |
|
|
|
a1942e |
Cairo::RefPtr<cairo::imagesurface></cairo::imagesurface>
|
|
|
4dc49c |
Surface::to_cairo_surface(bool premulted) const {
|
|
|
a1942e |
if (empty())
|
|
|
a1942e |
return Cairo::RefPtr<cairo::imagesurface>();</cairo::imagesurface>
|
|
|
a1942e |
Cairo::RefPtr<cairo::imagesurface> cairo_surface =</cairo::imagesurface>
|
|
|
a1942e |
Cairo::ImageSurface::create(Cairo::FORMAT_ARGB32, width(), height());
|
|
|
a1942e |
cairo_surface->flush();
|
|
|
a1942e |
unsigned char *pixels = cairo_surface->get_data();
|
|
|
a1942e |
int stride = cairo_surface->get_stride();
|
|
|
a1942e |
for(int r = 0; r < height(); ++r) {
|
|
|
a1942e |
const Color *src_row = row(r);
|
|
|
a1942e |
unsigned char *pixel = pixels;
|
|
|
a1942e |
pixels += stride;
|
|
|
a1942e |
for(int c = 0; c < width(); ++c) {
|
|
|
4dc49c |
Color color = src_row[c];
|
|
|
4dc49c |
if (!premulted) color.mult_alpha();
|
|
|
a1942e |
*(pixel++) = (guint8)(color.r*255.9);
|
|
|
a1942e |
*(pixel++) = (guint8)(color.g*255.9);
|
|
|
a1942e |
*(pixel++) = (guint8)(color.b*255.9);
|
|
|
a1942e |
*(pixel++) = (guint8)(color.a*255.9);
|
|
|
a1942e |
}
|
|
|
a1942e |
}
|
|
|
a1942e |
cairo_surface->mark_dirty();
|
|
|
a1942e |
return cairo_surface;
|
|
|
a1942e |
}
|
|
|
a1942e |
|
|
|
531f67 |
|
|
|
531f67 |
void DataSurface::clear() {
|
|
|
531f67 |
if (Color *data = data_begin()) delete[] data;
|
|
|
531f67 |
reset();
|
|
|
531f67 |
}
|
|
|
531f67 |
|
|
|
531f67 |
void DataSurface::init(int width, int height, int pitch, const Color *src_origin, int src_pitch) {
|
|
|
531f67 |
Color *origin = NULL;
|
|
|
531f67 |
if (width > 0 && height > 0) {
|
|
|
531f67 |
if (!pitch) pitch = width;
|
|
|
531f67 |
|
|
|
531f67 |
int count = data_count(width, height, pitch);
|
|
|
531f67 |
int offset = begin_offset(width, height, pitch);
|
|
|
531f67 |
Color *data = new Color[count];
|
|
|
531f67 |
origin = data - offset;
|
|
|
531f67 |
|
|
|
531f67 |
if (src_origin)
|
|
|
531f67 |
copy(width, height, src_origin, origin, src_pitch, pitch);
|
|
|
531f67 |
} else {
|
|
|
531f67 |
width = 0;
|
|
|
531f67 |
height = 0;
|
|
|
531f67 |
pitch = 0;
|
|
|
531f67 |
}
|
|
|
531f67 |
|
|
|
531f67 |
clear();
|
|
|
531f67 |
m_width = width;
|
|
|
531f67 |
m_height = height;
|
|
|
531f67 |
m_pitch = pitch;
|
|
|
531f67 |
m_origin = origin;
|
|
|
531f67 |
}
|
|
|
531f67 |
|
|
|
531f67 |
|
|
|
531f67 |
void AliasSurface::init(Color *origin, int width, int height, int pitch) {
|
|
|
531f67 |
clear();
|
|
|
531f67 |
if (origin && width > 0 && height > 0) {
|
|
|
531f67 |
m_width = width;
|
|
|
531f67 |
m_height = height;
|
|
|
531f67 |
m_pitch = pitch ? pitch : width;
|
|
|
531f67 |
m_origin = origin;
|
|
|
531f67 |
}
|
|
|
531f67 |
}
|