source: trunk/packages/vizservers/nanovis/imgLoaders/BMPImageLoaderImpl.cpp @ 2831

Last change on this file since 2831 was 2831, checked in by ldelgass, 9 years ago

Refactor texture classes, misc. cleanups, cut down on header pollution -- still
need to fix header deps in Makefile.in

  • Property svn:eol-style set to native
File size: 4.5 KB
Line 
1/* -*- mode: c++; c-basic-offset: 4; indent-tabs-mode: nil -*- */
2#include <stdio.h>
3#include <memory.h>
4#include <stdlib.h>
5
6#include "BMPImageLoaderImpl.h"
7#include "Image.h"
8
9BMPImageLoaderImpl::BMPImageLoaderImpl()
10{
11}
12
13BMPImageLoaderImpl::~BMPImageLoaderImpl()
14{
15}
16
17// I referred to cjbackhouse@hotmail.com                www.backhouse.tk
18Image *BMPImageLoaderImpl::load(const char *fileName)
19{
20    printf("BMP loader\n");
21    fflush(stdout);
22    Image *image = NULL;
23
24    printf("opening image file \"%s\"\n", fileName);
25    FILE *f = fopen(fileName, "rb");
26
27    if (!f)  {
28        printf("File not found\n");
29        return 0;
30    }
31
32    char header[54];
33    if (fread(&header, 54, 1, f) != 1) {
34        printf("can't read header of BMP file\n");
35        return 0;
36    }
37
38    if (header[0] != 'B' ||  header[1] != 'M') {
39        printf("File is not BMP format\n");
40        return 0;
41    }
42
43    //it seems gimp sometimes makes its headers small, so we have to do
44    //this. hence all the fseeks
45    int offset = *(unsigned int*)(header+10);
46
47    const unsigned int width = *(int*)(header+18);
48    const unsigned int height = *(int*)(header+22);
49
50    int bits = int(header[28]);           //colourdepth
51
52    printf("image width = %d height = %d bits=%d\n", width, height, bits);
53    fflush(stdout);
54
55    image = new Image(width, height, _targetImageFormat,
56                      Image::IMG_UNSIGNED_BYTE, NULL);
57
58    printf("image created\n");
59    fflush(stdout);
60
61    unsigned char *bytes = (unsigned char *)image->getImageBuffer();
62    memset(bytes, 0, sizeof(unsigned char) * width * height * _targetImageFormat);
63
64    printf("reset image buffer\n");
65    fflush(stdout);
66
67    unsigned int x, y;
68    unsigned char cols[256*4];  //colourtable
69    switch (bits) {
70    case 24:
71        fseek(f, offset, SEEK_SET);
72        if (_targetImageFormat == Image::IMG_RGB) {
73            if (fread(bytes, width*height*3, 1, f) != 1) {
74                fprintf(stderr, "can't read image data\n");
75            }
76            for (x = 0; x < width*height*3; x += 3)  { //except the format is BGR, grr
77                unsigned char temp = bytes[x];
78                bytes[x] = bytes[x+2];
79                bytes[x+2] = temp;
80            }
81        } else if (_targetImageFormat == Image::IMG_RGBA) {
82            char *buff = (char*)malloc(width * height * sizeof(unsigned char) * 3);
83            if (fread(buff, width*height*3, 1, f) != 1) {
84                fprintf(stderr, "can't read BMP image data\n");
85            }
86            for (x = 0, y = 0; x < width*height*3; x += 3, y += 4) {       //except the format is BGR, grr
87                bytes[y] = buff[x+2];
88                bytes[y+2] = buff[x];
89                bytes[y+3] = 255;
90            }
91            free(buff);
92        }
93        break;
94    case 32:
95        fseek(f, offset, SEEK_SET);
96        if (_targetImageFormat == Image::IMG_RGBA) {
97            if (fread(bytes, width*height*4, 1, f) != 1) {
98                fprintf(stderr, "can't read image data\n");
99            }
100            for (x = 0; x < width*height*4; x += 4)  { //except the format is BGR, grr
101                unsigned char temp = bytes[x];
102                bytes[x] = bytes[x+2];
103                bytes[x+2] = temp;
104            }
105        } else if (_targetImageFormat == Image::IMG_RGB) {
106            char *buff = (char*)malloc(width * height * sizeof(unsigned char) * 3);
107            if (fread(buff, width*height*4, 1, f) != 1) {
108                fprintf(stderr, "can't read BMP image data\n");
109            }
110            for (x = 0, y = 0; x < width*height*4; x += 4, y += 3) {       //except the format is BGR, grr
111                bytes[y] = buff[x+2];
112                bytes[y+2] = buff[x];
113            }
114            free(buff);
115        }
116        break;
117    case 8:
118        if (fread(cols, 256 * 4, 1, f) != 1) {
119            fprintf(stderr, "can't read colortable from BMP file\n");
120        }
121        fseek(f,offset,SEEK_SET); 
122        for (y = 0; y < height; ++y) {  //(Notice 4bytes/col for some reason)
123            for (x = 0; x < width; ++x) {
124                unsigned char byte;                 
125                if (fread(&byte, 1, 1, f) != 1) {
126                    fprintf(stderr, "error reading BMP file\n");
127                }
128                for (int c = 0; c < 3; ++c) {
129                    //bytes[(y*width+x)*3+c] = cols[byte*4+2-c];        //and look up in the table
130                    bytes[(y*width+x)*_targetImageFormat + c] = cols[byte*4+2-c];       //and look up in the table
131                }
132            }
133        }
134        break;
135    }
136
137    printf("image initialized\n");
138    fflush(stdout);
139    return image;
140}
Note: See TracBrowser for help on using the repository browser.