Created
April 3, 2018 00:23
-
-
Save envzhu/aef85750b2d505814ae4c4b32dfae78d 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{ | |
| FILE *bmp_f; | |
| 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; | |
| int bmp_read_head(bmp_format_t *img){ | |
| uint8_t buffer[4]; | |
| FILE *f = img->bmp_f; | |
| 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 font_view(FILE *out, bmp_format_t *img, int x_size, int y_size){ | |
| int i; | |
| int i_x, i_y, f_x, f_y; | |
| int code; | |
| for(code = 0x30; code < 0x50; code++){ | |
| fprintf(out, "0x%x \n", code); | |
| f_x = (code & 0x0f)*x_size; | |
| f_y = (code >>4)*y_size; | |
| for(i_y=img->Info_Header.img_height-1-f_y; i_y>img->Info_Header.img_height-f_y-1-y_size; i_y--){ | |
| for(i_x=f_x; i_x<f_x+x_size; 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/128; | |
| fprintf(out, "%c", "+ "[i]); | |
| } | |
| fprintf(out, "\n"); | |
| } | |
| } | |
| return 0; | |
| } | |
| int font2c(FILE *out, bmp_format_t *img, int x_size, int y_size){ | |
| int i, y_data; | |
| int i_x, i_y, f_x, f_y; | |
| int code; | |
| fputs("const unsigned char font[][] = {\n", out); | |
| for(code = 0x00; code < 0x50; code++){ | |
| fprintf(out, "//0x%x\n{\n", code); | |
| f_x = (code & 0x0f)*x_size; | |
| f_y = (code >>4)*y_size; | |
| for(i_y=img->Info_Header.img_height-1-f_y; i_y>img->Info_Header.img_height-f_y-1-y_size; i_y--){ | |
| fprintf(out, " "); | |
| y_data=0; | |
| for(i_x=f_x; i_x<f_x+x_size; 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/128; | |
| y_data += i; | |
| } | |
| fprintf(out, "0x%02x\,\n",y_data); | |
| } | |
| fputs("}\,\n", out); | |
| } | |
| fputs("}\;", out); | |
| return 0; | |
| } | |
| int main (int argc, char *argv[]){ | |
| FILE *f_c; | |
| bmp_format_t img; | |
| if ((img.bmp_f = fopen(argv[1], "rb")) == NULL) { | |
| printf("file open error!!\n"); | |
| exit(EXIT_FAILURE); | |
| } | |
| if ((f_c = fopen(argv[2], "w")) == NULL) { | |
| printf("file open error!!\n"); | |
| exit(EXIT_FAILURE); | |
| } | |
| int x_size = atoi(argv[3]), y_size = atoi(argv[4]); | |
| if (x_size==0 || y_size==0){ | |
| printf("font size error!!\n"); | |
| exit(EXIT_FAILURE); | |
| } | |
| bmp_read_head(&img); | |
| font2c(f_c, &img, x_size, y_size); | |
| fclose(img.bmp_f); | |
| fclose(f_c); | |
| } |
Author
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
You can convert bitmap font in bitmap format to language C array with below command.
bmpfont2c <input bitmap file name> <output c file name> <x size of font> <y size of font>For example ::
bmpfont2c font.bmp fot.c 4 8