Ignore:
Timestamp:
Mar 9, 2012, 10:12:14 AM (12 years ago)
Author:
ldelgass
Message:

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

File:
1 edited

Legend:

Unmodified
Added
Removed
  • trunk/packages/vizservers/nanovis/imgLoaders/BMPImageLoaderImpl.cpp

    r2798 r2831  
    11/* -*- mode: c++; c-basic-offset: 4; indent-tabs-mode: nil -*- */
    2 #include "BMPImageLoaderImpl.h"
    3 #include "Image.h"
    42#include <stdio.h>
    53#include <memory.h>
    64#include <stdlib.h>
     5
     6#include "BMPImageLoaderImpl.h"
     7#include "Image.h"
    78
    89BMPImageLoaderImpl::BMPImageLoaderImpl()
     
    1516
    1617// I referred to cjbackhouse@hotmail.com                www.backhouse.tk
    17 Image* BMPImageLoaderImpl::load(const char* fileName)
     18Image *BMPImageLoaderImpl::load(const char *fileName)
    1819{
    1920    printf("BMP loader\n");
    2021    fflush(stdout);
    21     Image* image = 0;
     22    Image *image = NULL;
    2223
    2324    printf("opening image file \"%s\"\n", fileName);
    24     FILE* f = fopen(fileName, "rb");
     25    FILE *f = fopen(fileName, "rb");
    2526
    2627    if (!f)  {
     
    3132    char header[54];
    3233    if (fread(&header, 54, 1, f) != 1) {
    33         printf("can't read header of BMP file\n");
    34         return 0;
    35     };
     34        printf("can't read header of BMP file\n");
     35        return 0;
     36    }
    3637
    3738    if (header[0] != 'B' ||  header[1] != 'M') {
    38         printf("File is not BMP format\n");
    39         return 0;
     39        printf("File is not BMP format\n");
     40        return 0;
    4041    }
    4142
    4243    //it seems gimp sometimes makes its headers small, so we have to do
    4344    //this. hence all the fseeks
    44     int offset=*(unsigned int*)(header+10);
    45        
    46     const unsigned int width =*(int*) (header+18);
    47     const unsigned int height =*(int*) (header+22);
     45    int offset = *(unsigned int*)(header+10);
    4846
    49     int bits=int(header[28]);           //colourdepth
     47    const unsigned int width = *(int*)(header+18);
     48    const unsigned int height = *(int*)(header+22);
     49
     50    int bits = int(header[28]);           //colourdepth
    5051
    5152    printf("image width = %d height = %d bits=%d\n", width, height, bits);
    5253    fflush(stdout);
    53    
     54
    5455    image = new Image(width, height, _targetImageFormat,
    55                       Image::IMG_UNSIGNED_BYTE, 0);
     56                      Image::IMG_UNSIGNED_BYTE, NULL);
    5657
    5758    printf("image created\n");
    5859    fflush(stdout);
    5960
    60     unsigned char* bytes = (unsigned char*) image->getImageBuffer();
     61    unsigned char *bytes = (unsigned char *)image->getImageBuffer();
    6162    memset(bytes, 0, sizeof(unsigned char) * width * height * _targetImageFormat);
    6263
     
    6465    fflush(stdout);
    6566
    66     unsigned int x,y;
     67    unsigned int x, y;
    6768    unsigned char cols[256*4];  //colourtable
    68     switch(bits) {
     69    switch (bits) {
    6970    case 24:
    70         fseek(f,offset,SEEK_SET);
    71         if (_targetImageFormat == Image::IMG_RGB) {
    72             if (fread(bytes,width*height*3,1,f) != 1) {
    73                 fprintf(stderr, "can't read image data\n");
    74             }
    75             for(x=0;x<width*height*3;x+=3)  { //except the format is BGR, grr
    76                 unsigned char temp = bytes[x];
    77                 bytes[x] = bytes[x+2];
    78                 bytes[x+2] = temp;
    79             }
    80         } else if (_targetImageFormat == Image::IMG_RGBA) {
    81             char* buff = (char*) malloc(width * height * sizeof(unsigned char) * 3);
    82             if (fread(buff,width*height*3,1,f) != 1) {
    83                 fprintf(stderr, "can't read BMP image data\n");
    84             }
    85             for(x=0, y = 0;x<width*height*3;x+=3, y+=4)    {       //except the format is BGR, grr
    86                 bytes[y] = buff[x+2];
    87                 bytes[y+2] = buff[x];
    88                 bytes[y+3] = 255;
    89             }
    90             free(buff);
    91         }
    92         break;
     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;
    9394    case 32:
    94         fseek(f,offset,SEEK_SET);
    95         if (_targetImageFormat == Image::IMG_RGBA) {
    96             if (fread(bytes,width*height*4,1,f) != 1) {
    97                 fprintf(stderr, "can't read image data\n");
    98             }
    99             for(x=0;x<width*height*4;x+=4)  { //except the format is BGR, grr
    100                 unsigned char temp = bytes[x];
    101                 bytes[x] = bytes[x+2];
    102                 bytes[x+2] = temp;
    103             }
    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;
     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;
    117117    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           case(4):
    137           fread(cols,16*4,1,f);
    138           fseek(f,offset,SEEK_SET);
    139           for(y=0;y<256;++y)
    140           for(x=0;x<256;x+=2)
    141           {
    142           BYTE byte;
    143           fread(&byte,1,1,f);                                               //as above, but need to exract two
    144          
    145           for(c=0;c<_targetImageFormat;++c)                                         //pixels from each byte
    146           bmp.pixel(x,y,c)=cols[byte/16*4+2-c];
     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    }
    147136
    148           for(c=0;c<_targetImageFormat;++c)
    149           bmp.pixel(x+1,y,c)=cols[byte%16*4+2-c];
    150         */
    151     }
    152    
    153137    printf("image initialized\n");
    154138    fflush(stdout);
Note: See TracChangeset for help on using the changeset viewer.