- Timestamp:
- Mar 9, 2012, 10:12:14 AM (12 years ago)
- File:
-
- 1 edited
Legend:
- Unmodified
- Added
- Removed
-
trunk/packages/vizservers/nanovis/imgLoaders/BMPImageLoaderImpl.cpp
r2798 r2831 1 1 /* -*- mode: c++; c-basic-offset: 4; indent-tabs-mode: nil -*- */ 2 #include "BMPImageLoaderImpl.h"3 #include "Image.h"4 2 #include <stdio.h> 5 3 #include <memory.h> 6 4 #include <stdlib.h> 5 6 #include "BMPImageLoaderImpl.h" 7 #include "Image.h" 7 8 8 9 BMPImageLoaderImpl::BMPImageLoaderImpl() … … 15 16 16 17 // I referred to cjbackhouse@hotmail.com www.backhouse.tk 17 Image * BMPImageLoaderImpl::load(const char*fileName)18 Image *BMPImageLoaderImpl::load(const char *fileName) 18 19 { 19 20 printf("BMP loader\n"); 20 21 fflush(stdout); 21 Image * image = 0;22 Image *image = NULL; 22 23 23 24 printf("opening image file \"%s\"\n", fileName); 24 FILE *f = fopen(fileName, "rb");25 FILE *f = fopen(fileName, "rb"); 25 26 26 27 if (!f) { … … 31 32 char header[54]; 32 33 if (fread(&header, 54, 1, f) != 1) { 33 34 35 } ;34 printf("can't read header of BMP file\n"); 35 return 0; 36 } 36 37 37 38 if (header[0] != 'B' || header[1] != 'M') { 38 39 39 printf("File is not BMP format\n"); 40 return 0; 40 41 } 41 42 42 43 //it seems gimp sometimes makes its headers small, so we have to do 43 44 //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); 48 46 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 50 51 51 52 printf("image width = %d height = %d bits=%d\n", width, height, bits); 52 53 fflush(stdout); 53 54 54 55 image = new Image(width, height, _targetImageFormat, 55 Image::IMG_UNSIGNED_BYTE, 0);56 Image::IMG_UNSIGNED_BYTE, NULL); 56 57 57 58 printf("image created\n"); 58 59 fflush(stdout); 59 60 60 unsigned char * bytes = (unsigned char*)image->getImageBuffer();61 unsigned char *bytes = (unsigned char *)image->getImageBuffer(); 61 62 memset(bytes, 0, sizeof(unsigned char) * width * height * _targetImageFormat); 62 63 … … 64 65 fflush(stdout); 65 66 66 unsigned int x, y;67 unsigned int x, y; 67 68 unsigned char cols[256*4]; //colourtable 68 switch (bits) {69 switch (bits) { 69 70 case 24: 70 fseek(f,offset,SEEK_SET);71 72 if (fread(bytes,width*height*3,1,f) != 1) {73 74 75 for(x=0;x<width*height*3;x+=3) { //except the format is BGR, grr76 77 78 79 80 81 char* buff = (char*)malloc(width * height * sizeof(unsigned char) * 3);82 if (fread(buff,width*height*3,1,f) != 1) {83 84 85 for(x=0, y = 0;x<width*height*3;x+=3, y+=4){ //except the format is BGR, grr86 87 88 89 90 91 92 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; 93 94 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; 117 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 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 } 147 136 148 for(c=0;c<_targetImageFormat;++c)149 bmp.pixel(x+1,y,c)=cols[byte%16*4+2-c];150 */151 }152 153 137 printf("image initialized\n"); 154 138 fflush(stdout);
Note: See TracChangeset
for help on using the changeset viewer.