exp-gfx.c 5.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258
  1. /* Copyright 1999 by Paul Ortyl <ortylp@from.pl> */
  2. #include <stdio.h>
  3. #include <stdlib.h>
  4. #include <string.h>
  5. #include "lang.h"
  6. #include "export.h"
  7. #include "font.h"
  8. #define WW (W*CW) /* pixel width of window */
  9. #define WH (H*CH) /* pixel hegiht of window */
  10. static inline void draw_char(unsigned char * colour_matrix, int fg, int bg,
  11. int c, int dbl, int _x, int _y, int sep)
  12. {
  13. int x,y;
  14. unsigned char* src= (latin1==LATIN1 ? font1_bits : font2_bits);
  15. int dest_x=_x*CW;
  16. int dest_y=_y*CH;
  17. for(y=0;y<(CH<<dbl); y++)
  18. {
  19. for(x=0;x<CW; x++)
  20. {
  21. int bitnr, bit, maskbitnr, maskbit;
  22. bitnr=(c/32*CH + (y>>dbl))*CW*32+ c%32*CW +x;
  23. bit=(*(src+bitnr/8))&(1<<bitnr%8);
  24. if (sep)
  25. {
  26. maskbitnr=(0xa0/32*CH + (y>>dbl))*CW*32+ 0xa0%32*CW +x;
  27. maskbit=(*(src+maskbitnr/8))&(1<<maskbitnr%8);
  28. *(colour_matrix+WW*(dest_y+y)+dest_x+x)=
  29. (char)((bit && (!maskbit)) ? fg : bg);
  30. }
  31. else
  32. *(colour_matrix+WW*(dest_y+y)+dest_x+x)=
  33. (char)(bit ? fg : bg);
  34. }
  35. }
  36. return;
  37. }
  38. static void prepare_colour_matrix(/*struct export *e,*/
  39. struct fmt_page *pg,
  40. unsigned char *colour_matrix)
  41. {
  42. int x, y;
  43. for (y = 0; y < H; ++y)
  44. {
  45. for (x = 0; x < W; ++x)
  46. {
  47. if (pg->dbl & (1<<(y-1)))
  48. {
  49. if (pg->data[y-1][x].attr & EA_HDOUBLE)
  50. draw_char(colour_matrix, pg->data[y][x].fg,
  51. pg->data[y][x].bg, pg->data[y][x].ch,
  52. (0),
  53. x, y,
  54. ((pg->data[y][x].attr & EA_SEPARATED) ? 1 : 0)
  55. );
  56. }
  57. else
  58. {
  59. draw_char(colour_matrix, pg->data[y][x].fg,
  60. pg->data[y][x].bg, pg->data[y][x].ch,
  61. ((pg->data[y][x].attr & EA_DOUBLE) ? 1 : 0),
  62. x, y,
  63. ((pg->data[y][x].attr & EA_SEPARATED) ? 1 : 0)
  64. );
  65. }
  66. }
  67. }
  68. return;
  69. }
  70. static int ppm_output(struct export *e, char *name, struct fmt_page *pg);
  71. struct export_module export_ppm = // exported module definition
  72. {
  73. "ppm", // id
  74. "ppm", // extension
  75. 0, // options
  76. 0, // size
  77. 0, // open
  78. 0, // close
  79. 0, // option
  80. ppm_output // output
  81. };
  82. static int ppm_output(struct export *e, char *name, struct fmt_page *pg)
  83. {
  84. FILE *fp;
  85. long n;
  86. static u8 rgb1[][3]={{0,0,0},
  87. {1,0,0},
  88. {0,1,0},
  89. {1,1,0},
  90. {0,0,1},
  91. {1,0,1},
  92. {0,1,1},
  93. {1,1,1}};
  94. unsigned char *colour_matrix;
  95. if (!(colour_matrix=malloc(WH*WW)))
  96. {
  97. export_error("cannot allocate memory");
  98. return 0;
  99. }
  100. prepare_colour_matrix(/*e,*/ pg, (unsigned char *)colour_matrix);
  101. if (not(fp = fopen(name, "w")))
  102. {
  103. free(colour_matrix);
  104. export_error("cannot create file");
  105. return -1;
  106. }
  107. fprintf(fp,"P6 %d %d 1\n", WW, WH);
  108. for(n=0;n<WH*WW;n++)
  109. {
  110. if (!fwrite(rgb1[(int) *(colour_matrix+n)], 3, 1, fp))
  111. {
  112. export_error("error while writting to file");
  113. free(colour_matrix);
  114. fclose(fp);
  115. return -1;
  116. }
  117. }
  118. free(colour_matrix);
  119. fclose(fp);
  120. return 0;
  121. }
  122. #ifdef WITH_PNG
  123. #include <png.h>
  124. static int png_open(struct export *e);
  125. static int png_option(struct export *e, int opt, char *arg);
  126. static int png_output(struct export *e, char *name, struct fmt_page *pg);
  127. static char *png_opts[] = // module options
  128. {
  129. "compression=<0-9>", // set compression level
  130. 0
  131. };
  132. struct png_data // private data in struct export
  133. {
  134. int compression;
  135. };
  136. struct export_module export_png = // exported module definition
  137. {
  138. "png", // id
  139. "png", // extension
  140. png_opts, // options
  141. sizeof(struct png_data), // size
  142. png_open, // open
  143. 0, // close
  144. png_option, // option
  145. png_output // output
  146. };
  147. #define D ((struct png_data *)e->data)
  148. static int png_open(struct export *e)
  149. {
  150. D->compression = Z_DEFAULT_COMPRESSION;
  151. return 0;
  152. }
  153. static int png_option(struct export *e, int opt, char *arg)
  154. {
  155. switch (opt)
  156. {
  157. case 1: // compression=
  158. if (*arg >= '0' && *arg <= '9')
  159. D->compression = *arg - '0';
  160. break;
  161. }
  162. return 0;
  163. }
  164. static int png_output(struct export *e, char *name, struct fmt_page *pg)
  165. {
  166. FILE *fp;
  167. int x;
  168. png_structp png_ptr;
  169. png_infop info_ptr;
  170. png_byte *row_pointers[WH];
  171. static u8 rgb8[][3]={{ 0, 0, 0},
  172. {255, 0, 0},
  173. { 0,255, 0},
  174. {255,255, 0},
  175. { 0, 0,255},
  176. {255, 0,255},
  177. { 0,255,255},
  178. {255,255,255}};
  179. unsigned char *colour_matrix;
  180. if (!(colour_matrix=malloc(WH*WW)))
  181. {
  182. export_error("cannot allocate memory");
  183. return -1;
  184. }
  185. prepare_colour_matrix(/*e,*/ pg, (unsigned char *)colour_matrix);
  186. if (not(fp = fopen(name, "w")))
  187. {
  188. free(colour_matrix);
  189. export_error("cannot create file");
  190. return -1;
  191. }
  192. png_ptr = png_create_write_struct(PNG_LIBPNG_VER_STRING,
  193. NULL, NULL, NULL);
  194. if (!png_ptr)
  195. {
  196. free(colour_matrix);
  197. fclose(fp);
  198. export_error("libpng init error");
  199. return -1;
  200. }
  201. info_ptr = png_create_info_struct(png_ptr);
  202. if (!info_ptr)
  203. {
  204. png_destroy_write_struct(&png_ptr,
  205. (png_infopp)NULL);
  206. free(colour_matrix);
  207. fclose(fp);
  208. export_error("libpng init error");
  209. return -1;
  210. }
  211. png_init_io(png_ptr, fp);
  212. png_set_compression_level(png_ptr, D->compression);
  213. png_set_compression_mem_level(png_ptr, 9);
  214. png_set_compression_window_bits(png_ptr, 15);
  215. png_set_IHDR(png_ptr, info_ptr, WW, WH,
  216. 8, PNG_COLOR_TYPE_PALETTE , PNG_INTERLACE_NONE,
  217. PNG_COMPRESSION_TYPE_DEFAULT, PNG_FILTER_TYPE_DEFAULT);
  218. png_set_PLTE(png_ptr, info_ptr,(png_color*) rgb8 , 8);
  219. png_write_info(png_ptr, info_ptr);
  220. for(x=0; x<WH; x++)
  221. { row_pointers[x]=colour_matrix+x*WW; }
  222. png_write_image(png_ptr, row_pointers);
  223. png_write_end(png_ptr, info_ptr);
  224. png_destroy_write_struct(&png_ptr, &info_ptr);
  225. free(colour_matrix);
  226. fclose(fp);
  227. return 0;
  228. }
  229. #endif