Guest User

Untitled

a guest
Dec 11th, 2017
72
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 5.74 KB | None | 0 0
  1. #include <stdio.h>
  2. #include <string.h>
  3. #include <stdlib.h>
  4. #include <math.h>
  5. #include <process.h>
  6. #define VER 1
  7.  
  8. /*
  9. * Algoritmo de codificacao de imagens bitmap em arquivo de video formato tvf
  10. */
  11.  
  12. FILE *IN; //Arquivo de entrada
  13. FILE *OUT; //Arquivo de saida
  14.  
  15. int L; //4 bytes sendo lidos
  16. int X; //Auxiliar de L
  17. int Larg, Altu, LarN; //Largura e altura padrao
  18. //Matrizes: Matriz[Altura][Largura]
  19. unsigned char N[1080][5760]; //Matriz da imagem atual (sendo processada)
  20. unsigned char T[1080][1920]; //Matriz da imagem anterior (Ja processada)
  21. unsigned char P[255][3]; //Paleta de cores para acesso direto (sem calculos)
  22.  
  23. //Efetua quantizacao por Floyd–Steinberg (Paleta uniforme)
  24. void quantiza(){
  25. int i, j, h;
  26. unsigned char NR, NG, NB, QP; //Novos R, G e B, Posicao de quantizacao da paleta
  27. char ER, EG, EB; //Erros de quantizacao
  28. LarN = (Larg*3)-3;
  29. for (i = Altu-1; i > 0; i--) {
  30. //Primeira coluna
  31. QP = (round(N[i][0]/51.0f) * 42) + (round(N[i][1]/42.5f) * 6) + round(N[i][2]/51.0f);
  32. ER = N[i][2] - P[QP][0]; //Erro
  33. EG = N[i][1] - P[QP][1];
  34. EB = N[i][0] - P[QP][2];
  35. N[i ][3] += round(EB * 0.4375); //L
  36. N[i ][4] += round(EG * 0.4375);
  37. N[i ][5] += round(ER * 0.4375);
  38. N[i-1][0] += round(EB * 0.3125); //S
  39. N[i-1][1] += round(EG * 0.3125);
  40. N[i-1][2] += round(ER * 0.3125);
  41. N[i-1][3] += round(EB * 0.0625); //SE
  42. N[i-1][4] += round(EG * 0.0625);
  43. N[i-1][5] += round(ER * 0.0625);
  44. T[i][0] = QP;
  45. for (j = 3; j < LarN; j+=3) {
  46. //N[i][j] = B, N[i][j+1] = G, N[i][j+2] = R
  47. QP = (round(N[i][j]/51.0f) * 42) + (round(N[i][j+1]/42.5f) * 6) + round(N[i][j+2]/51.0f);
  48. ER = N[i][j+2] - P[QP][0]; //Erro
  49. EG = N[i][j+1] - P[QP][1];
  50. EB = N[i][j ] - P[QP][2];
  51. N[i ][j+3] += round(EB * 0.4375); //L
  52. N[i ][j+4] += round(EG * 0.4375);
  53. N[i ][j+5] += round(ER * 0.4375);
  54. N[i-1][j-3] += round(EB * 0.1875); //SO
  55. N[i-1][j-2] += round(EG * 0.1875);
  56. N[i-1][j-1] += round(ER * 0.1875);
  57. N[i-1][j ] += round(EB * 0.3125); //S
  58. N[i-1][j+1] += round(EG * 0.3125);
  59. N[i-1][j+2] += round(ER * 0.3125);
  60. N[i-1][j+3] += round(EB * 0.0625); //SE
  61. N[i-1][j+4] += round(EG * 0.0625);
  62. N[i-1][j+5] += round(ER * 0.0625);
  63. T[i][j/3] = QP;
  64. }
  65. //Ultima coluna
  66. QP = (round(N[i][j]/51.0f) * 42) + (round(N[i][j+1]/42.5f) * 6) + round(N[i][j+2]/51.0f);
  67. ER = N[i][LarN+2] - P[QP][0]; //Erro
  68. EG = N[i][LarN+1] - P[QP][1];
  69. EB = N[i][LarN ] - P[QP][2];
  70. N[i-1][LarN-3] += round(EB * 0.1875); //SO
  71. N[i-1][LarN-2] += round(EG * 0.1875);
  72. N[i-1][LarN-1] += round(ER * 0.1875);
  73. N[i-1][LarN ] += round(EB * 0.3125); //S
  74. N[i-1][LarN+1] += round(EG * 0.3125);
  75. N[i-1][LarN+2] += round(ER * 0.3125);
  76. T[i][Larg-1] = QP;
  77. }
  78. for (j = 0; j < LarN; j+=3) {
  79. QP = (round(N[0][j]/51.0f) * 42) + (round(N[0][j+1]/42.5f) * 6) + round(N[0][j+2]/51.0f);
  80. ER = N[0][j+2] - P[QP][0]; //Erro
  81. EG = N[0][j+1] - P[QP][1];
  82. EB = N[0][j ] - P[QP][2];
  83. N[0][j+3] += round(EB * 0.4375); //L
  84. N[0][j+4] += round(EG * 0.4375);
  85. N[0][j+5] += round(ER * 0.4375);
  86. T[0][j/3] = QP;
  87. }
  88. T[0][Larg-1] = (round(N[0][LarN]/51.0f) * 42) + (round(N[0][LarN+1]/42.5f) * 6) + round(N[0][LarN+2]/51.0f);
  89. }
  90.  
  91. //Gera a paleta uniforme (Acesso acelerado)
  92. void gerapaleta(){
  93. int i, k; //br
  94. double j; //g
  95. int h = 0; //contador
  96. for (i = 0; i < 256; i+=51) {
  97. for (j = 0.0; j < 256.0; j+=42.5) {
  98. for (k = 0; k < 256; k+=51) {
  99. P[h][0] = k;
  100. P[h][1] = round(j);
  101. P[h][2] = i;
  102. h++;
  103. }
  104. }
  105. }
  106. }
  107.  
  108. /////////////////////////////////////////////////////////// MAIN
  109.  
  110. int main(int argc, char *argv[]){
  111. int i, j, k, h;
  112. if (argc < 3){ //Esperado pelo o menos o programa, arquivo de saida e uma imagem
  113. return 1;
  114. }
  115. IN = fopen(argv[2],"rb"); //Leitura Binaria
  116. if (IN == NULL){
  117. return 2;
  118. }
  119. OUT = fopen(argv[1],"wb"); //Arquivo de saida
  120. if (OUT == NULL){
  121. return 3;
  122. }
  123.  
  124. //Leitura do cabecalho
  125. fread(&L, sizeof(L), 1, IN);
  126. if ((L & 65535) != 19778){ //BM nao encontrado
  127. return 4;
  128. }
  129. fseek(IN, 10, SEEK_SET);
  130. fread(&L, sizeof(L), 1, IN);
  131. if (L != 54){ //Offset bits tem que ser igual a para imagem true color (nao trabalhamos com paletas)
  132. return 5;
  133. }
  134.  
  135. //Tamanho (largura e altura) da imagem
  136. fseek(IN, 18, SEEK_SET);
  137. fread(&Larg, 4, 1, IN);
  138. fread(&Altu, 4, 1, IN);
  139. if ((Larg & 4095) != Larg || (Altu & 4095) != Altu){
  140. return 6;
  141. }
  142. L = (Larg << 20) + (Altu << 8) + (VER << 4); //Escrita do cabecalho do video
  143. fwrite(&L, sizeof(int), 1, OUT);
  144. gerapaleta();
  145.  
  146. //Leitura do resto do cabecalho nao importa
  147. fseek(IN, 54, SEEK_SET);
  148. h = 0;
  149. for (i = 0; i < Altu; i++){ //Leitura da primeira imagem
  150. fread(&N[i], 1, Larg * 3, IN);
  151. //fwrite(&N[i], 1, Larg * 3, OUT);
  152. }
  153. //fflush(OUT);
  154.  
  155. //Quantizacao da primeira imagem
  156. system("ECHO %TIME%");
  157. quantiza();
  158. system("ECHO %TIME%");
  159. for (i = 0; i < Altu; i++) {
  160. //T[i][j] = N[i][j];
  161. fwrite(&T[i], 1, Larg, OUT);
  162. }
  163. /*for (i = 0; i < 30; i=i+3) {
  164. printf("%hu\t%hu\t%hu\n", N[0][i], N[0][i+1], N[0][i+2]);
  165. //fwrite(&N[i], 1, Larg * 3, OUT);
  166. }*/
  167. fflush(OUT);
  168. fclose(IN);
  169.  
  170. for (k = 3; k < argc; k++){
  171. IN = fopen(argv[k],"rb"); //Leitura Binaria
  172. printf("Leitura de %s\n", argv[k]);
  173. if (IN == NULL){
  174. return 2;
  175. }
  176. fseek(IN, 10, SEEK_SET);
  177. fread(&L, sizeof(L), 1, IN);
  178. if (L != 54){ //Offset bits tem que ser igual a para imagem true color (nao trabalhamos com paletas)
  179. return 5;
  180. }
  181. fseek(IN, 54, SEEK_SET); //Pula Cabecalho
  182. h = 0;
  183. for (i = 0; i < Altu; i++){ //Leitura
  184. fread(&N[i], 1, Larg * 3, IN);
  185. //fwrite(&N[i], 1, Larg * 3, OUT);
  186. }
  187. //Quantizacao da imagem
  188. //quantiza();
  189. //escreveT();
  190. } //Fim para cada arquivo
  191. fclose(OUT);
  192. return 0;
  193. }
Add Comment
Please, Sign In to add comment