fork download
  1. #include <stdio.h>
  2.  
  3. #include <stdlib.h>
  4.  
  5.  
  6.  
  7. int upside_down(const char *fname_s, const char *fname_t) {
  8.  
  9. FILE *fp_s = NULL; // source file handler
  10.  
  11. FILE *fp_t = NULL; // target file handler
  12.  
  13. unsigned int x,y; // for loop counter
  14.  
  15. unsigned int width, height; // image width, image height
  16.  
  17. unsigned char *image_s = NULL; // source image array
  18.  
  19. unsigned char *image_t = NULL; // target image array
  20.  
  21. unsigned int R, G, B; // color of R, G, B
  22.  
  23. unsigned int y_avg; // average of y axle
  24.  
  25. unsigned int y_t; // target of y axle
  26.  
  27.  
  28.  
  29. unsigned char header[54] = {
  30.  
  31. 0x42, // identity : B
  32.  
  33. 0x4d, // identity : M
  34.  
  35. 0, 0, 0, 0, // file size
  36.  
  37. 0, 0, // reserved1
  38.  
  39. 0, 0, // reserved2
  40.  
  41. 54, 0, 0, 0, // RGB data offset
  42.  
  43. 40, 0, 0, 0, // struct BITMAPINFOHEADER size
  44.  
  45. 0, 0, 0, 0, // bmp width
  46.  
  47. 0, 0, 0, 0, // bmp height
  48.  
  49. 1, 0, // planes
  50.  
  51. 24, 0, // bit per pixel
  52.  
  53. 0, 0, 0, 0, // compression
  54.  
  55. 0, 0, 0, 0, // data size
  56.  
  57. 0, 0, 0, 0, // h resolution
  58.  
  59. 0, 0, 0, 0, // v resolution
  60.  
  61. 0, 0, 0, 0, // used colors
  62.  
  63. 0, 0, 0, 0 // important colors
  64.  
  65. };
  66.  
  67.  
  68.  
  69. unsigned int file_size; // file size
  70.  
  71. unsigned int rgb_raw_data_offset; // RGB raw data offset
  72.  
  73.  
  74.  
  75. fp_s = fopen(fname_s, "rb");
  76.  
  77. if (fp_s == NULL) {
  78.  
  79. printf("fopen fp_s error\n");
  80.  
  81. return -1;
  82.  
  83. }
  84.  
  85.  
  86.  
  87. // move offset to 10 to find rgb raw data offset
  88.  
  89. fseek(fp_s, 10, SEEK_SET);
  90.  
  91. fread(&rgb_raw_data_offset, sizeof(unsigned int), 1, fp_s);
  92.  
  93. // move offset to 18 to get width & height;
  94.  
  95. fseek(fp_s, 18, SEEK_SET);
  96.  
  97. fread(&width, sizeof(unsigned int), 1, fp_s);
  98.  
  99. fread(&height, sizeof(unsigned int), 1, fp_s);
  100.  
  101.  
  102.  
  103. // move offset to rgb_raw_data_offset to get RGB raw data
  104.  
  105. fseek(fp_s, rgb_raw_data_offset, SEEK_SET);
  106.  
  107.  
  108.  
  109. image_s = (unsigned char *)malloc((size_t)width * height * 3);
  110.  
  111. if (image_s == NULL) {
  112.  
  113. printf("malloc images_s error\n");
  114.  
  115. return -1;
  116.  
  117. }
  118.  
  119.  
  120.  
  121. image_t = (unsigned char *)malloc((size_t)width * height * 3);
  122.  
  123. if (image_t == NULL) {
  124.  
  125. printf("malloc image_t error\n");
  126.  
  127. return -1;
  128.  
  129. }
  130.  
  131.  
  132.  
  133. fread(image_s, sizeof(unsigned char), (size_t)(long)width * height * 3, fp_s);
  134.  
  135.  
  136.  
  137.  
  138.  
  139. y_avg = 0 + (height-1);
  140.  
  141.  
  142.  
  143. for(y = 0; y != height; ++y) {
  144.  
  145. for(x = 0; x != width; ++x) {
  146.  
  147. R = *(image_s + 3 * (width * y + x) + 2);
  148.  
  149. G = *(image_s + 3 * (width * y + x) + 1);
  150.  
  151. B = *(image_s + 3 * (width * y + x) + 0);
  152.  
  153.  
  154.  
  155. int num[10];
  156.  
  157. num[1] = R;
  158.  
  159. num[2] = G;
  160.  
  161. num[3] = B;
  162.  
  163.  
  164.  
  165. int max = 0;
  166.  
  167.  
  168.  
  169.  
  170.  
  171.  
  172.  
  173. for(int j = 0; j < 2; ++j)
  174.  
  175. {
  176.  
  177. if (num[j] > max)
  178.  
  179. max = num[j];
  180.  
  181.  
  182.  
  183.  
  184.  
  185. }
  186.  
  187.  
  188.  
  189.  
  190.  
  191. int min = 255;
  192.  
  193.  
  194.  
  195.  
  196.  
  197.  
  198.  
  199. for(int m = 0; m < 2; ++m)
  200.  
  201. {
  202.  
  203. if (num[m] < min)
  204.  
  205. min = num[m];
  206.  
  207.  
  208.  
  209. }
  210.  
  211.  
  212.  
  213.  
  214.  
  215.  
  216.  
  217. printf("%d%s%d%s%d%s%d%s%d\n",R,"_",G,"_",B,"_",min,"_",max);
  218.  
  219.  
  220.  
  221.  
  222.  
  223.  
  224.  
  225.  
  226.  
  227. y_t = y;
  228.  
  229.  
  230.  
  231. *(image_t + 3 * (width * y_t + x) + 2) = R;
  232.  
  233. *(image_t + 3 * (width * y_t + x) + 1) = G;
  234.  
  235. *(image_t + 3 * (width * y_t + x) + 0) = B;
  236.  
  237.  
  238.  
  239.  
  240.  
  241.  
  242.  
  243.  
  244.  
  245.  
  246.  
  247.  
  248.  
  249.  
  250.  
  251.  
  252.  
  253.  
  254.  
  255.  
  256.  
  257. }
  258.  
  259. }
  260.  
  261.  
  262.  
  263.  
  264.  
  265.  
  266.  
  267.  
  268.  
  269.  
  270.  
  271.  
  272.  
  273. // write to new bmp
  274.  
  275. fp_t = fopen(fname_t, "wb");
  276.  
  277. if (fp_t == NULL) {
  278.  
  279. printf("fopen fname_t error\n");
  280.  
  281. return -1;
  282.  
  283. }
  284.  
  285.  
  286.  
  287. // file size
  288.  
  289. file_size = width * height * 3 + rgb_raw_data_offset;
  290.  
  291. header[2] = (unsigned char)(file_size & 0x000000ff);
  292.  
  293. header[3] = (file_size >> 8) & 0x000000ff;
  294.  
  295. header[4] = (file_size >> 16) & 0x000000ff;
  296.  
  297. header[5] = (file_size >> 24) & 0x000000ff;
  298.  
  299.  
  300.  
  301. // width
  302.  
  303. header[18] = width & 0x000000ff;
  304.  
  305. header[19] = (width >> 8) & 0x000000ff;
  306.  
  307. header[20] = (width >> 16) & 0x000000ff;
  308.  
  309. header[21] = (width >> 24) & 0x000000ff;
  310.  
  311.  
  312.  
  313. // height
  314.  
  315. header[22] = height &0x000000ff;
  316.  
  317. header[23] = (height >> 8) & 0x000000ff;
  318.  
  319. header[24] = (height >> 16) & 0x000000ff;
  320.  
  321. header[25] = (height >> 24) & 0x000000ff;
  322.  
  323.  
  324.  
  325. // write header
  326.  
  327. fwrite(header, sizeof(unsigned char), rgb_raw_data_offset, fp_t);
  328.  
  329. // write image
  330.  
  331. fwrite(image_t, sizeof(unsigned char), (size_t)(long)width *
Compilation error #stdin compilation error #stdout 0s 0KB
stdin
Standard input is empty
compilation info
prog.cpp: In function ‘int upside_down(const char*, const char*)’:
prog.cpp:331: error: expected primary-expression at end of input
prog.cpp:331: error: expected `;' at end of input
prog.cpp:331: error: expected `}' at end of input
prog.cpp:91: warning: ignoring return value of ‘size_t fread(void*, size_t, size_t, FILE*)’, declared with attribute warn_unused_result
prog.cpp:97: warning: ignoring return value of ‘size_t fread(void*, size_t, size_t, FILE*)’, declared with attribute warn_unused_result
prog.cpp:99: warning: ignoring return value of ‘size_t fread(void*, size_t, size_t, FILE*)’, declared with attribute warn_unused_result
prog.cpp:133: warning: ignoring return value of ‘size_t fread(void*, size_t, size_t, FILE*)’, declared with attribute warn_unused_result
prog.cpp:327: warning: ignoring return value of ‘size_t fwrite(const void*, size_t, size_t, FILE*)’, declared with attribute warn_unused_result
stdout
Standard output is empty