Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <stdio.h>
- #include <stdint.h>
- #include <stdlib.h>
- #include <string.h>
- #pragma pack(push, 1)
- typedef struct bmpHeader{
- uint16_t bfType;
- uint32_t bfSize, bfReserved, bfOffBits;
- }bmpHeader;
- typedef struct bmpInfoHeader{
- uint32_t biSize,biWidth,biHeight;
- uint16_t biPlanes,biBitCount;
- uint32_t biCompression, biSizeImage, biPelsPerMeter[2], biClrUsed, biClrImportant;
- }bmpInfoHeader;
- typedef struct pixel{
- unsigned char B;
- unsigned char G;
- unsigned char R;
- }pixel;
- #pragma pack(pop)
- int main(int argc, char *argv[]) {
- if (argc < 2) {
- printf("%s <input.bmp> <offset> \n", argv[0]);
- exit(1);
- }
- FILE *input ;
- input = fopen(argv[1], "rb");
- FILE *output;
- if (input == NULL) {
- exit(1);
- }
- if (argv[3] != NULL ) {
- output = fopen(argv[3], "wb");
- } else {
- output = fopen("NewBMP.bmp", "wb");
- }
- int offset = atoi(argv[2]);
- bmpHeader header;
- bmpInfoHeader infoHeader;
- fread(&header, sizeof(header), 1, input);
- fread(&infoHeader, sizeof(infoHeader), 1, input);
- pixel *pixBuff = malloc(header.bfSize - header.bfOffBits);
- pixel *offsetBuff = malloc(sizeof(pixel) * (abs(offset)));
- fread(pixBuff, sizeof(pixel), (infoHeader.biWidth*infoHeader.biHeight), input);
- if (offset>=0) {
- for (int x = 0; x < infoHeader.biHeight; x++) {
- for (int y = infoHeader.biWidth; y > (infoHeader.biWidth-offset) ; y--) {
- offsetBuff[infoHeader.biWidth - y] = pixBuff[x * infoHeader.biWidth + y -1];
- }
- for (int y = infoHeader.biWidth; y >offset ; y--) {
- pixBuff[x * infoHeader.biWidth + y -1].R = pixBuff[x * infoHeader.biWidth + y - offset -1].R ;
- }
- for (int y = 0; y < offset ; y++) {
- pixBuff[x * infoHeader.biWidth + y ].R = offsetBuff[offset-y].R ;
- }
- }
- } else{
- offset = abs(offset);
- for (int x = 0; x < infoHeader.biHeight; x++) {
- for (int y = 0; y < offset ; y++) {
- offsetBuff[y] = pixBuff[x * infoHeader.biWidth + y ];
- }
- for (int y = 0; y <infoHeader.biWidth -offset ; y++) {
- pixBuff[x * infoHeader.biWidth + y ].R = pixBuff[x * infoHeader.biWidth + y + offset].R ;
- }
- for (int y = infoHeader.biWidth-offset; y < infoHeader.biWidth ; y++) {
- pixBuff[x * infoHeader.biWidth + y ].R = offsetBuff[y - infoHeader.biWidth + offset].R ;
- }
- }
- }
- fwrite(&header, sizeof(header),1,output);
- fwrite(&infoHeader, sizeof(infoHeader),1,output);
- fwrite(pixBuff, sizeof(pixel), (infoHeader.biWidth*infoHeader.biHeight),output);
- fclose(output);
- fclose(input);
- return 0;
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement