Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <stdlib.h>
- #include <stdio.h>
- #include <stdint.h>
- #include <string.h>
- #pragma pack(push, 1)
- typedef struct BmHeader{
- uint8_t ID[2];
- uint32_t file_size, reserved, offset;
- } BmHeader;
- typedef struct BmInfoHeader{
- uint32_t size, width, height;
- uint16_t planes, bpp;
- uint32_t compession, image_size, unused[2], colors, important;
- } BmInfoHeader;
- #pragma pack(pop)
- #define debug 0
- #if debug
- #define log(fnt, ...) fprintf(stderr, fnt "\n", __VA_ARGS__)
- #else
- #define log(...)
- #endif
- int main(int args, char const *argv[]) {
- if (args < 3)
- {
- printf("%s <bmp in> <bmp out>", argv[0]);
- exit(1);
- }
- FILE *fr = fopen(argv[1], "rb");
- FILE *fw = fopen(argv[2], "wb");
- BmHeader bh;
- BmInfoHeader bih;
- fread(&bh, sizeof(BmHeader), 1, fr);
- fread(&bih.size, sizeof(unsigned int), 1, fr);
- fread(&bih.width, bih.size - 4, 1, fr);
- if (bih.size != 40 || bih.planes != 1 || bih.compession > 2){
- printf("Bmp file corrupted\n");
- exit(2);
- }
- if (bih.bpp != 24)
- {
- printf("bpp %d not supported\n", bih.bpp);
- exit(3);
- }
- log("fsize %d, offset 0x%x",bh.file_size, bh.offset);
- log("bpp %hd, compress %d, colors used %d",
- bih.bpp, bih.compession, bih.colors);
- unsigned int bufwidth = bih.width, bufheight = bih.height;
- fwrite(&bh, sizeof(BmHeader), 1, fw);
- fwrite(&bih, bih.size, 1, fw);
- char *buffer = malloc(bh.offset - (sizeof(BmHeader) + bih.size));
- fread(buffer, bh.offset - (sizeof(BmHeader) + bih.size), 1, fr);
- fwrite(buffer, bh.offset - (sizeof(BmHeader) + bih.size), 1, fw);
- free(buffer);
- buffer = malloc(bih.image_size);
- fread(buffer, bih.image_size, 1, fr);
- unsigned char bpp = bih.bpp / 8;
- for (int i = 0; i < bufheight / 2; i++) {
- for (int j = 0; j < bufwidth * bpp; j++) {
- char tmp = buffer[bpp * i * bufwidth + j];
- buffer[bpp * i * bufwidth + j] = buffer[bpp * bufwidth * (bufheight - i - 1) + j];
- buffer[bpp * bufwidth * (bufheight - i - 1) + j] = tmp;
- }
- }
- char exbuf[6];
- for (int i = 0; i < bufheight; i++) {
- for (int j = 0; j < bufwidth / 2; j++) {
- memcpy(exbuf, (&buffer[bpp * i * bufwidth + bpp * j]), bpp);
- memcpy(&buffer[bpp * i * bufwidth + bpp * j], &buffer[bpp * i * bufwidth + bpp * (bufwidth - 1 - j)], bpp);
- memcpy(&buffer[bpp * i * bufwidth + bpp * (bufwidth - 1 - j)], exbuf, bpp);
- }
- }
- fwrite(buffer, bih.image_size, 1, fw);
- free(buffer);
- buffer = malloc(bh.file_size - bh.offset - bih.image_size);
- fread(buffer, 1, bh.file_size - bh.offset - bih.image_size, fr);
- fwrite(buffer, 1, bh.file_size - bh.offset - bih.image_size, fw);
- free(buffer);
- fclose(fr);
- fclose(fw);
- return 0;
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement