xgetshape
diff src/image.c @ 0:2f02f100b20f
getting the window shape of another window
author | John Tsiombikas <nuclear@member.fsf.org> |
---|---|
date | Tue, 03 Nov 2015 00:42:08 +0200 |
parents | |
children |
line diff
1.1 --- /dev/null Thu Jan 01 00:00:00 1970 +0000 1.2 +++ b/src/image.c Tue Nov 03 00:42:08 2015 +0200 1.3 @@ -0,0 +1,122 @@ 1.4 +#include <stdio.h> 1.5 +#include <stdlib.h> 1.6 +#include <string.h> 1.7 +#include <errno.h> 1.8 +#include "image.h" 1.9 + 1.10 +int image_create(struct image *img, int w, int h) 1.11 +{ 1.12 + img->width = w; 1.13 + img->height = h; 1.14 + if(!(img->pixels = malloc(w * h * 4))) { 1.15 + return -1; 1.16 + } 1.17 + return 0; 1.18 +} 1.19 + 1.20 +void image_destroy(struct image *img) 1.21 +{ 1.22 + free(img->pixels); 1.23 + img->pixels = 0; 1.24 +} 1.25 + 1.26 +int image_save(struct image *img, const char *rgbname, const char *aname) 1.27 +{ 1.28 + int i; 1.29 + FILE *fp; 1.30 + unsigned char *pptr; 1.31 + 1.32 + if(rgbname) { 1.33 + if(!(fp = fopen(rgbname, "wb"))) { 1.34 + fprintf(stderr, "failed to open %s for writing: %s\n", rgbname, strerror(errno)); 1.35 + return -1; 1.36 + } 1.37 + 1.38 + fprintf(fp, "P6\n%d %d\n255\n", img->width, img->height); 1.39 + pptr = img->pixels; 1.40 + for(i=0; i<img->width * img->height; i++) { 1.41 + fputc(*pptr++, fp); 1.42 + fputc(*pptr++, fp); 1.43 + fputc(*pptr++, fp); 1.44 + ++pptr; 1.45 + } 1.46 + fclose(fp); 1.47 + } 1.48 + if(aname) { 1.49 + if(!(fp = fopen(aname, "wb"))) { 1.50 + fprintf(stderr, "failed to open %s for writing: %s\n", rgbname, strerror(errno)); 1.51 + return -1; 1.52 + } 1.53 + 1.54 + fprintf(fp, "P6\n%d %d\n255\n", img->width, img->height); 1.55 + pptr = img->pixels; 1.56 + for(i=0; i<img->width * img->height; i++) { 1.57 + int c = pptr[3]; 1.58 + pptr += 4; 1.59 + fputc(c, fp); 1.60 + fputc(c, fp); 1.61 + fputc(c, fp); 1.62 + } 1.63 + fclose(fp); 1.64 + } 1.65 + return 0; 1.66 +} 1.67 + 1.68 +int image_clear(struct image *img, int r, int g, int b, int a) 1.69 +{ 1.70 + return image_fillrect(img, 0, 0, img->width, img->height, r, g, b, a); 1.71 +} 1.72 + 1.73 +int image_fillrect(struct image *img, int x, int y, int w, int h, int r, int g, int b, int a) 1.74 +{ 1.75 + int i, j; 1.76 + unsigned char *pptr; 1.77 + 1.78 + if(x < 0) { 1.79 + w += x; 1.80 + x = 0; 1.81 + } 1.82 + if(y < 0) { 1.83 + h += y; 1.84 + y = 0; 1.85 + } 1.86 + if(x + w >= img->width) { 1.87 + w = img->width - x; 1.88 + } 1.89 + if(y + h >= img->height) { 1.90 + h = img->height - y; 1.91 + } 1.92 + if(w <= 0 || h <= 0) { 1.93 + return -1; 1.94 + } 1.95 + 1.96 + pptr = img->pixels + (y * img->width + x) * 4; 1.97 + for(i=0; i<h; i++) { 1.98 + for(j=0; j<w; j++) { 1.99 + pptr[0] = r; 1.100 + pptr[1] = g; 1.101 + pptr[2] = b; 1.102 + pptr[3] = a; 1.103 + pptr += 4; 1.104 + } 1.105 + pptr += (img->width - w) * 4; 1.106 + } 1.107 + return 0; 1.108 +} 1.109 + 1.110 +int image_chess(struct image *img, int sz, int r0, int g0, int b0, int r1, int g1, int b1) 1.111 +{ 1.112 + int i, j; 1.113 + unsigned char *pptr = img->pixels; 1.114 + 1.115 + for(i=0; i<img->height; i++) { 1.116 + for(j=0; j<img->width; j++) { 1.117 + int chess = ((i / sz) & 1) == ((j / sz) & 1); 1.118 + *pptr++ = chess ? r0 : r1; 1.119 + *pptr++ = chess ? g0 : g1; 1.120 + *pptr++ = chess ? b0 : b1; 1.121 + *pptr++ = 255; 1.122 + } 1.123 + } 1.124 + return 0; 1.125 +}