Guest User

Untitled

a guest
Oct 23rd, 2017
68
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 7.72 KB | None | 0 0
  1. /* AUTOR: ALEXIS LEUNG CHEUNG */
  2. /* FECHA: 14 - 04 - 2002 */
  3. /* TITULO: NINGUNO */
  4. /* DESCRIPCION: VISOR DE IMAGENES BMP DE 256 COLORES */
  5. /* NOTAS: ESTA LIMITADO A 256 COLORES Y UN AREA MAXIMA DE 320 x 200 PIXELES */
  6.  
  7. #include <stdio.h>
  8. #include <stdlib.h>
  9. #include <conio.h>
  10.  
  11. #define VAR 0
  12. #define INVALIDO -1
  13. #define FALLA 0
  14. #define EXITO 1
  15. #define KB_ESC 27
  16. /* MACROS PARA LAS FUNCIONES GRAFICAS */
  17. #define SEG_VIDEO 0xA000
  18. #define M320x200 0x13
  19. #define MTexto 0x3
  20.  
  21. struct cabecera {
  22. char ident[2];
  23. unsigned long int tam;
  24. char reservado[4];
  25. unsigned long int offset;
  26. };
  27.  
  28. struct info_general {
  29. unsigned long int tam_cabecera;
  30. unsigned long int anchura; /* en pixels */
  31. unsigned long int altura; /* en pixels */
  32. int planos;
  33. int tam_pixel; /* tama¤o en bits de cada pixel */
  34. unsigned long int compresion;
  35. unsigned long int tam_imagen; /* en bytes */
  36. unsigned long int h_resolution;
  37. unsigned long int v_resolution;
  38. unsigned long int num_color; /* numero de colores utilizados */
  39. unsigned long int color_imp; /* numeros de colores importantes */
  40. };
  41.  
  42. struct valor_rgb {
  43. unsigned char azul;
  44. unsigned char verde;
  45. unsigned char rojo;
  46. unsigned char reservado;
  47. };
  48.  
  49.  
  50. void modo_video(unsigned char modo);
  51. void setpal(char col,char r,char g,char b);
  52. void putpixel(unsigned int x, unsigned int y, unsigned char color);
  53. /*-------------------------------------------------------------------------*/
  54. char verifica(char *fname);
  55. void mostrar_pal(void);
  56. void carga(void);
  57. void menu(void);
  58. void presentacion(void);
  59. void info_general(void);
  60. void paleta(void);
  61. void mostrar_imagen(void);
  62. void cambiar_pal(struct valor_rgb *pal);
  63. void despedida(void);
  64.  
  65. struct cabecera bmp_cab;
  66. struct info_general bmp_info;
  67. struct valor_rgb *rgb;
  68. FILE *f;
  69. char fname[50];
  70.  
  71. int main()
  72. {
  73. presentacion();
  74. fclose(f);
  75. free(rgb); /* liberamos la memoria utilizada */
  76.  
  77. return 0;
  78. }
  79.  
  80. void carga(void)
  81. {
  82. register int i;
  83.  
  84. /* leemos la cabecera del archivo */
  85. fread( &bmp_cab, sizeof(struct cabecera), 1, f);
  86. /* leemos la informacion general del archivo */
  87. fread( &bmp_info, sizeof(struct info_general), 1, f);
  88.  
  89. /*leemos todos los colores que existen en la imagen */
  90. if (!bmp_info.num_color) /* si se usan todos los colores */
  91. bmp_info.num_color = 1 << bmp_info.tam_pixel; /* calculamos dependiendo del numero de bits que ocupa cada pixel */
  92.  
  93. /* reservamos memoria dinamica para la tabla de colores RGB */
  94. rgb = (struct valor_rgb *) malloc (bmp_info.num_color * sizeof(struct valor_rgb));
  95.  
  96. fread(rgb, bmp_info.num_color * sizeof(struct valor_rgb), 1, f);
  97. }
  98.  
  99. void presentacion(void)
  100. {
  101. char control;
  102.  
  103. clrscr();
  104.  
  105. printf("%55s","---VISUALIZADOR DE IMAGENES BMP---");
  106. printf("\n\n\n Introduzca el nombre y ruta de ser necesario");
  107. printf("\n ARCHIVO: ");
  108. gets(fname);
  109. if (!fname[0])
  110. exit(0);
  111.  
  112. control = verifica(fname);
  113.  
  114. if (control == FALLA)
  115. {
  116. printf("\n ERROR FATAL: el archivo no existe o esta protegido...");
  117. exit(1);
  118. }
  119. if (control == INVALIDO)
  120. {
  121. printf("\n ERROR FATAL: el archivo no es un BMP valido... ");
  122. exit(1);
  123. }
  124.  
  125. /* llamamos al menu principal */
  126. menu();
  127. }
  128.  
  129. char verifica(char *fname)
  130. {
  131. f = fopen(fname,"rb");
  132.  
  133. if (!f)
  134. return (FALLA);
  135.  
  136. carga(); /* leemos y almacenamos del archivo */
  137.  
  138. /* comprobamos que sea un archivo valido BMP */
  139. if (bmp_cab.ident[0] == 'B' && bmp_cab.ident[1] == 'M')
  140. return (EXITO);
  141. else
  142. return (INVALIDO);
  143.  
  144. }
  145.  
  146. void menu(void)
  147. {
  148. char opcion;
  149.  
  150. for (;;)
  151. {
  152. clrscr();
  153. printf("\n\n (1) INFORMACION GENERAL DEL ARCHIVO");
  154. printf("\n\n (2) PALETA DE COLORES USADAS");
  155. printf("\n\n (3) MOSTRAR PALETA");
  156. printf("\n\n (4) VISUALIZAR IMAGEN");
  157. printf("\n\n (5) SALIR");
  158. printf("\n\n OPCION: ");
  159. opcion = getche();
  160.  
  161. switch (opcion)
  162. {
  163. case '1': info_general(); break;
  164. case '2': paleta(); break;
  165. case '3': mostrar_pal(); break;
  166. case '4': mostrar_imagen(); break;
  167. case '5': despedida();
  168. }
  169. } /* termina bucle for(;;) */
  170. }
  171.  
  172. void info_general(void)
  173. {
  174.  
  175. clrscr();
  176. printf("%35s%s>\n","<",fname);
  177. printf("\n Tama¤o del archivo: %ld bytes",bmp_cab.tam);
  178. printf("\n Offset del archivo: %ld",bmp_cab.offset);
  179. printf("\n Tama¤o de la cabecera: %d bytes",bmp_info.tam_cabecera);
  180. printf("\n Anchura: %d pixels",bmp_info.anchura);
  181. printf("\n Altura: %d pixels",bmp_info.altura);
  182. printf("\n Numero de planos: %d",bmp_info.planos);
  183. printf("\n Numero de bits por pixel: %d bits",bmp_info.tam_pixel);
  184. printf("\n Compresion: %d ",bmp_info.compresion);
  185. printf("\n Tama¤o de la imagen: %ld bytes",bmp_info.tam_imagen);
  186. printf("\n Resolucion horizontal: %d pixels/metros",bmp_info.h_resolution);
  187. printf("\n Resolucion vertical: %d pixels/metros",bmp_info.v_resolution);
  188. printf("\n Numero de colores utilizados: %d colores",bmp_info.num_color);
  189. printf("\n Numero de colores importantes: %d colores",bmp_info.color_imp);
  190.  
  191. printf("\n\n\n PRESIONE CUALQUIER TECLA PARA CONTINUAR...");
  192.  
  193. getch();
  194. }
  195.  
  196. void cambiar_pal(struct valor_rgb *pal)
  197. {
  198. register int i;
  199.  
  200. for (i = 0; i < 256; i++)
  201. setpal( i, pal[i-VAR].rojo / 4, pal[i-VAR].verde / 4, pal[i-VAR].azul / 4);
  202. }
  203.  
  204. void paleta(void)
  205. {
  206. register int i,j;
  207. char opcion;
  208.  
  209. clrscr();
  210.  
  211. printf("\n %10s %10s %10s %10s\n\n","Color","Rojo","Verde","Azul");
  212. for (i = 0, j = 1; i <= (bmp_info.num_color); i++, j++)
  213. {
  214. if (j == 20 || i == (bmp_info.num_color) )
  215. {
  216. j = 0;
  217. i--;
  218. gotoxy(1,25);
  219. printf(" Presione [ESC] para salir o cualquier tecla para continuar....");
  220. opcion = getch();
  221.  
  222. if (opcion == KB_ESC)
  223. break;
  224. if (i+1 == (bmp_info.num_color))
  225. break;
  226.  
  227. clrscr();
  228. printf("\n %10s %10s %10s %10s\n\n","Color","Rojo","Verde","Azul");
  229. continue;
  230. }
  231. printf("\n %10d %10d %10d %10d", i, rgb[i].rojo, rgb[i].verde, rgb[i].azul);
  232. }
  233. }
  234.  
  235. void mostrar_imagen(void)
  236. {
  237. register int x,y;
  238. char *linea;
  239. int resto;
  240. unsigned long int posicion;
  241.  
  242. posicion = ftell(f); /* tomamos la posicion del puntero del archivo */
  243.  
  244. modo_video(M320x200);
  245.  
  246. cambiar_pal(rgb);
  247.  
  248. /* reservamos memoria suficiente para albergar una linea de pixeles */
  249. linea = (char *) malloc (bmp_info.anchura);
  250.  
  251. /* calculamos la cantidad de bytes necesario que sumado con la anchura
  252. sea un multiplo de cuatro */
  253. resto = (4 * ((bmp_info.anchura + 3) >> 2)) - bmp_info.anchura;
  254.  
  255. for (y = bmp_info.altura - 1; y >= 0; y--)
  256. {
  257. fread(linea, bmp_info.anchura, 1, f);
  258. for (x = 0; x < bmp_info.anchura; x++)
  259. putpixel(x,y,linea[x]);
  260. fseek(f, resto, SEEK_CUR);
  261. }
  262. getch();
  263.  
  264. fseek(f, posicion, SEEK_SET); /* restauramos la posicion inicial del puntero */
  265.  
  266. modo_video(MTexto);
  267. }
  268.  
  269. void mostrar_pal(void)
  270. {
  271. register int i,j;
  272.  
  273. modo_video(M320x200);
  274.  
  275. cambiar_pal(rgb);
  276.  
  277. for (i = 0; i < 256; i++)
  278. for (j = 0; j <= 50; j++)
  279. putpixel(i,j,i);
  280. getch();
  281.  
  282. modo_video(MTexto);
  283. }
  284.  
  285. void despedida(void)
  286. {
  287. clrscr();
  288. gotoxy(20,13);
  289. printf("ESPERO QUE HAYA DISFRUTADO DEL PROGRAMA...");
  290. getch();
  291. exit(0);
  292. }
  293.  
  294. /* ----FUNCIONES GRAFICAS ESCRITAS EN ENSAMBLADOR PARA SU MAYOR RAPIDEZ---- */
  295.  
  296.  
  297. void modo_video(unsigned char modo)
  298. {
  299. asm {
  300. push ax
  301. xor ax,ax
  302. mov al,modo
  303. int 10h
  304. pop ax
  305. }
  306. }
  307.  
  308. void putpixel(unsigned int x, unsigned int y, unsigned char color)
  309. {
  310. asm {
  311. push ax
  312. push bx
  313. push di
  314. push es
  315. mov ax,SEG_VIDEO
  316. mov es,ax
  317. mov bx,[y]
  318. mov di,bx
  319. shl bx,8
  320. shl di,6
  321. add di,bx
  322. add di,[x]
  323. mov bl,[color]
  324. mov es:[di],bl
  325. pop es
  326. pop di
  327. pop bx
  328. pop ax
  329. }
  330. }
  331.  
  332. void setpal(char col,char r,char g,char b)
  333. {
  334. asm {
  335. mov dx,03c8h;
  336. mov al,col;
  337. out dx,al;
  338. mov dx,03c9h;
  339. mov al,r;
  340. out dx,al;
  341. mov al,g;
  342. out dx,al;
  343. mov al,b;
  344. out dx,al;
  345. }
  346. }
Add Comment
Please, Sign In to add comment