File: exp-gfx.c

package info (click to toggle)
alevt 1:1.6.2-4
  • links: PTS
  • area: main
  • in suites: squeeze
  • size: 956 kB
  • ctags: 662
  • sloc: ansic: 6,284; makefile: 119; perl: 104; sh: 15
file content (287 lines) | stat: -rw-r--r-- 6,297 bytes parent folder | download | duplicates (3)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
/* Copyright 1999 by Paul Ortyl <ortylp@from.pl> */

#include <stdio.h>
#include <stdlib.h>
#include <string.h>

#include "lang.h"
#include "export.h"

#include "font.h"
#define WW	(W*CW)			/* pixel width of window */
#define WH	(H*CH)			/* pixel hegiht of window */


///////////////////////////////////////////////////////
// COMMON ROUTINES FOR PPM AND PNG

static inline void
draw_char(unsigned char * colour_matrix, 
	  int fg, int bg, int c, int dbl, int _x, int _y, 
	  int sep)
{
  int x,y;
  unsigned char* src= (latin1 ? font1_bits : font2_bits);
  int dest_x=_x*CW;
  int dest_y=_y*CH;
      
  for(y=0;y<(CH<<dbl); y++)
    {
      for(x=0;x<CW; x++)
	{
	  int bitnr, bit, maskbitnr, maskbit;
	  bitnr=(c/32*CH + (y>>dbl))*CW*32+ c%32*CW +x;
	  bit=(*(src+bitnr/8))&(1<<bitnr%8);
	  if (sep)
	    {
	      maskbitnr=(0xa0/32*CH + (y>>dbl))*CW*32+ 0xa0%32*CW +x;
	      maskbit=(*(src+maskbitnr/8))&(1<<maskbitnr%8);
	      *(colour_matrix+WW*(dest_y+y)+dest_x+x)=
		(char)((bit && (!maskbit)) ? fg : bg);
	    }
	  else 
	    *(colour_matrix+WW*(dest_y+y)+dest_x+x)=
	      (char)(bit ? fg : bg);
	}
    }
  return;
}

static void
prepare_colour_matrix(/*struct export *e,*/
		      struct fmt_page *pg, 
		      unsigned char *colour_matrix)
{
   int x, y;
   //   bzero(colour_matrix, WH*WW);
   for (y = 0; y < H; ++y)
	{
	  for (x = 0; x < W; ++x)
	    { 
	      if (pg->dbl & (1<<(y-1)))
		{
		  if (pg->data[y-1][x].attr & EA_HDOUBLE)
		     draw_char(colour_matrix, pg->data[y][x].fg, 
			    pg->data[y][x].bg, pg->data[y][x].ch, 
			    (0), 
			    x, y, 
			    ((pg->data[y][x].attr & EA_SEPARATED) ? 1 : 0)
			    );
		}
	      else
		{
		  draw_char(colour_matrix, pg->data[y][x].fg, 
			    pg->data[y][x].bg, pg->data[y][x].ch, 
			    ((pg->data[y][x].attr & EA_DOUBLE) ? 1 : 0), 
			    x, y, 
			    ((pg->data[y][x].attr & EA_SEPARATED) ? 1 : 0)
			    );
		}
	    }
	}
    return;
}



///////////////////////////////////////////////////////
// STUFF FOR PPM OUTPUT

static int ppm_output(struct export *e, char *name, struct fmt_page *pg);

struct export_module export_ppm =	// exported module definition
{
    "ppm",			// id
    "ppm",			// extension
    0,				// options
    0,				// size
    0,				// open
    0,				// close
    0,				// option
    ppm_output			// output
};

static int
ppm_output(struct export *e, char *name, struct fmt_page *pg)
{
  FILE *fp;
  long n;
  static u8 rgb1[][3]={{0,0,0},
		      {1,0,0},
		      {0,1,0},
		      {1,1,0},
		      {0,0,1},
		      {1,0,1},
		      {0,1,1},
		      {1,1,1}};
  
  unsigned char *colour_matrix;

  if (!(colour_matrix=malloc(WH*WW))) 
    {
      export_error("cannot allocate memory");
      return 0;
    }

  prepare_colour_matrix(/*e,*/ pg, (unsigned char *)colour_matrix); 
  
  if (not(fp = fopen(name, "w")))
    {
      free(colour_matrix);
      export_error("cannot create file");
      return -1;
    }
  
  fprintf(fp,"P6 %d %d 1\n", WW, WH);

  for(n=0;n<WH*WW;n++)
    {
      if (!fwrite(rgb1[(int) *(colour_matrix+n)], 3, 1, fp))
	{
	  export_error("error while writting to file");
	  free(colour_matrix);
	  fclose(fp);
	  return -1;
	}
    }
  
  free(colour_matrix);
  fclose(fp);
  return 0;
}



