Advertisement
Guest User

Untitled

a guest
May 21st, 2019
112
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.83 KB | None | 0 0
  1. #include <stdio.h>
  2. #include <stdint.h>
  3. #include <stdlib.h>
  4. #include <string.h>
  5.  
  6. #pragma pack(push, 1)
  7.  
  8. typedef struct bmpHeader{
  9. uint16_t bfType;
  10. uint32_t bfSize, bfReserved, bfOffBits;
  11. }bmpHeader;
  12.  
  13. typedef struct bmpInfoHeader{
  14. uint32_t biSize,biWidth,biHeight;
  15. uint16_t biPlanes,biBitCount;
  16. uint32_t biCompression, biSizeImage, biPelsPerMeter[2], biClrUsed, biClrImportant;
  17. }bmpInfoHeader;
  18.  
  19. typedef struct pixel{
  20. unsigned char B;
  21. unsigned char G;
  22. unsigned char R;
  23. }pixel;
  24.  
  25. #pragma pack(pop)
  26.  
  27. int main(int argc, char *argv[]) {
  28. if (argc < 2) {
  29. printf("%s <input.bmp> <offset> \n", argv[0]);
  30. exit(1);
  31. }
  32. FILE *input ;
  33. input = fopen(argv[1], "rb");
  34. FILE *output;
  35. if (input == NULL) {
  36. exit(1);
  37. }
  38. if (argv[3] != NULL ) {
  39. output = fopen(argv[3], "wb");
  40. } else {
  41. output = fopen("NewBMP.bmp", "wb");
  42. }
  43. int offset = atoi(argv[2]);
  44. bmpHeader header;
  45. bmpInfoHeader infoHeader;
  46. fread(&header, sizeof(header), 1, input);
  47. fread(&infoHeader, sizeof(infoHeader), 1, input);
  48.  
  49. pixel *pixBuff = malloc(header.bfSize - header.bfOffBits);
  50. pixel *offsetBuff = malloc(sizeof(pixel) * (abs(offset)));
  51.  
  52. fread(pixBuff, sizeof(pixel), (infoHeader.biWidth*infoHeader.biHeight), input);
  53.  
  54. if (offset>=0) {
  55.  
  56. for (int x = 0; x < infoHeader.biHeight; x++) {
  57. for (int y = infoHeader.biWidth; y > (infoHeader.biWidth-offset) ; y--) {
  58. offsetBuff[infoHeader.biWidth - y] = pixBuff[x * infoHeader.biWidth + y -1];
  59. }
  60. for (int y = infoHeader.biWidth; y >offset ; y--) {
  61. pixBuff[x * infoHeader.biWidth + y -1].R = pixBuff[x * infoHeader.biWidth + y - offset -1].R ;
  62. }
  63. for (int y = 0; y < offset ; y++) {
  64. pixBuff[x * infoHeader.biWidth + y ].R = offsetBuff[offset-y].R ;
  65. }
  66. }
  67. } else{
  68. offset = abs(offset);
  69. for (int x = 0; x < infoHeader.biHeight; x++) {
  70. for (int y = 0; y < offset ; y++) {
  71. offsetBuff[y] = pixBuff[x * infoHeader.biWidth + y ];
  72. }
  73. for (int y = 0; y <infoHeader.biWidth -offset ; y++) {
  74. pixBuff[x * infoHeader.biWidth + y ].R = pixBuff[x * infoHeader.biWidth + y + offset].R ;
  75. }
  76. for (int y = infoHeader.biWidth-offset; y < infoHeader.biWidth ; y++) {
  77. pixBuff[x * infoHeader.biWidth + y ].R = offsetBuff[y - infoHeader.biWidth + offset].R ;
  78. }
  79. }
  80. }
  81.  
  82. fwrite(&header, sizeof(header),1,output);
  83. fwrite(&infoHeader, sizeof(infoHeader),1,output);
  84. fwrite(pixBuff, sizeof(pixel), (infoHeader.biWidth*infoHeader.biHeight),output);
  85.  
  86. fclose(output);
  87. fclose(input);
  88.  
  89. return 0;
  90. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement