Scarica il sorgente database/54-bmp_functions.cpp
  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. //*** strumenti gestione file bmp ***//
  13. struct bmpfile {
  14. long x;
  15. long y;
  16. char *** matrice;
  17. };
  18. char *** crea_bmp(char *nome, long x, long y);
  19. struct bmpfile carica_bmp(char *nome);
  20. void disegna_bmp(char *nome, char *** array, long x, long y);
  21.  
  22. //*** MAIN ***//
  23. int main (void){
  24.  
  25. struct bmpfile car = carica_bmp("a.bmp");
  26.  
  27. char *** mappanf = crea_bmp("out.bmp",car.x,car.y);
  28.  
  29. disegna_bmp("out.bmp",car.matrice,car.x,car.y);
  30.  
  31.  
  32. }
  33.  
  34. //****** FUNZIONI *******//
  35. char *** crea_bmp(char *nome, long x, long y){
  36.  
  37. char buffer [55] = "\x42\x4d\x36\x39\xb1\x00\x00\x00\x00\x00" \
  38. "\x36\x00\x00\x00\x28\x00\x00\x00\xe0\x08\x00\x00\xa8\x06" \
  39. "\x00\x00\x01\x00\x18\x00\x00\x00\x00\x00\x00\x00\x00\x00" \
  40. "\xc4\x0e\x00\x00\xc4\x0e\x00\x00\x00\x00\x00\x00\x00\x00" \
  41. "\x00\x00";
  42.  
  43. buffer[18] = x%256;//asse x
  44. buffer[19] = x/256;
  45. buffer[22] = y%256;//asse y
  46. buffer[23] = y/256;
  47.  
  48. FILE *stream;
  49. stream = fopen(nome,"wb");
  50. fwrite(buffer, 1, 54, stream);
  51. fclose(stream);
  52.  
  53. char *** array;
  54. int i, j, h;
  55. array = (char***)malloc(x * sizeof(char *));
  56. for(i=0; i<x; i++) {
  57. array[i] = (char**)malloc(y * sizeof(char *));
  58. for (j=0; j<y; j++)
  59. array[i][j] = (char*)malloc(3 * sizeof(char *));
  60. }
  61.  
  62. int i1,i2,i3;
  63. for(i1=0; i1<x; i1++)
  64. for (i2=0; i2<y; i2++)
  65. for (i3=0; i3<3; i3++)
  66. array[i1][i2][i3] = 0;
  67.  
  68. return array;
  69. }
  70.  
  71. struct bmpfile carica_bmp(char *nome){
  72. struct bmpfile dati;
  73. long filesize,b,i,j,z=0;
  74. char *** array;
  75. char * buffer;
  76. FILE * file;
  77.  
  78. file = fopen(nome,"rb");
  79. fseek (file , 0 , SEEK_END);
  80. filesize = ftell(file);
  81. rewind(file);
  82.  
  83. buffer = (char *) malloc(filesize * sizeof(char *));
  84.  
  85. b = fread( buffer, 1, 54, file);
  86.  
  87. dati.x = ((unsigned char)buffer[18])+((unsigned char)buffer[19])*256;
  88. dati.y = ((unsigned char)buffer[22])+((unsigned char)buffer[23])*256;
  89.  
  90.  
  91. b = fread( buffer, 1, filesize, file);
  92.  
  93.  
  94.  
  95. array = (char***)malloc(dati.x * sizeof(char *));
  96. for(i=0; i<dati.x; i++) {
  97. array[i] = (char**)malloc(dati.y * sizeof(char *));
  98. for (j=0; j<dati.y; j++)
  99. array[i][j] = (char*)malloc(3 * sizeof(char *));
  100. }
  101. for(i=0; i<dati.x; i++)
  102. for (j=0; j<dati.y; j++){
  103. array[i][j][0]=buffer[z];
  104. array[i][j][1]=buffer[z+1];
  105. array[i][j][2]=buffer[z+2];
  106. z=z+3;
  107. }
  108.  
  109.  
  110. dati.matrice = array;
  111.  
  112. return dati;
  113. }
  114.  
  115. void disegna_bmp(char *nome, char *** array, long x, long y){
  116.  
  117. FILE *stream;
  118. stream = fopen(nome,"ab");
  119.  
  120. int i1,i2;
  121. for(i1=0; i1<x; i1++)
  122. for (i2=0; i2<y; i2++)
  123. fwrite(array[i1][i2], 1, 3, stream);
  124.  
  125. fclose(stream);
  126. }
  127.