fork download
  1. #define WIDTH 320
  2.  
  3. #define HEIGHT 240
  4.  
  5.  
  6.  
  7. #include<stdio.h>
  8.  
  9. #include<stdlib.h>
  10.  
  11.  
  12.  
  13. typedef struct color{
  14.  
  15. unsigned char r;
  16.  
  17. unsigned char g;
  18.  
  19. unsigned char b;
  20.  
  21. }COLOR;
  22.  
  23.  
  24.  
  25. COLOR data[WIDTH][HEIGHT];
  26.  
  27. COLOR data2[WIDTH][HEIGHT] = {0};
  28.  
  29. unsigned char graydata[WIDTH][HEIGHT];
  30.  
  31. int filter[3][3] = {{ -1, -1, -1 },
  32.  
  33. { -1, 8, -1 },
  34.  
  35. { -1, -1, -1}};
  36.  
  37.  
  38.  
  39. int main(){
  40.  
  41. int i, j, ii, jj;
  42.  
  43. int red, green, blue;
  44.  
  45. FILE *fp;
  46.  
  47. FILE *out;
  48.  
  49.  
  50.  
  51. char header[54];
  52.  
  53.  
  54.  
  55. //エラー処理
  56.  
  57. if((fp = fopen("image2.bmp", "rb")) == NULL){
  58.  
  59. fprintf(stderr, "file open error");
  60.  
  61. exit(1);
  62.  
  63. }
  64.  
  65. if((out = fopen("out.bmp", "wb")) == NULL){
  66.  
  67. fprintf(stderr, "file open error");
  68.  
  69. exit(1);
  70.  
  71. }
  72.  
  73.  
  74.  
  75. fread(header, 1, 54, fp);
  76.  
  77.  
  78.  
  79. for(j=HEIGHT-1; j>=0; j--){
  80.  
  81. for(i=0; i<WIDTH; i++){
  82.  
  83. data[i][j].b = getc(fp);
  84.  
  85. data[i][j].g = getc(fp);
  86.  
  87. data[i][j].r = getc(fp);
  88.  
  89. }
  90.  
  91. }
  92.  
  93.  
  94.  
  95. fclose(fp);
  96.  
  97.  
  98.  
  99. //グレースケール
  100.  
  101. for(i=0; i<WIDTH; i++){
  102.  
  103. for(j=0; j<HEIGHT; j++){
  104.  
  105. graydata[i][j] = (data[i][j].r + data[i][j].g + data[i][j].b)/3;
  106.  
  107. }
  108.  
  109. }
  110.  
  111.  
  112.  
  113. //エッジ抽出
  114.  
  115. for(j=0; j<HEIGHT; j++){
  116.  
  117. data2[0][j].r = graydata[0][j].r;
  118.  
  119. data2[0][j].g = graydata[0][j].g;
  120.  
  121. data2[0][j].b = graydata[0][j].b;
  122.  
  123. }
  124.  
  125. for(i=1; i<WIDTH-1; i++){
  126.  
  127. data2[i][0].r = graydata[i][0].r;
  128.  
  129. data2[i][0].g = graydata[i][0].g;
  130.  
  131. data2[i][0].b = graydata[i][0].b;
  132.  
  133. for(j=1; j<HEIGHT-1; j++){
  134.  
  135. red = 0, green = 0, blue = 0;
  136.  
  137. for(ii=0; ii<3; ii++){
  138.  
  139. for(jj=0; jj<3; jj++){
  140.  
  141. red += graydata[i+ii-1][j+jj-1].r * filter[ii][jj];
  142.  
  143. green += graydata[i+ii-1][j+jj-1].g * filter[ii][jj];
  144.  
  145. blue += graydata[i+ii-1][j+jj-1].b * filter[ii][jj];
  146.  
  147. }
  148.  
  149. }
  150.  
  151. //0未満の値は0に、255超える値を255にする
  152.  
  153. if(red < 0) red = 0;
  154.  
  155. else if(red > 255) red = 255;
  156.  
  157. if(green < 0) green = 0;
  158.  
  159. else if(green >255) green = 255;
  160.  
  161. if(blue < 0) blue = 0;
  162.  
  163. else if(blue > 255) blue = 255;
  164.  
  165.  
  166.  
  167. data2[i][j].r = red;
  168.  
  169. data2[i][j].g = green;
  170.  
  171. data2[i][j].b = blue;
  172.  
  173. }
  174.  
  175. data2[i][HEIGHT-1].r = graydata[i][HEIGHT-1].r;
  176.  
  177. data2[i][HEIGHT-1].g = graydata[i][HEIGHT-1].g;
  178.  
  179. data2[i][HEIGHT-1].b = graydata[i][HEIGHT-1].b;
  180.  
  181. }
  182.  
  183. for(j=0; j<HEIGHT; j++){
  184.  
  185. data2[WIDTH-1][j].r = graydata[WIDTH-1][j].r;
  186.  
  187. data2[WIDTH-1][j].g = graydata[WIDTH-1][j].g;
  188.  
  189. data2[WIDTH-1][j].b = graydata[WIDTH-1][j].b;
  190.  
  191. }
  192.  
  193.  
  194.  
  195. fwrite(header, 1, 54, out);
  196.  
  197.  
  198.  
  199. //出力
  200.  
  201. for(j=HEIGHT-1; j>=0; j--){
  202.  
  203. for(i=0; i<WIDTH; i++){
  204.  
  205. fputc(data2[i][j].b, out);
  206.  
  207. fputc(data2[i][j].g, out);
  208.  
  209. fputc(data2[i][j].r, out);
  210.  
  211. }
  212.  
  213. }
  214.  
  215. fclose(out);
  216.  
  217.  
  218.  
  219. return 0;
  220.  
  221. }
