Steven Rhodes / CNCAirbrush

Bitmap.cpp

Committer:
stvnrhodes
Date:
2012-04-18
Revision:
2:6d2a681c5cdf
Child:
3:328a79795feb

File content as of revision 2:6d2a681c5cdf:

#include "Bitmap.h"

#define OFFSET_LOCATION 0x0a
#define WIDTH_LOCATION 0x12
#define HEIGHT_LOCATION 0x16

bool Bitmap::isPixel(int pixel) {
    if (pixel > width) { return false; }
    if ((row_data[pixel/32]>>(pixel%32)) & 0x01) { return true; }
    else { return false; }
}

Bitmap::Bitmap () {
    loaded = false;
    fp = NULL;
    row_data = NULL;
}

bool Bitmap::openImg(char* filename) {
    if (fp != NULL) {
        fclose(fp);
        free(row_data);
    }
    printf("Opening\n\r");
    fp = fopen(filename, "rb");
    loaded = true;
    printf("Opened\n\r");
    fseek(fp, OFFSET_LOCATION, SEEK_SET);
    fread((char *) &offset, 1, 4, fp);
    fseek(fp, WIDTH_LOCATION, SEEK_SET);
    fread((char *) &width, 1, 4, fp);
    fread(&height, sizeof(long), 1, fp);
    fseek(fp, HEIGHT_LOCATION, SEEK_SET);
    printf("%x,%x,%x\n\r", offset, width, height);
    row_size = (width % 32) ? (width / 32) + 1 :  width / 32;
    row_data = (long *) malloc(row_size * sizeof(long));
    return true;
}

bool Bitmap::setRow(int row) {
    if (loaded) {
        row_num = row;
        fseek(fp, offset + row * row_size, SEEK_SET);
        for (int i = 0; i < row_size; i++) {
            fscanf(fp,"%4ld", row_data + i);
            printf("%4ld\n\r", row_data[i]);
        }
        return true;
    } else {
        return false;
    }
}

bool Bitmap::closeImg(void) {
    fclose(fp);
    free(row_data);
    return true;
}

int Bitmap::getHeight() {
    return height;
}

int Bitmap::getWidth() {
    return width;
}