File: pilot-read-screenshot.c

package info (click to toggle)
pilot-link 0.12.5-dfsg-2
  • links: PTS
  • area: main
  • in suites: buster, stretch
  • size: 6,868 kB
  • ctags: 5,811
  • sloc: ansic: 53,153; sh: 10,459; java: 2,584; perl: 2,247; python: 1,044; makefile: 991; yacc: 662; cpp: 551; xml: 39
file content (478 lines) | stat: -rw-r--r-- 9,923 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
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
/*
 * $Id: pilot-read-screenshot.c,v 1.16 2006/11/02 14:54:31 desrod Exp $ 
 *
 * pilot-read-screenshot.c
 *
 * Copyright (c) 2003-2004, Angus Ainslie
 *
 * This program is free software; you can redistribute it and/or modify it
 * under the terms of the GNU General Public License as published by the
 * Free Software Foundation; either version 2 of the License, or (at your
 * option) any later version.
 *
 * This program is distributed in the hope that it will be useful, but
 * WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General
 * Public License for more details.
 *
 * You should have received a copy of the GNU General Public License along
 * with this program; if not, write to the Free Software Foundation, Inc.,
 * 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
 *
 */

#ifdef HAVE_CONFIG_H
# include <config.h>
#endif

#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <unistd.h>
#ifdef HAVE_STDINT_H
#include <stdint.h>
#endif

#include "pi-source.h"
#include "pi-file.h"
#include "pi-header.h"
#include "pi-userland.h"

#ifdef HAVE_PNG
# include "png.h"
#endif

#define pi_mktag(c1,c2,c3,c4) (((c1)<<24)|((c2)<<16)|((c3)<<8)|(c4))

#define OUT_PPM	 1
#define OUT_PNG	 2

struct ss_state {
	int w,
	  h,
	  depth;
	unsigned char *pix_map;
};



#define max(a,b) (( a > b ) ? a : b )
#define min(a,b) (( a < b ) ? a : b )

/***********************************************************************
 *
 * Function:	 write_png
 *
 * Summary:
 *
 * Parameters:  None
 *
 * Returns:	  Nothing
 *
 ***********************************************************************/
#ifdef HAVE_PNG
void write_png ( char *fname, struct ss_state *state )
{
	unsigned char *gray_buf;
	int i, j;
	png_structp png_ptr;
	png_infop info_ptr;
	FILE *f;

	if( state->depth < 8 )
		gray_buf = malloc( state->w );

	png_ptr = png_create_write_struct
		(PNG_LIBPNG_VER_STRING, NULL, NULL, NULL);

	if (!png_ptr)
		return;

	info_ptr = png_create_info_struct (png_ptr);
	if (!info_ptr)
	{
		png_destroy_write_struct (&png_ptr, (png_infopp) NULL);
		return;
	}

	if (setjmp (png_jmpbuf (png_ptr)))
	{
		png_destroy_write_struct (&png_ptr, &info_ptr);
		fclose (f);
		return;
	}

	f = fopen (fname, "wb");

	png_init_io (png_ptr, f);

	if( state->depth < 8 )
		png_set_IHDR (png_ptr, info_ptr, state->w, state->h,
		8, PNG_COLOR_TYPE_GRAY, PNG_INTERLACE_NONE,
		PNG_COMPRESSION_TYPE_DEFAULT, PNG_FILTER_TYPE_DEFAULT);
	else
	  	png_set_IHDR (png_ptr, info_ptr, state->w, state->h,
		8, PNG_COLOR_TYPE_RGB, PNG_INTERLACE_NONE,
		PNG_COMPRESSION_TYPE_DEFAULT, PNG_FILTER_TYPE_DEFAULT);


	png_write_info (png_ptr, info_ptr);

	if( state->depth < 8 )
	{
		for (i = 0; i < state->h; i++)
		{
			for( j=0; j<state->w; j++ )
				gray_buf[j] = state->pix_map[i*3*state->w+j*3];

			png_write_row (png_ptr, gray_buf );
			png_write_flush (png_ptr);
		}
	}
	else
	{
		for (i = 0; i < state->h; i++)
		{
			png_write_row (png_ptr, &state->pix_map[i*3*state->w] );
			png_write_flush (png_ptr);
		}
	}

	png_write_end (png_ptr, info_ptr);
	png_destroy_write_struct (&png_ptr, &info_ptr);

	fclose( f );

	if( state->depth < 8 )
		free( gray_buf );
}
#endif