Compilation error #stdin compilation error #stdout 0s 0KB
stdin
Standard input is empty
compilation info
prog.cpp:27: warning: missing braces around initializer for ‘COLOR [240]’
prog.cpp:27: warning: missing braces around initializer for ‘COLOR’
prog.cpp: In function ‘int main()’:
prog.cpp:117: error: request for member ‘r’ in ‘graydata[0][j]’, which is of non-class type ‘unsigned char’
prog.cpp:119: error: request for member ‘g’ in ‘graydata[0][j]’, which is of non-class type ‘unsigned char’
prog.cpp:121: error: request for member ‘b’ in ‘graydata[0][j]’, which is of non-class type ‘unsigned char’
prog.cpp:127: error: request for member ‘r’ in ‘graydata[i][0]’, which is of non-class type ‘unsigned char’
prog.cpp:129: error: request for member ‘g’ in ‘graydata[i][0]’, which is of non-class type ‘unsigned char’
prog.cpp:131: error: request for member ‘b’ in ‘graydata[i][0]’, which is of non-class type ‘unsigned char’
prog.cpp:141: error: request for member ‘r’ in ‘graydata[((i + ii) + -0x000000001)][((j + jj) + -0x000000001)]’, which is of non-class type ‘unsigned char’
prog.cpp:143: error: request for member ‘g’ in ‘graydata[((i + ii) + -0x000000001)][((j + jj) + -0x000000001)]’, which is of non-class type ‘unsigned char’
prog.cpp:145: error: request for member ‘b’ in ‘graydata[((i + ii) + -0x000000001)][((j + jj) + -0x000000001)]’, which is of non-class type ‘unsigned char’
prog.cpp:175: error: request for member ‘r’ in ‘graydata[i][239]’, which is of non-class type ‘unsigned char’
prog.cpp:177: error: request for member ‘g’ in ‘graydata[i][239]’, which is of non-class type ‘unsigned char’
prog.cpp:179: error: request for member ‘b’ in ‘graydata[i][239]’, which is of non-class type ‘unsigned char’
prog.cpp:185: error: request for member ‘r’ in ‘graydata[319][j]’, which is of non-class type ‘unsigned char’
prog.cpp:187: error: request for member ‘g’ in ‘graydata[319][j]’, which is of non-class type ‘unsigned char’
prog.cpp:189: error: request for member ‘b’ in ‘graydata[319][j]’, which is of non-class type ‘unsigned char’
prog.cpp:75: warning: ignoring return value of ‘size_t fread(void*, size_t, size_t, FILE*)’, declared with attribute warn_unused_result
prog.cpp:195: 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