예제 #1
0
/**
 * ppg_animation_class_init:
 * @klass: (in): A #PpgAnimationClass.
 *
 * Initializes the GObjectClass.
 *
 * Returns: None.
 * Side effects: Properties, signals, and vtables are initialized.
 */
static void
ppg_animation_class_init (PpgAnimationClass *klass)
{
	GObjectClass *object_class;

	debug = !!g_getenv("PPG_ANIMATION_DEBUG");

	object_class = G_OBJECT_CLASS(klass);
	object_class->dispose = ppg_animation_dispose;
	object_class->finalize = ppg_animation_finalize;
	object_class->set_property = ppg_animation_set_property;
	g_type_class_add_private(object_class, sizeof(PpgAnimationPrivate));

	g_object_class_install_property(object_class,
	                                PROP_DURATION,
	                                g_param_spec_uint("duration",
	                                                  "Duration",
	                                                  "The duration of the animation",
	                                                  0,
	                                                  G_MAXUINT,
	                                                  250,
	                                                  G_PARAM_WRITABLE | G_PARAM_CONSTRUCT_ONLY));

	g_object_class_install_property(object_class,
	                                PROP_MODE,
	                                g_param_spec_enum("mode",
	                                                  "Mode",
	                                                  "The animation mode",
	                                                  PPG_TYPE_ANIMATION_MODE,
	                                                  PPG_ANIMATION_LINEAR,
	                                                  G_PARAM_WRITABLE | G_PARAM_CONSTRUCT_ONLY));

	g_object_class_install_property(object_class,
	                                PROP_TARGET,
	                                g_param_spec_object("target",
	                                                    "Target",
	                                                    "The target of the animation",
	                                                    G_TYPE_OBJECT,
	                                                    G_PARAM_WRITABLE | G_PARAM_CONSTRUCT_ONLY));

	g_object_class_install_property(object_class,
	                                PROP_FRAME_RATE,
	                                g_param_spec_uint("frame-rate",
	                                                  "frame-rate",
	                                                  "frame-rate",
	                                                  1,
	                                                  G_MAXUINT,
	                                                  60,
	                                                  G_PARAM_WRITABLE | G_PARAM_CONSTRUCT_ONLY));

	signals[TICK] = g_signal_new("tick",
	                             PPG_TYPE_ANIMATION,
	                             G_SIGNAL_RUN_FIRST,
	                             0,
	                             NULL,
	                             NULL,
	                             g_cclosure_marshal_VOID__VOID,
	                             G_TYPE_NONE,
	                             0);

#define SET_ALPHA(_T, _t) \
	alpha_funcs[PPG_ANIMATION_##_T] = ppg_animation_alpha_##_t

	SET_ALPHA(LINEAR, linear);
	SET_ALPHA(EASE_IN_QUAD, ease_in_quad);
	SET_ALPHA(EASE_OUT_QUAD, ease_out_quad);
	SET_ALPHA(EASE_IN_OUT_QUAD, ease_in_out_quad);

#define SET_TWEEN(_T, _t) \
	G_STMT_START { \
		guint idx = G_TYPE_##_T; \
		tween_funcs[idx] = tween_##_t; \
	} G_STMT_END

	SET_TWEEN(INT, int);
	SET_TWEEN(UINT, uint);
	SET_TWEEN(LONG, long);
	SET_TWEEN(ULONG, ulong);
	SET_TWEEN(FLOAT, float);
	SET_TWEEN(DOUBLE, double);
}
/**
 * Given the RGB data for two image surfaces, one a source image composited
 * with OVER onto a black background, and one a source image composited with 
 * OVER onto a white background, reconstruct the original image data into
 * black_data.
 * 
 * Consider a single color channel and a given pixel. Suppose the original
 * premultiplied color value was C and the alpha value was A. Let the final
 * on-black color be B and the final on-white color be W. All values range
 * over 0-255.
 * Then B=C and W=(255*(255 - A) + C*255)/255. Solving for A, we get
 * A=255 - (W - C). Therefore it suffices to leave the black_data color
 * data alone and set the alpha values using that simple formula. It shouldn't
 * matter what color channel we pick for the alpha computation, but we'll
 * pick green because if we went through a color channel downsample the green
 * bits are likely to be the most accurate.
 */
static void
_compute_alpha_values (uint32_t *black_data,
                       uint32_t *white_data,
                       int width, int height,
                       cairo_xlib_drawing_result_t *analysis)
{
    int num_pixels = width*height;
    int i;
    uint32_t first;
    uint32_t deltas = 0;
    unsigned char first_alpha;
  
    if (num_pixels == 0) {
        if (analysis) {
            analysis->uniform_alpha = True;
            analysis->uniform_color = True;
            /* whatever we put here will be true */
            analysis->alpha = 1.0;
            analysis->r = analysis->g = analysis->b = 0.0;
        }
        return;
    }
  
    first_alpha = 255 - (GREEN_OF(*white_data) - GREEN_OF(*black_data));
    /* set the alpha value of 'first' */
    first = SET_ALPHA(*black_data, first_alpha);
  
    for (i = 0; i < num_pixels; ++i) {
        uint32_t black = *black_data;
        uint32_t white = *white_data;
        unsigned char pixel_alpha = 255 - (GREEN_OF(white) - GREEN_OF(black));
        
        black = SET_ALPHA(black, pixel_alpha);
        *black_data = black;
        deltas |= (first ^ black);
        
        black_data++;
        white_data++;
    }
    
    if (analysis) {
        analysis->uniform_alpha = (deltas >> 24) == 0;
        if (analysis->uniform_alpha) {
            analysis->alpha = first_alpha/255.0;
            /* we only set uniform_color when the alpha is already uniform.
               it's only useful in that case ... and if the alpha was nonuniform
               then computing whether the color is uniform would require unpremultiplying
               every pixel */
            analysis->uniform_color = (deltas & ~(0xFF << 24)) == 0;
            if (analysis->uniform_color) {
                if (first_alpha == 0) {
                    /* can't unpremultiply, this is OK */
                    analysis->r = analysis->g = analysis->b = 0.0;
                } else {
                    double d_first_alpha = first_alpha;
                    analysis->r = (first & 0xFF)/d_first_alpha;
                    analysis->g = ((first >> 8) & 0xFF)/d_first_alpha;
                    analysis->b = ((first >> 16) & 0xFF)/d_first_alpha;
                }
            }
        }
    }