void write_ppm ( char *fname, struct ss_state *state)
{
	int i;
	FILE *f;

	f = fopen (fname, "wb");

	fprintf (f, "P6\n# ");

	fprintf (f, "%s\n", fname );

	fprintf (f, "%d %d\n", state->w, state->h );

	fprintf (f, "255\n" );

	for( i = 0; i < 3*state->h*state->w; i += 3 )
		fwrite( &state->pix_map[i], 3, 1, f);

	fclose( f );
}


/***********************************************************************
 *
 * Function:	 WritePictures
 *
 * Summary:	FIXME
 *
 * Parameters:
 *
 * Returns:
 *
 ***********************************************************************/
void WritePictures (int sd, int db, int type )
{
	char fname[FILENAME_MAX];
	char extension[8];
	int i, len, idx = 0, recs, imgNum = 0;
//	unsigned char inBuf[61440], *pixelBuf;
	pi_buffer_t *inBuf, *pixelBuf;
	unsigned long clut[256], magic;
	int attr, category, val, mask, j, k;
	struct ss_state state;

	if( type == OUT_PPM )
		sprintf (extension, ".ppm");
	else if( type == OUT_PNG )
		sprintf (extension, ".png");
	else
		return;

	inBuf = pi_buffer_new (61440);

	if (sd)
		while( 1 )
		{
			len =
			 dlp_ReadRecordByIndex (sd, db, idx, inBuf, 0, &attr, &category);

			if( len <= 0 )
			{
				/* EOF */
				break;
			}

			idx++;
			state.w = ( inBuf->data[4] << 8 )+ inBuf->data[5];
			state.h = ( inBuf->data[6] << 8 ) + inBuf->data[7];
			recs = inBuf->data[9];
			state.depth = inBuf->data[8];
			magic = ((unsigned long *)inBuf->data)[0];

			if(  magic != 0xBECEDEFE && magic != 0xDEDEFEFE )
			{
				 /* no magic must version 1 db */
	//			 fprintf( stderr, "No Magic !\n" );

				 state.w = 160;
				 state.h = 160;
				 recs = 1;

				switch( len )
				{
				case 3200:
					state.depth = 1;
					mask = 1;
					break;

				case 6400:
					state.depth = 2;
					mask = 3;
					break;

				case 12800:
					state.depth = 4;
					mask = 0x0f;
					break;

				case 26624:
					state.depth = 8;
					break;

				case 51200:
					state.depth = 16;
					break;

				default:
					/* unknown record */
					/* get next */
					fprintf( stderr, "Unknown record" );
					continue;
				}
			}

			pixelBuf
			= pi_buffer_new (state.h * state.w * state.depth / 8 + 10 + 1024);

	//		pixelBuf = malloc( state.h * state.w * state.depth / 8 + 10 + 1024 );
			state.pix_map = malloc( state.h * state.w * 3 );

			if( !pixelBuf || !state.pix_map )
			{
				fprintf( stderr, "Memory Allocation failed\n" );
				return;
			}

			if( magic == 0xBECEDEFE || magic == 0xDEDEFEFE )
				memcpy( pixelBuf->data, &inBuf->data[10], len - 10 );
			else
				memcpy( pixelBuf->data, inBuf->data, len );

			for( i=1; i< recs; i++ )
			{
				len =
				dlp_ReadRecordByIndex (sd, db, idx, inBuf, 0, &attr, &category);
				memcpy( &pixelBuf->data[i*61440-10], inBuf->data, len );
				idx++;
			}

			sprintf (fname, "ScreenShot%d", ++imgNum );

			if (plu_protect_files (fname, extension, sizeof(fname)) < 1) {
				goto cleanup;
			}

			printf ("Generating %s...\n", fname);
			fprintf( stderr, "height: %d width: %d records: %d bit depth: %d\n"
				, state.h, state.w, recs, state.depth );

			if( state.depth == 8 )
			memcpy( clut, &inBuf->data[len-1024], 1024 );

			switch( state.depth )
			{
				case 1:
				case 2:
				case 4:
					for( i = 0; i < state.h*state.w/(8/state.depth); i++)
					{
						for( j=(8/state.depth-1), k=0; j >= 0; j--, k++ )
						{
							/* get right bits */
							val = ((pixelBuf->data[i] >> (j * state.depth)) & mask);
							/* invert */
							val = mask - val;
							/* stretch */
							val *= (255/mask);

							state.pix_map[3*(i*(8/state.depth)+k)] = val;
							state.pix_map[3*(i*(8/state.depth)+k)+1] = val;
							state.pix_map[3*(i*(8/state.depth)+k)+2] = val;
						}
		 			}
			 	break;

				case 8:
					for( i = 0; i < state.h*state.w; i++)
					{
						state.pix_map[3*i] =
						*(1 + (char *)&clut[pixelBuf->data[i]]);
						state.pix_map[3*i+1] =
						*(2 + (char *)&clut[pixelBuf->data[i]]);
						state.pix_map[3*i+2] =
						*(3 + (char *)&clut[pixelBuf->data[i]]);
					}
				break;

				case 16:
					for( i = 0; i < state.h*state.w; i++)
					{
						state.pix_map[i*3] = pixelBuf->data[i*2] & 0xF8;
						state.pix_map[i*3+1] = ((pixelBuf->data[i*2] & 0x07 ) << 5)
						+ (( pixelBuf->data[i*2+1] & 0xE0 ) >> 3 );
						state.pix_map[i*3+2] = ( pixelBuf->data[i*2+1] & 0x1F ) << 3;
					}
					break;

				default:
					fprintf( stderr, "I'm out of my depth :)\n" );
				break;
			}

			if( type == OUT_PPM )
				write_ppm( fname, &state );
			#ifdef HAVE_PNG
			else
				write_png( fname, &state );
			#endif

cleanup:
			pi_buffer_free (pixelBuf);
			//	  free( pixelBuf );
			free( state.pix_map );

			//	  fclose (f);
		}
	else
		return;

	pi_buffer_free (inBuf);
}

