#include <u.h>
#include <libc.h>
#include <draw.h>
#include "mapbook.h"
#include "ifslib.h"
Mapbook *
new_mapbook(int width, int height) {
Mapbook *m;
m = (Mapbook*) emallocz(sizeof(Mapbook));
m->width = width;
m->height = height;
return m;
}
Mapbook *
free_mapbook(Mapbook *m) { // always suicides
Bitmap *b, *next;
if(m) {
for(b = m->bitmaps; b; b = next) {
next = b->next;
// free(b->data);
// free(b);
}
// free(m);
}
return nil;
}
void
print_mapbook(Mapbook *m) {
print("width %d\nheight %d\n", m->width, m->height);
Bitmap *b;
for(b = m->bitmaps; b; b = b->next) {
print("channel %d %s\n", b->id, b->name);
}
}
Bitmap *
get_bitmap(Mapbook *m, int id) {
Bitmap *b;
if(!m) return nil;
for(b = m->bitmaps; b; b=b->next)
if (b->id == id) break;
return b;
}
Bitmap *
blank_bitmap(Mapbook *m) {
Bitmap *blank = (Bitmap*)emallocz(sizeof(Bitmap));
blank->data = (uchar*) emallocz(m->width * m->height);
return blank;
}
Bitmap *
get_or_create_bitmap(Mapbook *m, int id) {
Bitmap *found, *last = nil;
for(found = m->bitmaps; found; found = found->next) {
last = found;
if(found->id == id) break;
}
if(!found) {
found = blank_bitmap(m);
last ? (last->next = found) : (m->bitmaps = found);
found->id = id;
}
return found;
}
void
clear_mapbook(Mapbook *m) {
Bitmap *b;
Bitmap *bn;
for(b = m->bitmaps; b;) {
free(b->name);
free(b->data);
bn = b->next;
free(b);
b = bn;
}
m->bitmaps = nil;
}
char *
set_mapbook_width(Mapbook *m, int nw) {
Rectangle src, dst;
Bitmap *original;
uchar *data, *inpoint, *srcpoint;
int y, dw;
if(!m) return "mapbook nil";
src.max.x = m->width;
dst.max.x = nw;
src.max.y = dst.max.y = m->height;
dw = nw < m->width ? nw : m->width;
original = m->bitmaps;
if(!original) {
m->width = nw;
return nil;
}
while(original) {
if(original->data) {
inpoint = data = (uchar*) emallocz(dst.max.x * dst.max.y);
srcpoint = original->data;
for(y = 0; y < m->height; y++) {
memcpy(inpoint, srcpoint, dw);
inpoint = &inpoint[nw];
srcpoint = &srcpoint[m->width];
}
free(original->data);
original->data = data;
}
original = original->next;
}
m->width = nw;
return nil;
}
char *
set_mapbook_height(Mapbook *m, int nh) {
Bitmap *original;
uchar *data;
if(!m) return "mapbook nil";
original = m->bitmaps;
if(!original) {
m->height = nh;
return nil;
}
while(original) {
if(original->data) {
data = (uchar*) emallocz(nh * m->width);
memcpy(data, original->data, nh * m->width);
free(original->data);
original->data = data;
}
original = original->next;
}
m->height = nh;
return nil;
}
|