summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorCarl Worth <cworth@cworth.org>2005-06-21 00:45:23 +0000
committerCarl Worth <cworth@cworth.org>2005-06-21 00:45:23 +0000
commit05d3a5a7110a7eb30b31927c0aac21d9200f471e (patch)
treef26537b5aaad2768dcc5e5c034482aadd7f3ac5b
parentf842a82a6bdc0d27e44c03963d15d911ac4cba73 (diff)
Update to work with cairo 0.5.0.
Remove in favor of cairo_surface_write_to_png.
-rw-r--r--ChangeLog13
-rw-r--r--Makefile.am2
-rw-r--r--fdclock.c43
-rw-r--r--fdface.c8
-rw-r--r--fdfacepng.c15
-rw-r--r--fdhand.c16
-rw-r--r--fdlogo.c13
-rw-r--r--write_png.c186
-rw-r--r--write_png.h12
9 files changed, 61 insertions, 247 deletions
diff --git a/ChangeLog b/ChangeLog
index dc02bd2..2930189 100644
--- a/ChangeLog
+++ b/ChangeLog
@@ -1,3 +1,16 @@
+2005-06-20 Carl Worth <cworth@cworth.org>
+
+ * fdclock.c: (clear), (make_background), (main_x):
+ * fdface.c: (draw_fancy_tick), (draw_plain_tick), (fdface_draw):
+ * fdfacepng.c: (dump_png):
+ * fdhand.c: (draw_hand), (draw_time), (fdhand_draw_now):
+ * fdlogo.c: (draw_outline), (draw_background), (draw_window_at),
+ (draw_windows): Update to work with cairo 0.5.0.
+
+ * Makefile.am:
+ * write_png.c:
+ * write_png.h: Remove in favor of cairo_surface_write_to_png.
+
2004-07-19 Eric Anholt <anholt@FreeBSD.org>
* configure.ac:
diff --git a/Makefile.am b/Makefile.am
index 4b46499..418d47b 100644
--- a/Makefile.am
+++ b/Makefile.am
@@ -7,7 +7,7 @@ fdclock_SOURCES = \
$(COMMON_SRCS)
fdfacepng_SOURCES = \
- fdfacepng.c write_png.c \
+ fdfacepng.c \
$(COMMON_SRCS)
bin_PROGRAMS = fdclock fdfacepng
diff --git a/fdclock.c b/fdclock.c
index cdef8c7..3d0d7c1 100644
--- a/fdclock.c
+++ b/fdclock.c
@@ -23,10 +23,13 @@
*/
#include <cairo.h>
+#include <cairo-xlib.h>
#include <X11/Xlib.h>
#include <X11/extensions/Xrender.h>
#include <sys/poll.h>
#include <math.h>
+#include <stdio.h>
+#include <stdlib.h>
#include <unistd.h>
#include "fdface.h"
#include "fdhand.h"
@@ -39,28 +42,29 @@ static void
clear (cairo_t *cr, double width, double height, double alpha)
{
cairo_save (cr);
- cairo_set_rgb_color (cr, 1, 1, 1);
- cairo_set_alpha (cr, alpha);
- cairo_set_operator (cr, CAIRO_OPERATOR_SRC);
+ cairo_set_source_rgba (cr, 1, 1, 1, alpha);
+ cairo_set_operator (cr, CAIRO_OPERATOR_SOURCE);
cairo_rectangle (cr, 0, 0, width, height);
cairo_fill (cr);
cairo_restore (cr);
}
-Pixmap
+static Pixmap
make_background (Display *dpy, Window root, int width, int height, int depth,
Visual *visual, Colormap cmap, Bool opaque)
{
- cairo_t *cr = cairo_create ();
+ cairo_t *cr;
cairo_surface_t *back_surface;
cairo_surface_t *temp_surface = 0;
+ cairo_surface_t *target_surface;
Pixmap background;
/*
* Background pixmap
*/
background = XCreatePixmap (dpy, root, width, height, depth);
- back_surface = cairo_xlib_surface_create (dpy, background, visual, 0, cmap);
+ back_surface = cairo_xlib_surface_create (dpy, background, visual, width, height);
+ target_surface = back_surface;
if (depth == 32)
{
@@ -71,10 +75,9 @@ make_background (Display *dpy, Window root, int width, int height, int depth,
* draw the background to the temporary surface
*/
- cairo_set_target_surface (cr, temp_surface);
+ target_surface = temp_surface;
}
- else
- cairo_set_target_surface (cr, back_surface);
+ cr = cairo_create (target_surface);
clear (cr, width, height, temp_surface ? 0 : 1);
@@ -82,8 +85,7 @@ make_background (Display *dpy, Window root, int width, int height, int depth,
{
cairo_save (cr);
{
- cairo_set_rgb_color (cr, 1, 1, 1);
- cairo_set_alpha (cr, opaque ? 1 : 0.5);
+ cairo_set_source_rgba (cr, 1, 1, 1, opaque ? 1 : 0.5);
cairo_scale (cr, width, height);
cairo_translate (cr, 0.5, 0.5);
cairo_arc (cr, 0, 0, 0.5, 0, 2 * M_PI);
@@ -96,13 +98,15 @@ make_background (Display *dpy, Window root, int width, int height, int depth,
if (temp_surface)
{
- cairo_set_target_surface (cr, back_surface);
+ cairo_t *cr2;
- clear (cr, width, height, 0);
+ cr2 = cairo_create (back_surface);
- cairo_set_alpha (cr, opaque ? 1 : 0.8);
+ clear (cr2, width, height, 0);
- cairo_show_surface (cr, temp_surface, width, height);
+ cairo_set_source_surface (cr2, temp_surface, 0, 0);
+ cairo_paint_with_alpha (cr2, opaque ? 1 : 0.8);
+ cairo_destroy (cr2);
cairo_surface_destroy (temp_surface);
}
cairo_surface_destroy (back_surface);
@@ -110,14 +114,14 @@ make_background (Display *dpy, Window root, int width, int height, int depth,
return background;
}
-void
+static void
main_x (char *dpy_name, char *geom, Bool seconds,
Bool translucent, Bool square, Bool opaque)
{
Display *dpy;
int scr;
int x = 0, y = 0;
- int width = CLOCK_WIDTH, height = CLOCK_HEIGHT;
+ unsigned int width = CLOCK_WIDTH, height = CLOCK_HEIGHT;
Window root;
Visual *visual;
XWMHints *wmhints;
@@ -135,7 +139,7 @@ main_x (char *dpy_name, char *geom, Bool seconds,
XEvent ev;
Bool paint = False;
int timeout;
- cairo_t *cr = cairo_create ();
+ cairo_t *cr;
dpy = XOpenDisplay (dpy_name);
if (!dpy)
@@ -265,12 +269,13 @@ main_x (char *dpy_name, char *geom, Bool seconds,
u_width, u_height, depth);
buffer_surface = cairo_xlib_surface_create (dpy, buffer,
visual, 0, cmap);
- cairo_set_target_surface (cr, buffer_surface);
+ cr = cairo_create (buffer_surface);
XCopyArea (dpy, background, buffer, gc,
0, 0, u_width, u_height, 0, 0);
fdhand_draw_now (cr, u_width, u_height, seconds);
XCopyArea (dpy, buffer, w, gc,
0, 0, u_width, u_height, x_off, y_off);
+ cairo_destroy (cr);
cairo_surface_destroy (buffer_surface);
XFreePixmap (dpy, buffer);
paint = False;
diff --git a/fdface.c b/fdface.c
index cd2a993..fcf7f08 100644
--- a/fdface.c
+++ b/fdface.c
@@ -29,9 +29,9 @@ draw_fancy_tick (cairo_t *cr, double radius)
{
cairo_save (cr);
cairo_arc (cr, 0, 0, radius, 0, 2 * M_PI);
- cairo_set_rgb_color (cr, 0.231, 0.502, 0.682);
+ cairo_set_source_rgb (cr, 0.231, 0.502, 0.682);
cairo_fill (cr);
- cairo_set_rgb_color (cr, 0.73, 0.73, 0.73);
+ cairo_set_source_rgb (cr, 0.73, 0.73, 0.73);
cairo_set_line_width (cr, radius * 2 / 3);
cairo_arc (cr, 0, 0, radius * 2, 0, 2 * M_PI);
cairo_stroke (cr);
@@ -43,7 +43,7 @@ draw_plain_tick (cairo_t *cr, double radius)
{
cairo_save (cr);
cairo_arc (cr, 0, 0, radius, 0, 2 * M_PI);
- cairo_set_rgb_color (cr, 0.73, 0.73, 0.73);
+ cairo_set_source_rgb (cr, 0.73, 0.73, 0.73);
cairo_fill (cr);
cairo_restore (cr);
}
@@ -51,8 +51,6 @@ draw_plain_tick (cairo_t *cr, double radius)
void
fdface_draw (cairo_t *cr, double width, double height)
{
- double x_off, y_off;
- double size;
int minute;
cairo_save (cr);
diff --git a/fdfacepng.c b/fdfacepng.c
index da72cf9..d1786ba 100644
--- a/fdfacepng.c
+++ b/fdfacepng.c
@@ -22,29 +22,30 @@
* PERFORMANCE OF THIS SOFTWARE.
*/
+#include <stdio.h>
#include <cairo.h>
#include "fdface.h"
static int
dump_png (int width, int height)
{
- int stride = width * 4;
- cairo_t *cr = cairo_create ();;
- char *image = calloc (stride * height, 1);
+ cairo_t *cr;
+ cairo_surface_t *surface;
char out[1024];
- cairo_set_target_image (cr, image, CAIRO_FORMAT_ARGB32,
- width, height, stride);
+ surface = cairo_image_surface_create (CAIRO_FORMAT_ARGB32,
+ width, height);
+ cr = cairo_create (surface);
cairo_move_to (cr, 0, 0);
cairo_line_to (cr, width, 0);
cairo_line_to (cr, width, height);
cairo_line_to (cr, 0, height);
cairo_close_path (cr);
- cairo_set_rgb_color (cr, 1, 1, 1);
+ cairo_set_source_rgb (cr, 1, 1, 1);
cairo_fill (cr);
fdface_draw (cr, width, height);
sprintf (out, "freedesktop-clock-%d.png", width);
- write_png_argb32 (image, out, width, height, stride);
+ cairo_surface_write_to_png (surface, out);
cairo_destroy (cr);
return 0;
diff --git a/fdhand.c b/fdhand.c
index a6f84d3..42ffc7f 100644
--- a/fdhand.c
+++ b/fdhand.c
@@ -74,15 +74,14 @@ static void
draw_hand (cairo_t *cr, double noon_deg, double width, double length, double alt,
void (*draw) (cairo_t *cr, double width, double length))
{
- cairo_matrix_t *m = cairo_matrix_create ();
- cairo_current_matrix (cr, m);
+ cairo_matrix_t m;
+ cairo_get_matrix (cr, &m);
{
cairo_translate (cr, alt/2, alt);
cairo_rotate (cr, noon_deg * M_PI / 180.0 - M_PI_2);
(*draw) (cr, width, length);
}
- cairo_set_matrix (cr, m);
- cairo_matrix_destroy (m);
+ cairo_set_matrix (cr, &m);
}
#define HOUR_WIDTH 0.03
@@ -120,12 +119,10 @@ draw_time (cairo_t *cr, double width, double height, struct timeval *tv, int sec
if (seconds)
draw_hand (cr, second_angle, SECOND_WIDTH, SECOND_LENGTH,
SECOND_ALT, draw_second);
- cairo_set_rgb_color (cr, 0, 0, 0);
- cairo_set_alpha (cr, 0.3);
+ cairo_set_source_rgba (cr, 0, 0, 0, 0.3);
cairo_fill (cr);
- cairo_set_rgb_color (cr, 0, 0, 0);
- cairo_set_alpha (cr, 1);
+ cairo_set_source_rgba (cr, 0, 0, 0, 1);
draw_hand (cr, hour_angle, HOUR_WIDTH, HOUR_LENGTH,
0.0, draw_hour);
cairo_fill (cr);
@@ -144,8 +141,7 @@ void
fdhand_draw_now (cairo_t *cr, double width, double height, int seconds)
{
struct timeval tv;
- struct timezone tz;
- gettimeofday (&tv, &tz);
+ gettimeofday (&tv, NULL);
draw_time (cr, width, height, &tv, seconds);
}
diff --git a/fdlogo.c b/fdlogo.c
index d758376..21dbfb5 100644
--- a/fdlogo.c
+++ b/fdlogo.c
@@ -56,7 +56,7 @@ draw_boundary (cairo_t *cr)
static void
draw_outline (cairo_t *cr)
{
- cairo_set_rgb_color (cr, 0.73, 0.73, 0.73);
+ cairo_set_source_rgb (cr, 0.73, 0.73, 0.73);
cairo_set_line_width (cr, 2);
draw_boundary (cr);
cairo_stroke (cr);
@@ -66,7 +66,7 @@ static void
draw_background (cairo_t *cr)
{
cairo_save (cr);
- cairo_set_rgb_color (cr, 0.231, 0.502, 0.682);
+ cairo_set_source_rgb (cr, 0.231, 0.502, 0.682);
cairo_translate (cr, 3.5, 3.5);
cairo_scale (cr, 0.887, 0.848);
draw_boundary (cr);
@@ -115,18 +115,18 @@ draw_window_at (cairo_t *cr, double x, double y, double scale)
draw_window (cr);
cairo_save (cr);
{
- cairo_set_rgb_color (cr, 1, 1, 1);
+ cairo_set_source_rgb (cr, 1, 1, 1);
cairo_fill (cr);
}
cairo_restore (cr);
- cairo_set_rgb_color (cr, 0.231, 0.502, 0.682);
+ cairo_set_source_rgb (cr, 0.231, 0.502, 0.682);
cairo_scale (cr, 1/scale, 1/scale);
cairo_stroke (cr);
}
cairo_restore (cr);
}
-void
+static void
draw_windows (cairo_t *cr)
{
cairo_save (cr);
@@ -135,8 +135,7 @@ draw_windows (cairo_t *cr)
cairo_line_to (cr, 48.25, 20.375);
cairo_line_to (cr, 30.25, 35.825);
cairo_close_path (cr);
- cairo_set_rgb_color (cr, 1, 1, 1);
- cairo_set_alpha (cr, 0.5);
+ cairo_set_source_rgba (cr, 1, 1, 1, 0.5);
cairo_stroke (cr);
}
cairo_restore (cr);
diff --git a/write_png.c b/write_png.c
deleted file mode 100644
index fd8f060..0000000
--- a/write_png.c
+++ /dev/null
@@ -1,186 +0,0 @@
-#include <stdio.h>
-#include <png.h>
-#include <stdlib.h>
-
-#include "write_png.h"
-
-static void
-unpremultiply_data (png_structp png, png_row_infop row_info, png_bytep data)
-{
- int i;
-
- for (i = 0; i < row_info->rowbytes; i += 4) {
- unsigned char *b = &data[i];
- unsigned char alpha = b[3];
- unsigned long pixel;
- unsigned long *p;
- if (alpha == 0)
- pixel = 0;
- else
- pixel = ((((b[0] * 255) / alpha) << 0) |
- (((b[1] * 255) / alpha) << 8) |
- (((b[2] * 255) / alpha) << 16) |
- (alpha << 24));
- p = (unsigned long *) b;
- *p = pixel;
- }
-}
-
-static void
-premultiply_data (png_structp png, png_row_infop row_info, png_bytep data)
-{
- int i;
-
- for (i = 0; i < row_info->rowbytes; i += 4) {
- unsigned char *base = &data[i];
- unsigned char blue = base[0];
- unsigned char green = base[1];
- unsigned char red = base[2];
- unsigned char alpha = base[3];
- unsigned long p;
-
- red = (unsigned) red * (unsigned) alpha / 255;
- green = (unsigned) green * (unsigned) alpha / 255;
- blue = (unsigned) blue * (unsigned) alpha / 255;
- p = (alpha << 24) | (red << 16) | (green << 8) | (blue << 0);
- memcpy (base, &p, sizeof (unsigned long));
- }
-}
-
-void
-write_png_argb32 (char *buffer, char* filename,
- int width, int height, int stride)
-{
- FILE* f;
- png_struct* png;
- png_info* info;
- png_byte** rows;
- int i;
- png_color_16 white;
-
- f = fopen (filename, "w");
- rows = malloc (height*sizeof (png_byte*));
-
- for (i=0;i<height;i++) {
- rows[i]=buffer+i*stride;
- }
-
- png = png_create_write_struct (PNG_LIBPNG_VER_STRING, NULL, NULL, NULL);
- info = png_create_info_struct (png);
-
- png_init_io (png, f);
- png_set_IHDR (png, info,
- width, height, 8,
- PNG_COLOR_TYPE_RGB_ALPHA,
- PNG_INTERLACE_NONE,
- PNG_COMPRESSION_TYPE_DEFAULT,
- PNG_FILTER_TYPE_DEFAULT);
-
- white.red=0xff;
- white.blue=0xff;
- white.green=0xff;
- png_set_bKGD (png, info, &white);
-
- png_set_write_user_transform_fn (png, unpremultiply_data);
- png_set_bgr (png);
-
- png_write_info (png, info);
- png_write_image (png, rows);
- png_write_end (png, info);
-
- png_destroy_write_struct (&png, &info);
-
- free (rows);
- fclose (f);
-}
-
-char *
-read_png_argb32 (char *filename,
- int *widthp, int *heightp, int *stridep)
-{
- FILE *f;
- char *buffer;
- png_structp png;
- png_infop info;
- png_bytepp rows;
- int i;
- png_uint_32 width, height;
- png_uint_32 stride;
- int depth, color, interlace;
-
- png = png_create_read_struct (PNG_LIBPNG_VER_STRING, NULL, NULL, NULL);
- if (png == NULL)
- return NULL;
- info = png_create_info_struct (png);
- if (info == NULL)
- {
- png_destroy_read_struct (&png, NULL, NULL);
- return NULL;
- }
- if (setjmp (png->jmpbuf))
- {
- png_destroy_read_struct (&png, &info, NULL);
- return NULL;
- }
- f = fopen (filename, "rb");
- if (f == NULL)
- {
- png_destroy_read_struct (&png, &info, NULL);
- return NULL;
- }
- png_init_io (png, f);
- png_read_info (png, info);
- png_get_IHDR (png, info, &width, &height, &depth, &color, &interlace,
- NULL, NULL);
-
- if (color == PNG_COLOR_TYPE_PALETTE && depth <= 8)
- png_set_expand (png);
-
- if (color == PNG_COLOR_TYPE_GRAY && depth < 8)
- png_set_expand (png);
-
- if (png_get_valid (png, info, PNG_INFO_tRNS))
- png_set_expand (png);
-
- if (depth == 16)
- png_set_strip_16 (png);
-
- if (depth < 8)
- png_set_packing (png);
-
- if (color == PNG_COLOR_TYPE_GRAY || color == PNG_COLOR_TYPE_GRAY_ALPHA)
- png_set_gray_to_rgb (png);
-
- if (interlace != PNG_INTERLACE_NONE)
- png_set_interlace_handling (png);
-
- png_set_bgr (png);
- png_set_filler (png, 255, PNG_FILLER_AFTER);
-
- png_set_read_user_transform_fn (png, premultiply_data);
-
- png_read_update_info (png, info);
-
- stride = width * 4;
- buffer = malloc (stride * height);
-
- rows = malloc (sizeof (png_bytep) * height);
-
- for (i = 0; i < height; i++)
- rows[i] = (png_bytep) (buffer + i * stride);
-
- png_read_image (png, rows);
- png_read_end (png, info);
-
- free (rows);
- fclose (f);
- png_destroy_read_struct (&png, &info, NULL);
-
- *widthp = (int) width;
- *heightp = (int) height;
- *stridep = (int) stride;
-
- return buffer;
-}
-
-
diff --git a/write_png.h b/write_png.h
deleted file mode 100644
index cab9882..0000000
--- a/write_png.h
+++ /dev/null
@@ -1,12 +0,0 @@
-#ifndef WRITE_PNG_H
-#define WRITE_PNG_H
-
-void
-write_png_argb32 (char *buffer, char* filename,
- int width, int height, int stride);
-
-char *
-read_png_argb32 (char *filename,
- int *width, int *height, int *stride);
-
-#endif