Last active
March 14, 2018 05:51
-
-
Save envzhu/a226f2bf1567bcf0a201f1b920812d64 to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| #include <stdio.h> | |
| #include <stdint.h> | |
| #include <stdlib.h> | |
| #include <string.h> | |
| typedef struct _bmp_format{ | |
| struct { | |
| uint32_t file_size; | |
| int reserved; | |
| uint32_t offset; | |
| }File_Header; | |
| struct { | |
| uint32_t info_header_size; | |
| uint32_t img_width; | |
| uint32_t img_height; | |
| uint16_t plane_num; | |
| uint16_t color_bit; | |
| uint32_t compression; | |
| uint32_t img_data_size; | |
| uint32_t x_pix_per_meter; | |
| uint32_t y_pix_per_meter; | |
| uint32_t color_used_num; | |
| uint32_t color_important; | |
| }Info_Header; | |
| uint8_t *img_data; | |
| }bmp_format_t; | |
| #define putd(d) do{printf("%s %x %d\n", #d, d, d);}while(0) | |
| char ascii_palette[] = " ...',;:clodxkO0KXNWM"; | |
| int bmp_open(FILE *f, bmp_format_t *img){ | |
| uint8_t buffer[4]; | |
| fread(buffer, 1, 2, f); | |
| fread(&img->File_Header, sizeof(img->File_Header), 1, f); | |
| fread(&img->Info_Header, sizeof(img->Info_Header), 1, f); | |
| img->img_data = malloc(img->Info_Header.img_data_size); | |
| fread(img->img_data, img->Info_Header.img_data_size, sizeof(uint8_t), f); | |
| int i; | |
| int i_x; | |
| int i_y; | |
| for(i_y=img->Info_Header.img_height-1; i_y>=0; i_y--){ | |
| for(i_x=0; i_x<img->Info_Header.img_width; i_x++){ | |
| i= (img->img_data[(img->Info_Header.img_width*i_y+i_x)*3]+img->img_data[(img->Info_Header.img_width*i_y+i_x)*3+1]+img->img_data[(img->Info_Header.img_width*i_y+i_x)*3+2])/3; | |
| i=i/16; | |
| printf("%c", ascii_palette[i]); | |
| } | |
| printf("\n"); | |
| } | |
| return 0; | |
| } | |
| int bmp_read_point(bmp_format_t *img, uint32_t x, uint32_t y){ | |
| uint8_t *p = &img->img_data[(img->Info_Header.img_width*(img->Info_Header.img_height-1-y)+x)*3]; | |
| return (*(p++))<<16 +(*(p++))<<8 +*(p++) ; | |
| } | |
| int bmp_get_grey_scale_point(bmp_format_t *img, uint32_t x, uint32_t y){ | |
| uint8_t *p = &img->img_data[(img->Info_Header.img_width*(img->Info_Header.img_height-1-y)+x)*3]; | |
| return ((*(p++)) + (*(p++)) + *(p++))/3 ; | |
| } | |
| int bmp_test(FILE *f, bmp_format_t *img){ | |
| //printf("%c%c\n", img->File_Header.file_type_1, img->File_Header.file_type_2); | |
| putd(img->File_Header.file_size); | |
| putd(img->File_Header.offset); | |
| putd(img->Info_Header.info_header_size); | |
| putd(img->Info_Header.img_width); | |
| putd(img->Info_Header.img_height); | |
| putd(img->Info_Header.plane_num); | |
| putd(img->Info_Header.color_bit); | |
| putd(img->Info_Header.compression); | |
| putd(img->Info_Header.img_data_size); | |
| putd(img->Info_Header.x_pix_per_meter); | |
| putd(img->Info_Header.y_pix_per_meter); | |
| putd(img->Info_Header.color_used_num); | |
| putd(img->Info_Header.color_important); | |
| return 0; | |
| } | |
| int main (int argc, char *argv[]){ | |
| FILE *f; | |
| bmp_format_t img; | |
| printf("bmp2ascii\n"); | |
| if ((f = fopen(argv[1], "rb")) == NULL) { | |
| printf("file open error!!\n"); | |
| exit(EXIT_FAILURE); | |
| } | |
| bmp_open(f, &img); | |
| bmp_test(f, &img); | |
| fclose(f); | |
| } |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment