simplify menu handling
[rofl0r-pato.git] / imginterface.c
bloba058dd101ec23bef0960d5eb28abee0edd5a510c
1 #include <string.h>
2 #include <assert.h>
4 #include <leptonica/allheaders.h>
7 #include "imginterface.h"
10 Image* img_new(size_t w, size_t h) {
11 Image* result;
12 result = malloc(sizeof(Image) + (w * h * 4));
13 result->data = (char*) result + sizeof(Image);
14 result->h = h;
15 result->w = w;
16 return result;
19 Image* getWorldImage(void) {
20 struct Pix* pix32;
21 struct Pix* pngfile = pixRead("world3.png");
23 int w, h;
25 Image* result;
26 pixGetDimensions(pngfile, &w, &h, NULL);
27 pix32 = pixConvertTo32(pngfile);
28 result = img_new(w, h);
29 memcpy(result->data, pix32->data, w * h * 4);
30 pixDestroy(&pngfile);
31 pixDestroy(&pix32);
32 return result;
35 void img_fillcolor(Image* img, rgb_t color) {
36 int* out = (int*) img->data;
37 size_t h, w;
38 for(h = 0; h < img->h; h++) {
39 for(w = 0; w < img->w; w++) {
40 *(out++) = color.asInt;
45 Image* img_scale(Image* img, size_t zoomFactor_w, size_t zoomFactor_h) {
46 Image* result = img_new(img->w * zoomFactor_w, img->h * zoomFactor_h);
47 int* in = (int*) img->data;
48 int* out = (int*) result->data;
49 size_t h, w, zh, zw;
50 for(h = 0; h < img->h; h++) {
51 for(w = 0; w < img->w; w++) {
52 for(zh = 0; zh < zoomFactor_h; zh++) {
53 for(zw = 0; zw < zoomFactor_w; zw++) {
54 *(out + (((h * zoomFactor_h + zh) * result->w) + (w * zoomFactor_w) + zw)) = *in;
57 in++;
60 return result;
63 void img_embed(Image* dest, Image* source, size_t x, size_t y) {
64 assert(source && dest && x + source->w < dest->w && y + source->h < dest->h);
65 int* out;
66 int* in = (int*) source->data;
67 size_t w, h;
68 for(h = 0; h < source->h; h++) {
69 out = ((int*) dest->data) + ((h + y) * dest->w) + x;
70 for(w = 0; w < source->w; w++) {
71 *(out++) = *(in++);