- 论坛徽章:
- 0
|
程序继续:
char *useage = "Useage: %s <multicolor BMP file> <out filename>\n";
bool loadBMPImage(const char *filename, BMPImage *img);
void rgb2gray(BMPImage *img, const char *outfile);
int main(int argc, char *argv[]) {
if(argc < 3) {
printf(useage, argv[0]);
exit(1);
}
BMPImage img;
loadBMPImage(argv[1], &img);
// initRgbTable();
// time_t start = time(NULL);
clock_t start = clock();
rgb2gray(&img, argv[2]);
clock_t end = clock();
printf("past: %ld\n", end - start);
return 0;
}
bool loadBMPImage(const char *filename, BMPImage *img) {
FILE *file;
uint32_t size; // size of the image in bytes.
uint32_t i; // standard counter.
uint16_t planes; // number of planes in image (must be 1)
uint16_t bpp; // number of bits per pixel (must be 24)
printf("Start reading info from BMP file: %s\n", filename);
// make sure the file is there
if(!(file=fopen(filename, "rb"))) {
printf("File not found\n");
return false;
}
fseek(file, 2, SEEK_CUR);
if(fread(&img->bh, 12, 1, file) != 1) {
printf("Error reading BmpHead\n");
return false;
}
if(fread(&img->ih, 40, 1, file) != 1) {
printf("Error reading InfoHead\n");
return false;
}
if(img->ih.colorPlane != 1) {
printf("Planes is not 1: %u\n", planes);
return false;
}
if(img->ih.bitColor != 24) {
printf("Bpp is not 24: %u\n", bpp);
return false;
}
printf("width: %dpx\n", img->ih.width);
printf("height: %dpx\n", img->ih.height);
// calculate the size (assuming 24 bits or 3 bytes pre pixel).
size = img->ih.width * img->ih.height;
// read the data
img->data = malloc(size*3);
if(!img->data) {
printf("Error allocating memory fro color-corrected image data\n");
return false;
}
if(fread(img->data, size*3, 1, file) != 1) {
printf("Error reading image data\n");
return false;
}
printf("Finish Reading info from BMP file: %s\n", filename);
/*
for( i=0; i<size; i++) { // reverse all of the colors. (bgr -> rgb)
img->data.r ^= img->data.b;
img->data.b ^= img->data.r;
img->data.r ^= img->data.b;
}
*/
// we're done
fclose(file);
return true;
} |
[ 本帖最后由 hshq_cn 于 2007-6-28 13:30 编辑 ] |
|