#include #include #include "images.h" int image_new(int width, int height, Image* img) { //Image img = malloc(sizeof(Image)); if (img == NULL) return 0; img->height = height; img->width = width; // initialize bitmap... img->bitmap = malloc(height * sizeof(int*)); for (int i = 0; i < height; i++) { img->bitmap[i] = malloc(3 * width * sizeof(int)); } //*image = *img; return 1; } int image_set_px(Image image, int x, int y, int r, int g, int b) { if (!image_check_coords(image, x, y)) return 0; image.bitmap[y][(3 * x) + 0] = (char) r; image.bitmap[y][(3 * x) + 1] = (char) g; image.bitmap[y][(3 * x) + 2] = (char) b; return 1; } int image_set_px_c(Image image, int x, int y, Color color) { if (!image_check_coords(image, x, y)) return 0; if (color.alpha == 0) return 1; if (color.alpha == 1) { image.bitmap[y][(3 * x) + 0] = color.r; image.bitmap[y][(3 * x) + 1] = color.g; image.bitmap[y][(3 * x) + 2] = color.b; } else { //printf("adding %d to %f\n", (color.alpha * color.r), ((1.0f - color.alpha) * (image.bitmap[y][(3 * x) + 0]/* & 0x000000ff */))); image.bitmap[y][(3 * x) + 0] = (color.alpha * (color.r/* & 0x000000ff */)) + ((1.0f - color.alpha) * (image.bitmap[y][(3 * x) + 0]/* & 0x000000ff */)); image.bitmap[y][(3 * x) + 1] = (color.alpha * (color.g/* & 0x000000ff */)) + ((1.0f - color.alpha) * (image.bitmap[y][(3 * x) + 1]/* & 0x000000ff */)); image.bitmap[y][(3 * x) + 2] = (color.alpha * (color.b/* & 0x000000ff */)) + ((1.0f - color.alpha) * (image.bitmap[y][(3 * x) + 2]/* & 0x000000ff */)); } return 1; } int image_check_coords(Image image, int x, int y) { return x >= 0 && x < image.width && y >= 0 && y < image.height; } int image_destroy(Image image) { for (int i = 0; i < image.height; i++) { free(image.bitmap[i]); } free(image.bitmap); return 1; } ///////// Colors Color color_new(int r, int g, int b) { Color c; c.r = r; c.g = g; c.b = b; c.alpha = 1; return c; } Color color_new_alpha(int r, int g, int b, float alpha) { Color c; c.r = r; c.g = g; c.b = b; c.alpha = alpha; return c; } void print_color(Color c) { printf("color is: #%02hhX%02hhX%02hhX\n", c.r, c.g, c.b); } Color color_mix(Color c1, Color c2, float ratio) { return color_new_alpha( ratio * c1.r + ((1 - ratio) * c2.r), ratio * c1.g + ((1 - ratio) * c2.g), ratio * c1.b + ((1 - ratio) * c2.b), ratio * c1.alpha + ((1 - ratio) * c2.alpha) ); } //// IMAGE DRAWING HELPERS int image_draw_rect(Image image, int x1, int y1, int x2, int y2, Color color) { int retval = 1; int y; for (; x1 < x2; x1++) { for (y = y1; y < y2; y++) { if (!image_set_px_c(image, x1, y, color)) retval = 0; } } return retval; } int image_draw_circ(Image image, int x, int y, int r, Color color) { int retval = 1; int i, j, r2 = r * r; for (i = -r+1; i < r; i++) { for (j = -r+1; j < r; j++) { if (i*i + j*j >= r2) continue; if (!image_set_px_c(image, x + i, y + j, color)) retval = 0; } } return retval; } int image_draw_square(Image image, int x, int y, int size, Color color) { int retval = 1; int i, j; for (i = -size / 2; i <= size / 2; i++) { for (j = -size / 2; j <= size / 2; j++) { if (!image_set_px_c(image, x + i, y + j, color)) retval = 0; } } return retval; }