performance improvements

master
Anton Lydike 5 years ago
parent 107588ae7f
commit 83ab573efb

@ -35,10 +35,7 @@ int image_new(int width, int height, Image* img) {
// initialize bitmap... // initialize bitmap...
img->bitmap = malloc(height * sizeof(int*)); img->bitmap = malloc(height * sizeof(int*));
for (int i = 0; i < height; i++) { for (int i = 0; i < height; i++) {
img->bitmap[i] = malloc(width * sizeof(int*)); img->bitmap[i] = malloc(3 * width * sizeof(int));
for (int j = 0; j < width; j++) {
img->bitmap[i][j] = malloc(3 * sizeof(int));
}
} }
//*image = *img; //*image = *img;
@ -93,10 +90,10 @@ int image_save(Image* image, char* path) {
int line; int line;
for (line = 0; line < image->height; line++) { for (line = 0; line < image->height; line++) {
byte_pos = 0; byte_pos = 0;
for (int px = 0; px < image->width; px++) { for (int px = 0; px < image->width * 3; px += 3) {
bytes[byte_offset + byte_pos++] = image->bitmap[line][px][2]; bytes[byte_offset + byte_pos++] = image->bitmap[line][px + 2];
bytes[byte_offset + byte_pos++] = image->bitmap[line][px][1]; bytes[byte_offset + byte_pos++] = image->bitmap[line][px + 1];
bytes[byte_offset + byte_pos++] = image->bitmap[line][px][0]; bytes[byte_offset + byte_pos++] = image->bitmap[line][px + 0];
} }
byte_offset += align_to_four_bytes(byte_pos); byte_offset += align_to_four_bytes(byte_pos);
} }
@ -114,9 +111,18 @@ int image_save(Image* image, char* path) {
int image_set_px(Image* image, int x, int y, int r, int g, int b) { 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; if (!image_check_coords(image, x, y)) return 0;
image->bitmap[y][x][0] = (char) r; image->bitmap[y][(3 * x) + 0] = (char) r;
image->bitmap[y][x][1] = (char) g; image->bitmap[y][(3 * x) + 1] = (char) g;
image->bitmap[y][x][2] = (char) b; 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;
image->bitmap[y][(3 * x) + 0] = (char) color->r;
image->bitmap[y][(3 * x) + 1] = (char) color->g;
image->bitmap[y][(3 * x) + 2] = (char) color->b;
return 1; return 1;
} }
@ -128,9 +134,6 @@ int image_check_coords(Image* image, int x, int y) {
int image_destroy(Image* image) { int image_destroy(Image* image) {
for (int i = 0; i < image->height; i++) { for (int i = 0; i < image->height; i++) {
for (int j = 0; j < image->width; j++) {
free(image->bitmap[i][j]);
}
free(image->bitmap[i]); free(image->bitmap[i]);
} }
free(image->bitmap); free(image->bitmap);
@ -138,3 +141,26 @@ int image_destroy(Image* image) {
return 1; return 1;
} }
///////// Colors
Color* color_new(int r, int g, int b) {
Color* c = malloc(sizeof(Color));
c->r = r;
c->g = g;
c->b = b;
return c;
}
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;
}

@ -1,8 +1,14 @@
#ifndef IMAGES_H #ifndef IMAGES_H
#define IMAGES_H #define IMAGES_H
typedef struct _color {
char r;
char g;
char b;
} Color;
typedef struct _image { typedef struct _image {
char*** bitmap; char** bitmap;
int width; int width;
int height; int height;
} Image; } Image;
@ -12,6 +18,7 @@ int image_new(int width, int height, Image* image);
int image_save(Image* image, char* path); int image_save(Image* image, char* path);
int image_set_px(Image* image, int x, int y, int r, int g, int b); int image_set_px(Image* image, int x, int y, int r, int g, int b);
int image_set_px_c(Image* image, int x, int y, Color* color);
int image_check_coords(Image* image, int x, int y); int image_check_coords(Image* image, int x, int y);
@ -19,6 +26,13 @@ int image_destroy(Image* image);
int image_byte_size(Image *image); int image_byte_size(Image *image);
int image_draw_rect(Image* image, int x1, int y1, int x2, int y2, Color* color);
Color* color_new(int r, int g, int b);
#include "images.c" #include "images.c"
#endif #endif

@ -5,20 +5,22 @@
int main(int argc, char* argv[]) { int main(int argc, char* argv[]) {
Image img; Image img;
image_new(255,255, &img); image_new(512,512, &img);
int r,g,b; Color* green = color_new(0,255,0);
Color* purple = color_new(125,17,249);
image_draw_rect(&img, 255, 0, 512, 255, green);
image_draw_rect(&img, 0, 255, 255, 512, purple);
for (int i = 0; i < 255; i++) { for (int i = 0; i < 255; i++) {
for (int j = 0; j < 255; j++) { for (int j = 0; j < 255; j++) {
double factor = (i*i + j*j) / (100*100); image_set_px(&img, i, j, i, 0, 0);
r = i; }
g = 255 -j; }
b = i -255; for (int i = 0; i <= 256; i++) {
for (int j = 0; j <= 256; j++) {
image_set_px(&img, i + 255, j + 255, j, j, 255);
image_set_px(&img, i, j, factor * r, factor * g, factor * b);
} }
} }
@ -30,4 +32,7 @@ int main(int argc, char* argv[]) {
printf("Wrote file successfully\n"); printf("Wrote file successfully\n");
} }
image_destroy(&img); image_destroy(&img);
free(green);
free(purple);
} }
Loading…
Cancel
Save