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=150 ;
- #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 {
- uint8_t b, g, r;
- } rgb;
- #pragma pack(pop)
- rgb **arraymake(uint32_t width, uint32_t height)
- {
- rgb **pixels = (rgb **)malloc (height * sizeof(rgb *));
- for (uint32_t row = 0; row < height; ++row)
- {
- pixels[row] = (rgb *)malloc(width * sizeof(rgb));
- }
- return pixels;
- }
- int main(int argc, char *argv[]) {
- if (argc < 3) {
- printf("%s <bmp in> <bmp out>\n", argv[0]);
- exit(1);
- }
- FILE *bmf = fopen(argv[1], "rb");
- FILE *bmout = fopen(argv[2], "wb");
- 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);
- }
- fseek(bmf, bih.bh.offset, SEEK_SET);
- fwrite(&bih, sizeof(bih), 1, bmout);
- //PixelData PD;
- //PD.arr=arraymake(bih.width, bih.height);
- /* PD.arr = (rgb **) malloc(bih.height * sizeof(rgb *));
- for (int row = 0; row < bih.height; ++row)
- {
- PD.arr[row] = (rgb *) malloc(bih.width * sizeof(rgb));
- }
- */
- rgb PD[bih.height][bih.width];
- for (uint32_t i = 0; i < bih.height; i++) {
- for (uint32_t j = 0; j < bih.width; j++) {
- fread(&PD[i][j], sizeof(rgb), 1, bmf);
- }
- }
- for (uint32_t i = 0; i < bih.height ; i++) {
- for (uint32_t j = 0; j < (bih.width); j++) {
- int32_t temp = j - n;
- if (temp > bih.width)
- temp=bih.width-1;
- if (temp <= 0)
- temp=0;/*if (temp>0)
- temp = bih.width+temp;
- else
- temp=bih.width-temp;
- */
- rgb temp_pix_second;
- rgb temp_pix_third;
- temp_pix_second.b=PD[i][j].b;
- temp_pix_second.g=PD[i][j].g;
- if (temp==0)
- temp_pix_second.r=(uint8_t)0;
- else{
- temp_pix_second.r = PD[i][temp].r;
- PD[i][temp].r=(uint8_t)0;
- }
- temp_pix_third=temp_pix_second;
- /*if (PD[i][j].r==(uint8_t)0)
- temp_pix_third.r=(uint8_t)0;
- else
- temp_pix_third.r=PD[i][temp].r;
- */
- fwrite(&temp_pix_third, sizeof(rgb),1,bmout);
- }
- }
- /* for (uint32_t i = 0; i < bih.height ; i++) {
- for (uint32_t j = 0; j < bih.width ; j++) {
- fwrite(&PD.arr[i][j], sizeof(rgb),1,bmout);
- }
- }
- */
- //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];
- //unsigned char **data= new unsigned char [size]="\0"; // allocate 3 bytes per pixel
- for (int ii=0;ii<size;ii++)
- fread(data, sizeof(unsigned char), 1, 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<bih.height;i++)
- {
- for (int j=0;j<bih.width; j++)
- {
- data[3*(i*bih.width+j)+2]=data[3*(i*bih.width+j)+1]+n;
- fwrite(&data[3*(i*bih.width+j)],sizeof(unsigned char),1,bmout);
- fwrite(&data[3*(i*bih.width+j)+1],sizeof(unsigned char),1,bmout);
- fwrite(&data[3*(i*bih.width+j)+2],sizeof(unsigned char),1,bmout);
- }
- }
- */
- /*
- 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