int main (int argc, const char *argv[])
{
	int c,			/* switch */
	 db,
	 sd = -1,
	 dbcount = 0,
	  type = OUT_PPM;

	const char
                *pformat;

	struct PilotUser User;

	poptContext po;

	struct poptOption options[] = {
		USERLAND_RESERVED_OPTIONS
		{"format", 	'f', POPT_ARG_STRING, &pformat, 0, "Specify picture output type (ppm or png)"},
		POPT_TABLEEND
	};

	po = poptGetContext("pilot-read-screenshot", argc, argv, options, 0);
	poptSetOtherOptionHelp(po,"\n\n");

	if (argc<2) {
		poptPrintUsage(po,stderr,0);
		return 1;
	}

	while ((c = poptGetNextOpt(po)) >= 0) {
		fprintf(stderr,"   ERROR: Unhandled option %d.\n",c);
		return 1;
	}
	if (c<-1) {
		plu_badoption(po,c);
	}

	if (!strncmp ("png", pformat, 3))
	{
#ifdef HAVE_PNG
		type = OUT_PNG;
#else
		fprintf (stderr, "   ERROR: pilot-read-screenshot was built without png support.\n");
#endif
	}
	else if (!strncmp ("ppm", pformat, 3))
	{
		type = OUT_PPM;
	}
	else
	{
		fprintf (stderr, "   ERROR: Unknown output type, defaulting to ppm\n");
		type = OUT_PPM;
	}

	sd = plu_connect ();

	if (sd < 0)
	 	goto error;

	if (dlp_ReadUserInfo (sd, &User) < 0)
	 	goto error_close;

	if (dlp_OpenDB (sd, 0, dlpOpenRead, "ScreenShotDB", &db) < 0)
	{
		fprintf (stderr,"   ERROR: Unable to open Screen Shot database on Palm.\n");
		dlp_AddSyncLogEntry (sd, "Unable to open Screen Shot database.\n");
		goto error_close;
	}

	WritePictures (sd, db, type );

	if (sd)
	{
		/* Close the database */
		dlp_CloseDB (sd, db);
	 }

	if (sd)
	{
		dlp_AddSyncLogEntry (sd,
							 "Successfully read screenshots from Palm.\n"
							 "Thank you for using pilot-link.");
		dlp_EndOfSync (sd, 0);
		pi_close (sd);
	}

	if (!plu_quiet) {
		printf ("\nList complete. %d files found.\n", dbcount);
	}

	return 0;

error_close:
	pi_close (sd);

error:
	return -1;
}

/* vi: set ts=8 sw=4 sts=4 noexpandtab: cin */
/* ex: set tabstop=4 expandtab: */
/* Local Variables: */
/* indent-tabs-mode: t */
/* c-basic-offset: 8 */
/* End: */