Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <stdio.h>
- #include <stdint.h>
- #include <stdlib.h>
- const n=100;
- #pragma pack(push, 1)
- #define DEBUG 1
- #if DEBUG
- #define LOG(fmt, ...) fprintf(stderr, fmt "\n", __VA_ARGS__)
- #else
- #define LOG(...)
- #endif
- typedef struct BmpHeader {
- uint16_t signature;
- uint32_t file_zize, reserved, offset;
- } BmpHeader;
- typedef struct BmpInfoHeader {
- BmpHeader bh;
- uint32_t size, width, height;
- uint16_t planes,bpp;
- uint32_t compression, image_size, unused[2], colors, important;
- } BmpInfoHeader;
- typedef struct rgb {
- union {
- struct {
- uint8_t b, g, r;
- };
- uint8_t start;
- };
- } rgb;
- #pragma pack(pop)
- int main(int argc, char *argv[]) {
- if (argc < 3) {
- printf("%s <bmp in> <bmp out>\n", argv[0]);
- exit(1);
- }
- //char *tmpEnd;
- //int brighten = strtol(argv[1], &tmpEnd, 10);
- //LOG("____________________________________\nPercentage: %d", brighten);
- FILE *bmf = fopen(argv[1], "rb");
- //FILE *bmpout = fopen(argv[3], "wb");
- //LOG("Input file name: %s\nOutput file name: %s\n", argv[2], argv[3]);
- BmpInfoHeader bih;
- fread(&bih, sizeof bih, 1, bmf);
- BmpHeader bh = bih.bh;
- if (bih.size != 40 || bih.planes != 1 || bh.reserved != 0 || bih.compression > 2) {
- printf("BMP file corrupted");
- exit(2);
- }
- if (bih.bpp != 24) {
- printf("bpp %hd not supported\n", bih.bpp);
- exit(3);
- }
- LOG("size %d, offset 0x%x\n", bih.bh.file_zize, bih.bh.offset);
- LOG("bpp %hd, compression %ld, colors %d\n", bih.bpp,bih.compression, bih.colors);
- fseek(bmf, bih.bh.offset, SEEK_SET);
- FILE *temp=fopen(argv[1],"rb");
- unsigned char info[54];
- fread(info, sizeof(unsigned char), 54, temp); // read the 54-byte header
- // extract image height and width from header
- int width = *(int*)&info[18];
- int height = *(int*)&info[22];
- int size = 3 * width * height;
- unsigned char data [size]; // allocate 3 bytes per pixel
- fread(data, sizeof(unsigned char), size, temp); // read the rest of the data at once
- FILE *bmout=fopen(argv[2],"wb");
- //fwrite(&bih, sizeof bih, 1, bmout);
- for(int i = 0; i < size; i += 3)
- {
- unsigned char tmp = data[i];
- data[i] = data[i+2];
- data[i+2] = tmp;
- }
- /*
- FILE *bmout=fopen(argv[2],"wb");
- fwrite(&bih, sizeof bih, 1, bmout);
- for (int i=0;i<bih.width*bih.height;i++)
- {
- rgb pixel;
- fread(&pixel,sizeof pixel, 1, bmf);//1 15 57
- if(pixel.g+n<bih.width)
- pixel.r = pixel.g + n;
- else
- pixel.r=bih.width;
- for(int j=0;j<3;j++)
- {
- uint8_t *subpix=&pixel.start+j;
- }
- fwrite(&pixel,sizeof pixel,1,bmout);
- }
- fclose(bmout);*/
- fclose(bmf);
- return 0;
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement