Last active
July 9, 2025 06:13
-
-
Save IchikawaYukko/5dd3efce93247b4d23faeb327b2bfd87 to your computer and use it in GitHub Desktop.
bin2bmp
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
| // bin2bmp.cpp : アプリケーションのエントリ ポイントを定義します。 | |
| // | |
| #include <iostream> | |
| #include <stdio.h> | |
| #include <stdlib.h> | |
| #include <stdint.h> | |
| #include <math.h> | |
| using namespace std; | |
| typedef struct bmp { | |
| //char16_t magic; | |
| uint32_t filesize; | |
| uint32_t reserved; | |
| uint32_t data_offset; | |
| uint32_t header_size; | |
| uint32_t x_width; | |
| uint32_t y_height; | |
| uint16_t plain; | |
| uint16_t color_depth; | |
| uint32_t compression; | |
| uint32_t image_size; | |
| uint32_t xdencity; | |
| uint32_t ydenxity; | |
| uint32_t color_count_minor; | |
| uint32_t color_count_major; | |
| } BMPHEADER; | |
| int main() | |
| { | |
| FILE* src = fopen("python-3.13.5-amd64.exe", "rb"); | |
| uint16_t HEADER_SIZE = 54; | |
| fseek(src, 0, SEEK_END); | |
| uint32_t src_size = ftell(src); | |
| uint32_t x_size = floor(sqrt(src_size / 3)) ; | |
| uint32_t y_size = x_size; | |
| cout << x_size; | |
| fseek(src, 0, SEEK_SET); | |
| BMPHEADER header = { | |
| //0x4d42, | |
| src_size + HEADER_SIZE, | |
| 0, | |
| HEADER_SIZE, | |
| 40, | |
| x_size, y_size, | |
| 1, | |
| 24, | |
| 0, | |
| src_size, | |
| 3779, 3779, | |
| 0, 0}; | |
| /*char* buf = (char*)malloc(1048576); | |
| if (buf == NULL) { | |
| return -1; | |
| }*/ | |
| FILE* dst = fopen("tmp.bmp", "wb"); | |
| if (src == NULL) { | |
| return -1; | |
| } | |
| char writebuf[sizeof(header)]; | |
| memcpy(&writebuf, (void *) &header, sizeof(header)+2); | |
| //ftell() | |
| // Magic | |
| fputc('B', dst); | |
| fputc('M', dst); | |
| // BMP header | |
| fwrite(&writebuf, sizeof(char), sizeof(writebuf), dst); | |
| // Copy image data | |
| int c = 0; | |
| while ((c = fgetc(src)) != EOF) { | |
| fputc(c, dst); | |
| } | |
| //size_t read_bytes = fread(buf, 1024, 1, f); | |
| // Finalize | |
| fclose(src); | |
| fflush(dst); | |
| fclose(dst); | |
| //cout << "read bytes: " << endl; | |
| return 0; | |
| } |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment