| 1 |
|
| 2 |
|
| 3 |
|
| 4 |
|
| 5 |
|
| 6 |
|
| 7 |
|
| 8 |
|
| 9 |
|
| 10 |
|
| 11 |
|
| 12 |
|
| 13 |
|
| 14 |
|
| 15 |
|
| 16 |
|
| 17 |
|
| 18 |
#ifdef HAVE_CONFIG_H |
| 19 |
#include "config.h" |
| 20 |
#endif |
| 21 |
|
| 22 |
#include "byzanzencodergif.h" |
| 23 |
|
| 24 |
#include <string.h> |
| 25 |
#include <glib/gi18n.h> |
| 26 |
|
| 27 |
#include "gifenc.h" |
| 28 |
|
| 29 |
G_DEFINE_TYPE (ByzanzEncoderGif, byzanz_encoder_gif, BYZANZ_TYPE_ENCODER) |
| 30 |
|
| 31 |
static gboolean |
| 32 |
byzanz_encoder_write_data (gpointer closure, |
| 33 |
const guchar * data, |
| 34 |
gsize len, |
| 35 |
GError ** error) |
| 36 |
{ |
| 37 |
ByzanzEncoder *encoder = closure; |
| 38 |
|
| 39 |
return g_output_stream_write_all (encoder->output_stream, data, len, |
| 40 |
NULL, encoder->cancellable, error); |
| 41 |
} |
| 42 |
|
| 43 |
static gboolean |
| 44 |
byzanz_encoder_gif_setup (ByzanzEncoder * encoder, |
| 45 |
GOutputStream * stream, |
| 46 |
guint width, |
| 47 |
guint height, |
| 48 |
GCancellable * cancellable, |
| 49 |
GError ** error) |
| 50 |
{ |
| 51 |
ByzanzEncoderGif *gif = BYZANZ_ENCODER_GIF (encoder); |
| 52 |
|
| 53 |
gif->gifenc = gifenc_new (width, height, byzanz_encoder_write_data, encoder, NULL); |
| 54 |
|
| 55 |
gif->image_data = g_malloc (width * height); |
| 56 |
gif->cached_data = g_malloc (width * height); |
| 57 |
gif->cached_tmp = g_malloc (width * height); |
| 58 |
return TRUE; |
| 59 |
} |
| 60 |
|
| 61 |
static gboolean |
| 62 |
byzanz_encoder_gif_quantize (ByzanzEncoderGif * gif, |
| 63 |
cairo_surface_t * surface, |
| 64 |
GError ** error) |
| 65 |
{ |
| 66 |
GifencPalette *palette; |
| 67 |
|
| 68 |
g_assert (!gif->has_quantized); |
| 69 |
|
| 70 |
palette = gifenc_quantize_image (cairo_image_surface_get_data (surface), |
| 71 |
cairo_image_surface_get_width (surface), cairo_image_surface_get_height (surface), |
| 72 |
cairo_image_surface_get_stride (surface), TRUE, 255); |
| 73 |
|
| 74 |
if (!gifenc_initialize (gif->gifenc, palette, TRUE, error)) |
| 75 |
return FALSE; |
| 76 |
|
| 77 |
memset (gif->image_data, |
| 78 |
gifenc_palette_get_alpha_index (palette), |
| 79 |
gifenc_get_width (gif->gifenc) * gifenc_get_height (gif->gifenc)); |
| 80 |
|
| 81 |
gif->has_quantized = TRUE; |
| 82 |
return TRUE; |
| 83 |
} |
| 84 |
|
| 85 |
static gboolean |
| 86 |
byzanz_encoder_write_image (ByzanzEncoderGif *gif, guint64 msecs, GError **error) |
| 87 |
{ |
| 88 |
guint elapsed; |
| 89 |
guint width; |
| 90 |
|
| 91 |
g_assert (gif->cached_data != NULL); |
| 92 |
g_assert (gif->cached_area.width > 0); |
| 93 |
g_assert (gif->cached_area.height > 0); |
| 94 |
|
| 95 |
width = gifenc_get_width (gif->gifenc); |
| 96 |
elapsed = msecs - gif->cached_time; |
| 97 |
elapsed = MAX (elapsed, 10); |
| 98 |
|
| 99 |
if (!gifenc_add_image (gif->gifenc, gif->cached_area.x, gif->cached_area.y, |
| 100 |
gif->cached_area.width, gif->cached_area.height, elapsed, |
| 101 |
gif->cached_data + width * gif->cached_area.y + gif->cached_area.x, |
| 102 |
width, error)) |
| 103 |
return FALSE; |
| 104 |
|
| 105 |
gif->cached_time = msecs; |
| 106 |
return TRUE; |
| 107 |
} |
| 108 |
|
| 109 |
static gboolean |
| 110 |
byzanz_encoder_gif_encode_image (ByzanzEncoderGif * gif, |
| 111 |
cairo_surface_t * surface, |
| 112 |
const cairo_region_t * region, |
| 113 |
cairo_rectangle_int_t * area_out) |
| 114 |
{ |
| 115 |
cairo_rectangle_int_t extents, area, rect; |
| 116 |
guint8 transparent; |
| 117 |
guint i, n_rects, stride, width; |
| 118 |
|
| 119 |
cairo_region_get_extents (region, &extents); |
| 120 |
transparent = gifenc_palette_get_alpha_index (gif->gifenc->palette); |
| 121 |
stride = cairo_image_surface_get_stride (surface); |
| 122 |
width = gifenc_get_width (gif->gifenc); |
| 123 |
|
| 124 |
|
| 125 |
|
| 126 |
for (i = extents.y; i < (guint) (extents.y + extents.height); i++) { |
| 127 |
memset (gif->cached_tmp + width * i + extents.x, transparent, extents.width); |
| 128 |
} |
| 129 |
|
| 130 |
|
| 131 |
n_rects = cairo_region_num_rectangles (region); |
| 132 |
memset (area_out, 0, sizeof (cairo_rectangle_int_t)); |
| 133 |
for (i = 0; i < n_rects; i++) { |
| 134 |
cairo_region_get_rectangle (region, i, &rect); |
| 135 |
if (gifenc_dither_rgb_with_full_image ( |
| 136 |
gif->cached_tmp + width * rect.y + rect.x, width, |
| 137 |
gif->image_data + width * rect.y + rect.x, width, |
| 138 |
gif->gifenc->palette, |
| 139 |
cairo_image_surface_get_data (surface) + (rect.x - extents.x) * 4 |
| 140 |
+ (rect.y - extents.y) * stride, |
| 141 |
rect.width, rect.height, stride, &area)) { |
| 142 |
area.x += rect.x; |
| 143 |
area.y += rect.y; |
| 144 |
if (area_out->width > 0 && area_out->height > 0) |
| 145 |
gdk_rectangle_union ((const GdkRectangle*)area_out, (const GdkRectangle*) &area, (GdkRectangle*)area_out); |
| 146 |
else |
| 147 |
*area_out = area; |
| 148 |
} |
| 149 |
} |
| 150 |
|
| 151 |
return area_out->width > 0 && area_out->height > 0; |
| 152 |
} |
| 153 |
|
| 154 |
static void |
| 155 |
byzanz_encoder_swap_image (ByzanzEncoderGif * gif, |
| 156 |
cairo_rectangle_int_t * area) |
| 157 |
{ |
| 158 |
guint8 *swap; |
| 159 |
|
| 160 |
swap = gif->cached_data; |
| 161 |
gif->cached_data = gif->cached_tmp; |
| 162 |
gif->cached_tmp = swap; |
| 163 |
gif->cached_area = *area; |
| 164 |
} |
| 165 |
|
| 166 |
static gboolean |
| 167 |
byzanz_encoder_gif_process (ByzanzEncoder * encoder, |
| 168 |
GOutputStream * stream, |
| 169 |
guint64 msecs, |
| 170 |
cairo_surface_t * surface, |
| 171 |
const cairo_region_t * region, |
| 172 |
GCancellable * cancellable, |
| 173 |
GError ** error) |
| 174 |
{ |
| 175 |
ByzanzEncoderGif *gif = BYZANZ_ENCODER_GIF (encoder); |
| 176 |
cairo_rectangle_int_t area; |
| 177 |
|
| 178 |
if (!gif->has_quantized) { |
| 179 |
if (!byzanz_encoder_gif_quantize (gif, surface, error)) |
| 180 |
return FALSE; |
| 181 |
gif->cached_time = msecs; |
| 182 |
if (!byzanz_encoder_gif_encode_image (gif, surface, region, &area)) { |
| 183 |
g_assert_not_reached (); |
| 184 |
} |
| 185 |
byzanz_encoder_swap_image (gif, &area); |
| 186 |
} else { |
| 187 |
if (byzanz_encoder_gif_encode_image (gif, surface, region, &area)) { |
| 188 |
if (!byzanz_encoder_write_image (gif, msecs, error)) |
| 189 |
return FALSE; |
| 190 |
byzanz_encoder_swap_image (gif, &area); |
| 191 |
} |
| 192 |
} |
| 193 |
|
| 194 |
return TRUE; |
| 195 |
} |
| 196 |
|
| 197 |
static gboolean |
| 198 |
byzanz_encoder_gif_close (ByzanzEncoder * encoder, |
| 199 |
GOutputStream * stream, |
| 200 |
guint64 msecs, |
| 201 |
GCancellable * cancellable, |
| 202 |
GError ** error) |
| 203 |
{ |
| 204 |
ByzanzEncoderGif *gif = BYZANZ_ENCODER_GIF (encoder); |
| 205 |
|
| 206 |
if (!gif->has_quantized) { |
| 207 |
g_set_error_literal (error, G_IO_ERROR, G_IO_ERROR_FAILED, _("No image to encode.")); |
| 208 |
return FALSE; |
| 209 |
} |
| 210 |
|
| 211 |
if (!byzanz_encoder_write_image (gif, msecs, error) || |
| 212 |
!gifenc_close (gif->gifenc, error)) |
| 213 |
return FALSE; |
| 214 |
|
| 215 |
return TRUE; |
| 216 |
} |
| 217 |
|
| 218 |
static void |
| 219 |
byzanz_encoder_gif_finalize (GObject *object) |
| 220 |
{ |
| 221 |
ByzanzEncoderGif *gif = BYZANZ_ENCODER_GIF (object); |
| 222 |
|
| 223 |
g_free (gif->image_data); |
| 224 |
g_free (gif->cached_data); |
| 225 |
g_free (gif->cached_tmp); |
| 226 |
if (gif->gifenc) |
| 227 |
gifenc_free (gif->gifenc); |
| 228 |
|
| 229 |
G_OBJECT_CLASS (byzanz_encoder_gif_parent_class)->finalize (object); |
| 230 |
} |
| 231 |
|
| 232 |
static void |
| 233 |
byzanz_encoder_gif_class_init (ByzanzEncoderGifClass *klass) |
| 234 |
{ |
| 235 |
GObjectClass *object_class = G_OBJECT_CLASS (klass); |
| 236 |
ByzanzEncoderClass *encoder_class = BYZANZ_ENCODER_CLASS (klass); |
| 237 |
|
| 238 |
object_class->finalize = byzanz_encoder_gif_finalize; |
| 239 |
|
| 240 |
encoder_class->setup = byzanz_encoder_gif_setup; |
| 241 |
encoder_class->process = byzanz_encoder_gif_process; |
| 242 |
encoder_class->close = byzanz_encoder_gif_close; |
| 243 |
|
| 244 |
encoder_class->filter = gtk_file_filter_new (); |
| 245 |
g_object_ref_sink (encoder_class->filter); |
| 246 |
gtk_file_filter_set_name (encoder_class->filter, _("GIF images")); |
| 247 |
gtk_file_filter_add_mime_type (encoder_class->filter, "image/gif"); |
| 248 |
gtk_file_filter_add_pattern (encoder_class->filter, "*.gif"); |
| 249 |
} |
| 250 |
|
| 251 |
static void |
| 252 |
byzanz_encoder_gif_init (ByzanzEncoderGif *encoder_gif) |
| 253 |
{ |
| 254 |
} |
| 255 |
|