Italiano
HOME | PROGETTI | CLASSI C++ | Sorgenti c\c++ | Galleria | Guestbook | Server BW | Contattami | Login

<< Sovrapporre immagini BMP >>

visiste: 1705


Semplicissimo algoritmo che mostra come sovrapporre due immagini rendendole visibili entrambe. Mettendo dei pesi si puo decidere quale rendere piu visibile.



  1. //*************************************//
  2. //******** created by mamo139 *********//
  3. //*** http://mamo139.altervista.org ***//
  4. //*************************************//
  5.  
  6. #include <stdio.h>
  7. #include <stdlib.h>
  8. #include <string.h>
  9. #include <math.h>
  10.  
  11.  
  12.  
  13. //*** strumenti gestione file bmp ***//
  14. struct bmpfile {
  15. long x;
  16. long y;
  17. char *** matrice;
  18. };
  19. char *** crea_bmp(char *nome, long x, long y);
  20. struct bmpfile carica_bmp(char *nome);
  21. void disegna_bmp(char *nome, char *** array, long x, long y);
  22.  
  23. //*** strumenti gestione colori ***//
  24. struct rgb {
  25. int red;
  26. int green;
  27. int blue;
  28. };
  29. struct rgb_d {
  30. double red;
  31. double green;
  32. double blue;
  33. };
  34. struct hsv {
  35. int hue;
  36. int saturation;
  37. int value;
  38. };
  39. struct hsv_d {
  40. double hue;
  41. double saturation;
  42. double value;
  43. };
  44. struct rgb hsv_rgb(struct hsv hsv_map);
  45. struct hsv rgb_hsv(struct rgb rgb_map);
  46.  
  47. //*** MAIN ***//
  48. int main (void){
  49.  
  50. double peso1 = 0.65;
  51. double peso2 = 0.65;
  52.  
  53. long x,y;
  54. struct hsv hsv1;
  55. struct hsv hsv2;
  56. struct hsv hsvf;
  57. struct rgb rgbf;
  58. struct rgb rgb1;
  59. struct rgb rgb2;
  60.  
  61. struct bmpfile img1 = carica_bmp("a.bmp");
  62. struct bmpfile img2 = carica_bmp("b.bmp");
  63.  
  64. if(img1.x>img2.x) x = img2.x;
  65. else x = img1.x;
  66. if(img1.y>img2.y) y = img2.y;
  67. else y = img1.y;
  68.  
  69. char *** mappanf = crea_bmp("out.bmp",x,y);
  70.  
  71. long i,j;
  72. for(i=0;i<x;i++)
  73. for(j=0;j<y;j++){
  74. rgb1.red = (unsigned char) img1.matrice[i][j][2];
  75. rgb1.green = (unsigned char) img1.matrice[i][j][1];
  76. rgb1.blue = (unsigned char) img1.matrice[i][j][0];
  77. rgb2.red = (unsigned char) img2.matrice[i][j][2];
  78. rgb2.green = (unsigned char) img2.matrice[i][j][1];
  79. rgb2.blue = (unsigned char) img2.matrice[i][j][0];
  80.  
  81. rgbf.red=(int)(rgb1.red*peso1+rgb2.red*peso2);
  82. if(rgbf.red>255) rgbf.red = 255;
  83. rgbf.green=(int)(rgb1.green*peso1+rgb2.green*peso2);
  84. if(rgbf.green>255) rgbf.green = 255;
  85. rgbf.blue=(int)(rgb1.blue*peso1+rgb2.blue*peso2);
  86. if(rgbf.blue>255) rgbf.blue = 255;
  87.  
  88. mappanf[i][j][2]=rgbf.red;
  89. mappanf[i][j][1]=rgbf.green;
  90. mappanf[i][j][0]=rgbf.blue;
  91. }
  92.  
  93. disegna_bmp("out.bmp",mappanf,x,y);
  94.  
  95.  
  96. }
  97.  
  98. //****** FUNZIONI *******//
  99. char *** crea_bmp(char *nome, long x, long y){
  100.  
  101. char buffer [55] = "\x42\x4d\x36\x39\xb1\x00\x00\x00\x00\x00" \
  102. "\x36\x00\x00\x00\x28\x00\x00\x00\xe0\x08\x00\x00\xa8\x06" \
  103. "\x00\x00\x01\x00\x18\x00\x00\x00\x00\x00\x00\x00\x00\x00" \
  104. "\xc4\x0e\x00\x00\xc4\x0e\x00\x00\x00\x00\x00\x00\x00\x00" \
  105. "\x00\x00";
  106.  
  107. buffer[18] = x%256;//asse x
  108. buffer[19] = x/256;
  109. buffer[22] = y%256;//asse y
  110. buffer[23] = y/256;
  111.  
  112. FILE *stream;
  113. stream = fopen(nome,"wb");
  114. fwrite(buffer, 1, 54, stream);
  115. fclose(stream);
  116.  
  117. char *** array;
  118. int i, j, h;
  119. array = (char***)malloc(x * sizeof(char *));
  120. for(i=0; i<x; i++) {
  121. array[i] = (char**)malloc(y * sizeof(char *));
  122. for (j=0; j<y; j++)
  123. array[i][j] = (char*)malloc(3 * sizeof(char *));
  124. }
  125.  
  126. int i1,i2,i3;
  127. for(i1=0; i1<x; i1++)
  128. for (i2=0; i2<y; i2++)
  129. for (i3=0; i3<3; i3++)
  130. array[i1][i2][i3] = 0;
  131.  
  132. return array;
  133. }
  134.  
  135. struct bmpfile carica_bmp(char *nome){
  136. struct bmpfile dati;
  137. long filesize,b,i,j,z=0;
  138. char *** array;
  139. char * buffer;
  140. FILE * file;
  141.  
  142. file = fopen(nome,"rb");
  143. fseek (file , 0 , SEEK_END);
  144. filesize = ftell(file);
  145. rewind(file);
  146.  
  147. buffer = (char *) malloc(filesize * sizeof(char *));
  148.  
  149. b = fread( buffer, 1, 54, file);
  150.  
  151. dati.x = ((unsigned char)buffer[18])+((unsigned char)buffer[19])*256;
  152. dati.y = ((unsigned char)buffer[22])+((unsigned char)buffer[23])*256;
  153.  
  154.  
  155. b = fread( buffer, 1, filesize, file);
  156.  
  157.  
  158.  
  159. array = (char***)malloc(dati.x * sizeof(char *));
  160. for(i=0; i<dati.x; i++) {
  161. array[i] = (char**)malloc(dati.y * sizeof(char *));
  162. for (j=0; j<dati.y; j++)
  163. array[i][j] = (char*)malloc(3 * sizeof(char *));
  164. }
  165. for(i=0; i<dati.x; i++)
  166. for (j=0; j<dati.y; j++){
  167. array[i][j][0]=buffer[z];
  168. array[i][j][1]=buffer[z+1];
  169. array[i][j][2]=buffer[z+2];
  170. z=z+3;
  171. }
  172.  
  173.  
  174. dati.matrice = array;
  175.  
  176. return dati;
  177. }
  178.  
  179. void disegna_bmp(char *nome, char *** array, long x, long y){
  180.  
  181. FILE *stream;
  182. stream = fopen(nome,"ab");
  183.  
  184. int i1,i2;
  185. for(i1=0; i1<x; i1++)
  186. for (i2=0; i2<y; i2++)
  187. fwrite(array[i1][i2], 1, 3, stream);
  188.  
  189. fclose(stream);
  190. }
  191.  
  192.  
  193.  
  194. struct rgb hsv_rgb(struct hsv hsv_map){
  195.  
  196. struct rgb_d rgb_map;
  197. struct rgb rgb_int;
  198.  
  199.  
  200. double H = ((double)hsv_map.hue);
  201. double S = ((double)hsv_map.saturation)/255;
  202. double V = ((double)hsv_map.value)/255;
  203.  
  204. double hi = ((int)(H/60))%6;
  205. double f = (H/60) - (double)hi;
  206. double p = V*(1-S);
  207. double q = V*(1-(f*S));
  208. double t = V*(1-(1-f)*S);
  209.  
  210. if (hi == 0){ rgb_map.red=V; rgb_map.green=t; rgb_map.blue=p;}
  211. else if (hi == 1){ rgb_map.red=q*255; rgb_map.green=V; rgb_map.blue=p;}
  212. else if (hi == 2){ rgb_map.red=p*255; rgb_map.green=V; rgb_map.blue=t;}
  213. else if (hi == 3){ rgb_map.red=p*255; rgb_map.green=q; rgb_map.blue=V;}
  214. else if (hi == 4){ rgb_map.red=t*255; rgb_map.green=p; rgb_map.blue=V;}
  215. else if (hi == 5){ rgb_map.red=V*255; rgb_map.green=p; rgb_map.blue=q;}
  216. else printf("ERRORE!");
  217.  
  218. rgb_int.red = (int)(rgb_map.red*255);
  219. rgb_int.green = (int)(rgb_map.green*255);
  220. rgb_int.blue = (int)(rgb_map.blue*255);
  221.  
  222. return rgb_int;
  223. }
  224.  
  225. struct hsv rgb_hsv(struct rgb rgb_map){
  226.  
  227. struct rgb_d rgb_d;
  228. struct hsv_d hsv_d;
  229. struct hsv hsv;
  230.  
  231. //(0,1)
  232. rgb_d.red= ((double)rgb_map.red)/255;
  233. rgb_d.green= ((double)rgb_map.green)/255;
  234. rgb_d.blue= ((double)rgb_map.blue)/255;
  235.  
  236. double gradi = 0;
  237. double MAX= 0, MIN=0, numeratore=0;
  238. double H = 0; //tonalitą
  239.  
  240. if( rgb_d.red == rgb_d.green && rgb_d.green == rgb_d.blue) { H=0; MAX = rgb_d.red; MIN = rgb_d.red; } //tonalitą trovata *********
  241. else{
  242. if( rgb_d.red >= rgb_d.green && rgb_d.red >= rgb_d.blue && rgb_d.green >= rgb_d.blue){ //rosso maggiore blu minore
  243. MAX = rgb_d.red;
  244. MIN = rgb_d.blue;
  245. gradi = 0;
  246. numeratore = rgb_d.green-rgb_d.blue;
  247. }
  248. else if( rgb_d.red >= rgb_d.green && rgb_d.red >= rgb_d.blue && rgb_d.green < rgb_d.blue){ //rosso maggiore verde minore
  249. MAX = rgb_d.red;
  250. MIN = rgb_d.green;
  251. gradi = 360;
  252. numeratore = rgb_d.green-rgb_d.blue;
  253. }
  254. else if( rgb_d.green >= rgb_d.red && rgb_d.green >= rgb_d.blue && rgb_d.red <= rgb_d.blue){ //verde maggiore, rosso minore
  255. MAX = rgb_d.green;
  256. MIN = rgb_d.red;
  257. gradi = 120;
  258. numeratore = rgb_d.blue-rgb_d.red;
  259. }
  260. else if( rgb_d.green >= rgb_d.red && rgb_d.green >= rgb_d.blue && rgb_d.blue < rgb_d.red){ //verde maggiore, blu minore
  261. MAX = rgb_d.green;
  262. MIN = rgb_d.blue;
  263. gradi = 120;
  264. numeratore = rgb_d.blue-rgb_d.red;
  265. }
  266. else if( rgb_d.blue >= rgb_d.red && rgb_d.blue >= rgb_d.green && rgb_d.red <= rgb_d.green){ //blu maggiore, rosso minore
  267. MAX = rgb_d.blue;
  268. MIN = rgb_d.red;
  269. gradi = 240;
  270. numeratore = rgb_d.red-rgb_d.green;
  271. }
  272. else if( rgb_d.blue >= rgb_d.red && rgb_d.blue >= rgb_d.green && rgb_d.green < rgb_d.red){ //blu maggiore, verde minore
  273. MAX = rgb_d.blue;
  274. MIN = rgb_d.green;
  275. gradi = 240;
  276. numeratore = rgb_d.red-rgb_d.green;
  277. }
  278. else printf("ERRORE!");
  279.  
  280. H = ( (int) (60*(numeratore/(MAX-MIN)) + gradi) ) % 360;//tonalitą trovata ***********
  281.  
  282. }
  283.  
  284. double S = 0;//saturazione
  285.  
  286. if(MAX == 0) S = 0; //saturazione trovata **********
  287. else S = (MAX - MIN) / MAX; //saturazione trovata **********
  288.  
  289. double V = MAX; //luminositą/valore trovata **********
  290.  
  291. hsv_d.hue=H;
  292. hsv_d.saturation=S;
  293. hsv_d.value=V;
  294.  
  295. hsv.hue= (int)(hsv_d.hue);
  296. hsv.saturation= (int)(hsv_d.saturation * 255);
  297. hsv.value= (int)(hsv_d.value * 255);
  298.  
  299.  
  300.  
  301.  
  302. return hsv;
  303. }
  304.  
Versione sito: 1.03.01 by mamo139. - Tutti i sorgenti presenti su questo sito sono rilasciati sotto licenza "GNU GPL version 3".