Adam Resnick / StepperMotors

Dependents:   CNCAirbrushCode

Committer:
stretch
Date:
Fri May 04 06:25:41 2012 +0000
Revision:
1:cf60b60a1b5b
Parent:
0:3058939fa37c

        

Who changed what in which revision?

UserRevisionLine numberNew contents of line
stretch 0:3058939fa37c 1 #include "Bitmap.h"
stretch 0:3058939fa37c 2 #define OFFSET_LOCATION 0x0a
stretch 0:3058939fa37c 3 #define WIDTH_LOCATION 0x12
stretch 0:3058939fa37c 4 #define HEIGHT_LOCATION 0x16
stretch 0:3058939fa37c 5
stretch 0:3058939fa37c 6 bool Bitmap::isPixel(int pixel, bool reversed) {
stretch 0:3058939fa37c 7 if (pixel >= width) { return false; }
stretch 0:3058939fa37c 8 if (reversed) { pixel = width - pixel - 1; }
stretch 0:3058939fa37c 9 if ((row_data[pixel/(8*sizeof(char))]>>(8*sizeof(char)-(pixel%(8*sizeof(char)))-1)) & 0x01) { return false; }
stretch 0:3058939fa37c 10 else { return true; }
stretch 0:3058939fa37c 11 }
stretch 0:3058939fa37c 12
stretch 0:3058939fa37c 13 bool Bitmap::isPixel(int pixel) {
stretch 0:3058939fa37c 14 return isPixel(pixel, false);
stretch 0:3058939fa37c 15 }
stretch 0:3058939fa37c 16
stretch 0:3058939fa37c 17 bool Bitmap::isBlankRow(void) {
stretch 0:3058939fa37c 18 for (int i = 0; i < width; i++) {
stretch 0:3058939fa37c 19 if (isPixel(i))
stretch 0:3058939fa37c 20 return false;
stretch 0:3058939fa37c 21 }
stretch 0:3058939fa37c 22 return true;
stretch 0:3058939fa37c 23 }
stretch 0:3058939fa37c 24
stretch 0:3058939fa37c 25 Bitmap::Bitmap () {
stretch 0:3058939fa37c 26 loaded = false;
stretch 0:3058939fa37c 27 fp = NULL;
stretch 0:3058939fa37c 28 row_data = NULL;
stretch 0:3058939fa37c 29 }
stretch 0:3058939fa37c 30
stretch 0:3058939fa37c 31 bool Bitmap::openImg(char* filename) {
stretch 0:3058939fa37c 32 if (fp != NULL) {
stretch 0:3058939fa37c 33 fclose(fp);
stretch 0:3058939fa37c 34 free(row_data);
stretch 0:3058939fa37c 35 }
stretch 0:3058939fa37c 36 fp = fopen(filename, "rb");
stretch 0:3058939fa37c 37 loaded = true;
stretch 0:3058939fa37c 38 fseek(fp, OFFSET_LOCATION, SEEK_SET);
stretch 0:3058939fa37c 39 fread((char *) &offset, 1, 4, fp);
stretch 0:3058939fa37c 40 fseek(fp, WIDTH_LOCATION, SEEK_SET);
stretch 0:3058939fa37c 41 fread((char *) &width, 1, 4, fp);
stretch 0:3058939fa37c 42 fread(&height, sizeof(long), 1, fp);
stretch 0:3058939fa37c 43 fseek(fp, HEIGHT_LOCATION, SEEK_SET);
stretch 0:3058939fa37c 44 row_size = (width % 32) ? 4 * ((width / 32) + 1) : 4 * (width / 32);
stretch 0:3058939fa37c 45 row_data = (char *) malloc(row_size * sizeof(char));
stretch 0:3058939fa37c 46 return true;
stretch 0:3058939fa37c 47 }
stretch 0:3058939fa37c 48
stretch 0:3058939fa37c 49 bool Bitmap::setRow(int row) {
stretch 0:3058939fa37c 50 if (loaded) {
stretch 0:3058939fa37c 51 row_num = row;
stretch 0:3058939fa37c 52 fseek(fp, offset + row * row_size*sizeof(char), SEEK_SET);
stretch 0:3058939fa37c 53 fread(row_data, sizeof(char), row_size, fp);
stretch 0:3058939fa37c 54 return true;
stretch 0:3058939fa37c 55 } else {
stretch 0:3058939fa37c 56 return false;
stretch 0:3058939fa37c 57 }
stretch 0:3058939fa37c 58 }
stretch 0:3058939fa37c 59
stretch 0:3058939fa37c 60 bool Bitmap::closeImg(void) {
stretch 0:3058939fa37c 61 fclose(fp);
stretch 0:3058939fa37c 62 free(row_data);
stretch 0:3058939fa37c 63 return true;
stretch 0:3058939fa37c 64 }
stretch 0:3058939fa37c 65
stretch 0:3058939fa37c 66 int Bitmap::getHeight() {
stretch 0:3058939fa37c 67 return height;
stretch 0:3058939fa37c 68 }
stretch 0:3058939fa37c 69
stretch 0:3058939fa37c 70 int Bitmap::getWidth() {
stretch 0:3058939fa37c 71 return width;
stretch 0:3058939fa37c 72 }
stretch 0:3058939fa37c 73