///////////////////////////////////////////////////////
// STUFF FOR PNG OUTPUT

#ifdef WITH_PNG

#include <png.h>
static int png_open(struct export *e);
static int png_option(struct export *e, int opt, char *arg);
static int png_output(struct export *e, char *name, struct fmt_page *pg);

static char *png_opts[] =	// module options
{
    "compression=<0-9>",	// set compression level
    0
};

struct png_data			// private data in struct export
{
    int compression;
};

struct export_module export_png =	// exported module definition
{
    "png",			// id
    "png",			// extension
    png_opts,			// options
    sizeof(struct png_data),	// size
    png_open,			// open
    0,				// close
    png_option,			// option
    png_output			// output
};

#define D  ((struct png_data *)e->data)



static int
png_open(struct export *e)
{
    //e->reveal=1;	// the default should be the same in all formats
    D->compression = Z_DEFAULT_COMPRESSION;
    return 0;
}

static int
png_option(struct export *e, int opt, char *arg)
{
    switch (opt)
    {
	case 1: // compression=
	    if (*arg >= '0' && *arg <= '9')
		D->compression = *arg - '0';
	    break;
    }
    return 0;
}


static int
png_output(struct export *e, char *name, struct fmt_page *pg)
{
  FILE *fp;
  int x;
  png_structp png_ptr;
  png_infop info_ptr;
  png_byte *row_pointers[WH];
  static u8 rgb8[][3]={{  0,  0,  0},
		      {255,  0,  0},
		      {  0,255,  0},
		      {255,255,  0},
		      {  0,  0,255},
		      {255,  0,255},
		      {  0,255,255},
		      {255,255,255}};
  unsigned char *colour_matrix;

  if (!(colour_matrix=malloc(WH*WW))) 
    {
      export_error("cannot allocate memory");
      return -1;
    }
  
  prepare_colour_matrix(/*e,*/ pg, (unsigned char *)colour_matrix); 
  
  if (not(fp = fopen(name, "w")))
    {
      free(colour_matrix);
      export_error("cannot create file");
      return -1;
      } 
  
  png_ptr = png_create_write_struct(PNG_LIBPNG_VER_STRING, 
				    NULL, NULL, NULL);
  if (!png_ptr)
    {
      free(colour_matrix);
      fclose(fp);
      export_error("libpng init error");
      return -1;
    }
  info_ptr = png_create_info_struct(png_ptr);
  if (!info_ptr)
    {
      png_destroy_write_struct(&png_ptr,
			       (png_infopp)NULL);
      free(colour_matrix);
      fclose(fp);
      export_error("libpng init error");
      return -1;
    }
  png_init_io(png_ptr, fp);
  png_set_compression_level(png_ptr, D->compression);
  png_set_compression_mem_level(png_ptr, 9);
  png_set_compression_window_bits(png_ptr, 15);
  png_set_IHDR(png_ptr, info_ptr, WW, WH,
	       8, PNG_COLOR_TYPE_PALETTE , PNG_INTERLACE_NONE,
	       PNG_COMPRESSION_TYPE_DEFAULT, PNG_FILTER_TYPE_DEFAULT);
  png_set_PLTE(png_ptr, info_ptr,(png_color*) rgb8 , 8);
  png_write_info(png_ptr, info_ptr);
  
  for(x=0; x<WH; x++)
    { row_pointers[x]=colour_matrix+x*WW; }
  png_write_image(png_ptr, row_pointers);
  png_write_end(png_ptr, info_ptr);
  png_destroy_write_struct(&png_ptr, &info_ptr);
  
  free(colour_matrix);
  fclose(fp);
  return 0;
}

#endif /* WITH_PNG */