xgetshape

annotate src/image.c @ 2:b832d3b3ed98

taking offset into account in get_window_shape
author John Tsiombikas <nuclear@member.fsf.org>
date Mon, 01 Feb 2016 12:18:29 +0200
parents
children
rev   line source
nuclear@0 1 #include <stdio.h>
nuclear@0 2 #include <stdlib.h>
nuclear@0 3 #include <string.h>
nuclear@0 4 #include <errno.h>
nuclear@0 5 #include "image.h"
nuclear@0 6
nuclear@0 7 int image_create(struct image *img, int w, int h)
nuclear@0 8 {
nuclear@0 9 img->width = w;
nuclear@0 10 img->height = h;
nuclear@0 11 if(!(img->pixels = malloc(w * h * 4))) {
nuclear@0 12 return -1;
nuclear@0 13 }
nuclear@0 14 return 0;
nuclear@0 15 }
nuclear@0 16
nuclear@0 17 void image_destroy(struct image *img)
nuclear@0 18 {
nuclear@0 19 free(img->pixels);
nuclear@0 20 img->pixels = 0;
nuclear@0 21 }
nuclear@0 22
nuclear@0 23 int image_save(struct image *img, const char *rgbname, const char *aname)
nuclear@0 24 {
nuclear@0 25 int i;
nuclear@0 26 FILE *fp;
nuclear@0 27 unsigned char *pptr;
nuclear@0 28
nuclear@0 29 if(rgbname) {
nuclear@0 30 if(!(fp = fopen(rgbname, "wb"))) {
nuclear@0 31 fprintf(stderr, "failed to open %s for writing: %s\n", rgbname, strerror(errno));
nuclear@0 32 return -1;
nuclear@0 33 }
nuclear@0 34
nuclear@0 35 fprintf(fp, "P6\n%d %d\n255\n", img->width, img->height);
nuclear@0 36 pptr = img->pixels;
nuclear@0 37 for(i=0; i<img->width * img->height; i++) {
nuclear@0 38 fputc(*pptr++, fp);
nuclear@0 39 fputc(*pptr++, fp);
nuclear@0 40 fputc(*pptr++, fp);
nuclear@0 41 ++pptr;
nuclear@0 42 }
nuclear@0 43 fclose(fp);
nuclear@0 44 }
nuclear@0 45 if(aname) {
nuclear@0 46 if(!(fp = fopen(aname, "wb"))) {
nuclear@0 47 fprintf(stderr, "failed to open %s for writing: %s\n", rgbname, strerror(errno));
nuclear@0 48 return -1;
nuclear@0 49 }
nuclear@0 50
nuclear@0 51 fprintf(fp, "P6\n%d %d\n255\n", img->width, img->height);
nuclear@0 52 pptr = img->pixels;
nuclear@0 53 for(i=0; i<img->width * img->height; i++) {
nuclear@0 54 int c = pptr[3];
nuclear@0 55 pptr += 4;
nuclear@0 56 fputc(c, fp);
nuclear@0 57 fputc(c, fp);
nuclear@0 58 fputc(c, fp);
nuclear@0 59 }
nuclear@0 60 fclose(fp);
nuclear@0 61 }
nuclear@0 62 return 0;
nuclear@0 63 }
nuclear@0 64
nuclear@0 65 int image_clear(struct image *img, int r, int g, int b, int a)
nuclear@0 66 {
nuclear@0 67 return image_fillrect(img, 0, 0, img->width, img->height, r, g, b, a);
nuclear@0 68 }
nuclear@0 69
nuclear@0 70 int image_fillrect(struct image *img, int x, int y, int w, int h, int r, int g, int b, int a)
nuclear@0 71 {
nuclear@0 72 int i, j;
nuclear@0 73 unsigned char *pptr;
nuclear@0 74
nuclear@0 75 if(x < 0) {
nuclear@0 76 w += x;
nuclear@0 77 x = 0;
nuclear@0 78 }
nuclear@0 79 if(y < 0) {
nuclear@0 80 h += y;
nuclear@0 81 y = 0;
nuclear@0 82 }
nuclear@0 83 if(x + w >= img->width) {
nuclear@0 84 w = img->width - x;
nuclear@0 85 }
nuclear@0 86 if(y + h >= img->height) {
nuclear@0 87 h = img->height - y;
nuclear@0 88 }
nuclear@0 89 if(w <= 0 || h <= 0) {
nuclear@0 90 return -1;
nuclear@0 91 }
nuclear@0 92
nuclear@0 93 pptr = img->pixels + (y * img->width + x) * 4;
nuclear@0 94 for(i=0; i<h; i++) {
nuclear@0 95 for(j=0; j<w; j++) {
nuclear@0 96 pptr[0] = r;
nuclear@0 97 pptr[1] = g;
nuclear@0 98 pptr[2] = b;
nuclear@0 99 pptr[3] = a;
nuclear@0 100 pptr += 4;
nuclear@0 101 }
nuclear@0 102 pptr += (img->width - w) * 4;
nuclear@0 103 }
nuclear@0 104 return 0;
nuclear@0 105 }
nuclear@0 106
nuclear@0 107 int image_chess(struct image *img, int sz, int r0, int g0, int b0, int r1, int g1, int b1)
nuclear@0 108 {
nuclear@0 109 int i, j;
nuclear@0 110 unsigned char *pptr = img->pixels;
nuclear@0 111
nuclear@0 112 for(i=0; i<img->height; i++) {
nuclear@0 113 for(j=0; j<img->width; j++) {
nuclear@0 114 int chess = ((i / sz) & 1) == ((j / sz) & 1);
nuclear@0 115 *pptr++ = chess ? r0 : r1;
nuclear@0 116 *pptr++ = chess ? g0 : g1;
nuclear@0 117 *pptr++ = chess ? b0 : b1;
nuclear@0 118 *pptr++ = 255;
nuclear@0 119 }
nuclear@0 120 }
nuclear@0 121 return 0;
nuclear@0 122 }