bmpdiff.c 9.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298
  1. /* ************************************************************************* */
  2. /* Organizacion del Computador II */
  3. /* */
  4. /* Buscador de diferencias en archivos BMP */
  5. /* */
  6. /* Ejmplo para obtener la cantidad de pixeles distintos por diferencia */
  7. /* bin/diff -v img1.bmp img2.bmp 0 | awk '{print $5}' | sort | uniq -c */
  8. /* */
  9. /* ************************************************************************* */
  10. #include <stdio.h>
  11. #include <stdlib.h>
  12. #include "libbmp.h"
  13. typedef struct s_options {
  14. char* program_name;
  15. int value;
  16. int verbose;
  17. int image;
  18. int help;
  19. int summaryop;
  20. int* summary;
  21. char* file1;
  22. char* file2;
  23. uint8_t epsilon;
  24. } options;
  25. void print_help(char* name);
  26. int read_options(int argc, char* argv[], options* opt);
  27. uint8_t cmp(uint8_t a, uint8_t b, int c, int r, char* channel, options* opt);
  28. int main(int argc, char* argv[]){
  29. int i, j;
  30. // (0) leer parametros
  31. options opt;
  32. if (argc == 1) {
  33. print_help(argv[0]);
  34. return 0;
  35. }
  36. if (read_options(argc, argv, &opt)) {
  37. printf("ERROR reading parameters\n");
  38. return 1;
  39. }
  40. int len1 = strlen(opt.file1);
  41. int len2 = strlen(opt.file2);
  42. if (strcmp(&(opt.file1[len1-4]),".bmp") || strcmp(&(opt.file2[len2-4]),".bmp")) {
  43. printf("ERROR: nombre del archivo\n");
  44. return -1;
  45. }
  46. // (0.1) siempre armo el summary
  47. opt.summary = (int*)malloc(sizeof(int)*256);
  48. for (i=0; i<256; i++) {
  49. opt.summary[i]=0;
  50. }
  51. // (1) leer imagenes
  52. BMP* bmp1 = bmp_read(opt.file1);
  53. BMP* bmp2 = bmp_read(opt.file2);
  54. if (bmp1 == 0 || bmp1 == 0) {
  55. printf("ERROR: no se puede abrir el archivo\n");
  56. return -1;
  57. }
  58. // (2) check tipo de archivo
  59. if (((BMPIH*)(bmp1->ih))->biSize != ((BMPIH*)(bmp1->ih))->biSize) {
  60. printf("ERROR: tipo de archivo diferente\n");
  61. return -1;
  62. }
  63. // (3) check tamaño del archivo
  64. int w1 = ((BMPIH*)(bmp1->ih))->biWidth;
  65. int h1 = ((BMPIH*)(bmp1->ih))->biHeight;
  66. int c1 = ((BMPIH*)(bmp1->ih))->biBitCount;
  67. int w2 = ((BMPIH*)(bmp2->ih))->biWidth;
  68. int h2 = ((BMPIH*)(bmp2->ih))->biHeight;
  69. int c2 = ((BMPIH*)(bmp2->ih))->biBitCount;
  70. if (w1!=w2 || h1!=h2 || c1!=c2) {
  71. printf("ERROR: tamaño de archivo diferente\n");
  72. return -1;
  73. }
  74. //printf("%i=%i %i=%i %i=%i\n",w1,w2,h1,h2,c1,c2);
  75. if (w1 % 4 != 0) {
  76. // TODO: soportar padding!
  77. printf("ERROR: padding no soportado\n");
  78. return -1;
  79. }
  80. // (3) check el bit count TODO: only 24 o 32
  81. if (c1 != 24 && c1 != 32) {
  82. printf("ERROR: (%i) bitcount distinto de 24 o 32\n", c1);
  83. return -1;
  84. }
  85. // (4) crear imagenes de diferencias
  86. BMP *bmpDiffR, *bmpDiffG, *bmpDiffB, *bmpDiffA;
  87. bmpDiffR = bmp_copy(bmp1, 0);
  88. bmpDiffG = bmp_copy(bmp1, 0);
  89. bmpDiffB = bmp_copy(bmp1, 0);
  90. if (c1 == 32) {
  91. bmpDiffA = bmp_copy(bmp1,0);
  92. }
  93. // (5) extraer data
  94. uint8_t *data1, *data2, *dataR, *dataG, *dataB, *dataA;
  95. data1 = bmp_data(bmp1);
  96. data2 = bmp_data(bmp2);
  97. dataR = bmp_data(bmpDiffR);
  98. dataG = bmp_data(bmpDiffG);
  99. dataB = bmp_data(bmpDiffB);
  100. if (c1 == 32) {
  101. dataA = bmp_data(bmpDiffA);
  102. }
  103. // (6) calcular diferencias
  104. if (c1 == 32) {
  105. for(j=0;j<h1;j++) {
  106. for(i=0;i<w1;i++) {
  107. int pos = (j*w1+i)*4;
  108. uint8_t R1 = data1[pos+3];
  109. uint8_t G1 = data1[pos+2];
  110. uint8_t B1 = data1[pos+1];
  111. uint8_t A1 = data1[pos+0];
  112. uint8_t R2 = data2[pos+3];
  113. uint8_t G2 = data2[pos+2];
  114. uint8_t B2 = data2[pos+1];
  115. uint8_t A2 = data2[pos+0];
  116. dataR[pos+0] = cmp(R1,R2,i,j,"R",&opt);
  117. dataR[pos+1] = dataR[pos+0];
  118. dataR[pos+2] = dataR[pos+30];
  119. dataR[pos+3] = 255;
  120. dataG[pos+0] = cmp(G1,G2,i,j,"G",&opt);
  121. dataG[pos+1] = dataG[pos+0];
  122. dataG[pos+2] = dataG[pos+0];
  123. dataG[pos+3] = 255;
  124. dataB[pos+0] = cmp(B1,B2,i,j,"B",&opt);
  125. dataB[pos+1] = dataB[pos+0];
  126. dataB[pos+2] = dataB[pos+0];
  127. dataB[pos+3] = 255;
  128. dataA[pos+0] = cmp(A1,A2,i,j,"A",&opt);
  129. dataA[pos+1] = dataA[pos+0];
  130. dataA[pos+2] = dataA[pos+0];
  131. dataA[pos+3] = 255;
  132. }
  133. }
  134. } else if(c1 == 24) {
  135. for(j=0;j<h1;j++) {
  136. for(i=0;i<w1;i++) {
  137. int pos = (j*w1+i)*3;
  138. uint8_t R1 = data1[pos+2];
  139. uint8_t G1 = data1[pos+1];
  140. uint8_t B1 = data1[pos+0];
  141. uint8_t R2 = data2[pos+2];
  142. uint8_t G2 = data2[pos+1];
  143. uint8_t B2 = data2[pos+0];
  144. dataR[pos+2] = cmp(R1,R2,i,j,"R",&opt);
  145. dataR[pos+1] = dataR[pos+2];
  146. dataR[pos+0] = dataR[pos+2];
  147. dataG[pos+2] = cmp(G1,G2,i,j,"G",&opt);
  148. dataG[pos+1] = dataG[pos+2];
  149. dataG[pos+0] = dataG[pos+2];
  150. dataB[pos+2] = cmp(B1,B2,i,j,"B",&opt);
  151. dataB[pos+1] = dataB[pos+2];
  152. dataB[pos+0] = dataB[pos+2];
  153. }
  154. }
  155. }
  156. // (7) mostrar summary
  157. if(opt.summaryop) {
  158. for(i=1;i<256;i++) {
  159. if(opt.summary[i]!=0) {
  160. printf("%i\t%i\n", i, opt.summary[i]);
  161. }
  162. }
  163. }
  164. // (8) guardar resultados
  165. if(opt.image) {
  166. char* strX = "diffX.bmp";
  167. char* fileSto = malloc(strlen(opt.file1) + 5 + 1);
  168. strcpy(fileSto, opt.file1);
  169. strcpy(fileSto + len1 - 4, strX);
  170. fileSto[len1]='R';
  171. bmp_save(fileSto, bmpDiffR);
  172. fileSto[len1]='G';
  173. bmp_save(fileSto, bmpDiffG);
  174. fileSto[len1]='B';
  175. bmp_save(fileSto, bmpDiffB);
  176. fileSto[len1]='A';
  177. if (c1 == 32) {
  178. bmp_save(fileSto,bmpDiffA);
  179. }
  180. // (8.1) borrar las imagenes
  181. bmp_delete(bmp1);
  182. bmp_delete(bmp2);
  183. bmp_delete(bmpDiffR);
  184. bmp_delete(bmpDiffG);
  185. bmp_delete(bmpDiffB);
  186. if (c1 == 32) {
  187. bmp_delete(bmpDiffA);
  188. }
  189. }
  190. // (9) retorno error si encontre una diferencia
  191. for (i = opt.epsilon; i < 256; i++) {
  192. if (opt.summary[i] > 0) {
  193. return -1;
  194. }
  195. }
  196. return 0;
  197. }
  198. uint8_t cmp(uint8_t a, uint8_t b, int c, int r, char* channel, options* opt) {
  199. int diff = (int)abs(((int)a)-((int)b));
  200. opt->summary[diff]++;
  201. if (opt->verbose && diff > opt->epsilon) {
  202. printf("%i\t%i\t%s\t=\t%i\n", r, c, channel, diff);
  203. }
  204. if (opt->value) {
  205. return (uint8_t)((256-diff) & 0xff);
  206. }
  207. if (diff > opt->epsilon) {
  208. return 255;
  209. }
  210. return 0;
  211. }
  212. void print_help(char* name) {
  213. printf ( "Uso: %s <opciones> <archivo_1> <archivo_2> <epsilon>\n", name );
  214. printf ( "Ejemplo de uso:\n" );
  215. printf ( " %s lena_a.bmp lena_b.bmp 5\n", name );
  216. printf ( "\n" );
  217. printf ( " Verifica pixel a pixel que la diferencia entre lena_a.bmp y \n" );
  218. printf ( " lena_b.bmp no supere el valor 5. Genera una imágen por canal \n" );
  219. printf ( " a partir de las diferencias. Coloca un pixel blanco donde \n" );
  220. printf ( " haya diferencias y uno negro donde no. Si se usa -a entonces \n" );
  221. printf ( " indica en gris el valor de la diferencia, donde negro es sin \n" );
  222. printf ( " diferencias y blanco es diferencia en 1.\n" );
  223. printf ( "\n" );
  224. printf ( " -h, --help Imprime esta ayuda\n" );
  225. printf ( " -a, --value Valor de la diferencia en la imagen\n" );
  226. printf ( " -v, --verbose Ejecuta en verbose mostrando las diferencias\n" );
  227. printf ( " -s, --summary Muestra un resumen de diferencias\n" );
  228. printf ( " -i, --image Genera Imagenes de diferencias\n" );
  229. }
  230. int read_options(int argc, char* argv[], options* opt) {
  231. opt->program_name = argv[0];
  232. opt->verbose = 0;
  233. opt->value = 0;
  234. opt->help = 0;
  235. opt->file1 = 0;
  236. opt->file2 = 0;
  237. opt->image = 0;
  238. opt->summaryop = 0;
  239. int i, optionals = 0;
  240. for (i=1; i < argc; i++) {
  241. if (!strcmp(argv[i], "-h") || !strcmp(argv[i], "--help")) {
  242. opt->help = 1;
  243. optionals++;
  244. }
  245. if (!strcmp(argv[i], "-v") || !strcmp(argv[i], "--verbose")) {
  246. opt->verbose = 1;
  247. optionals++;
  248. }
  249. if (!strcmp(argv[i], "-a") || !strcmp(argv[i], "--value")) {
  250. opt->value = 1;
  251. optionals++;
  252. }
  253. if (!strcmp(argv[i], "-i") || !strcmp(argv[i], "--image")) {
  254. opt->image = 1;
  255. optionals++;
  256. }
  257. if (!strcmp(argv[i], "-s") || !strcmp(argv[i], "--summary")) {
  258. opt->summaryop = 1;
  259. optionals++;
  260. }
  261. }
  262. if (argc - 1 - optionals != 3) {
  263. return 1;
  264. }
  265. opt->epsilon = atoi(argv[argc-1]);
  266. opt->file1 = argv[argc-2];
  267. opt->file2 = argv[argc-3];
  268. return 0;
  269. }