File: plug-0.54.c

package info (click to toggle)
xpcd 2.04-3
  • links: PTS
  • area: main
  • in suites: slink
  • size: 624 kB
  • ctags: 556
  • sloc: ansic: 6,313; makefile: 217; sh: 200; perl: 12
file content (449 lines) | stat: -rw-r--r-- 11,781 bytes parent folder | download
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

/*
 * 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., 675 Mass Ave, Cambridge, MA 02139, USA.
 */

/*
 * PhotoCD loading file filter for the GIMP
 *   (c) 1996 Gerd Knorr <kraxel@cs.tu-berlin.de>
 *
 * Hint: If you use the high resolutions, you may want to have a look at
 * xpcd. xpcd can load small pieces from a photocd image, so you don't have
 * to load the whole image (and crop after) if you need a small piece only.
 * This reduces memory usage.
 * You can find it at sunsite (and mirrors):
 *   sunsite.unc.edu:/pub/Linux/X11/xapps/graphics/viewer/xpcd-$version.tar.gz
 *
 * Some of the code (main and some dialog stuff)  is just cut-and
 * pasted from the JPEG plug-in
 *    (by Peter Mattis)
 *
 */

/*
 * ChangeLog
 *
 * Version 2.0beta3:
 *
 *  - thumbnail tweaking (load speedup, layout)
 *
 *
 * Version 2.0beta2:
 *
 *  - more load options (orientation)
 *  - 4BASE and 16BASE resolutions appear only in the dialog box
 *    if they are present in the file
 *  - thumbnails work again, and they are numbered now
 *  - finetuning and bugfixes, both plug-in and libpcd
 *
 *
 * Version 2.0beta1:
 *
 *  - it is based on the libpcd now (and used as test application for it)
 *  - faster: Your CD-ROM is the speed limit now, as long as you don't try
 *    to load the 3072x2048 image with only 8 MB RAM.
 *  - 4BASE & 16BASE support added (OK, these are not that fast)
 *  - You can select between RGB and grayscaled image now
 *  - thumbnail loading temporarly removed
 *
 *
 * Version 1.1:
 *
 *  - added thumbnails support: you can load the "overview.pcd" file now.
 *    you get a huge image with them all :-)
 *
 *
 */

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

#include "gimp.h"
#include "pcd.h"

/*--------------------------------------------------------*/

static char    *prog_name;

/*--------------------------------------------------------*/

static char    *font[] =
{				/* stolen from X11's 5x7-font */

    " ##  ",
    "#  # ",
    "#  # ",
    "#  # ",
    "#  # ",
    " ##  ",
    "     ",

    "  #  ",
    " ##  ",
    "  #  ",
    "  #  ",
    "  #  ",
    " ### ",
    "     ",

    " ##  ",
    "#  # ",
    "   # ",
    "  #  ",
    " #   ",
    "#### ",
    "     ",

    "###  ",
    "   # ",
    " ##  ",
    "   # ",
    "#  # ",
    " ##  ",
    "     ",

    "  #  ",
    " ##  ",
    "# #  ",
    "#### ",
    "  #  ",
    "  #  ",
    "     ",

    "#### ",
    "#    ",
    "###  ",
    "   # ",
    "#  # ",
    " ##  ",
    "     ",

    " ##  ",
    "#    ",
    "###  ",
    "#  # ",
    "#  # ",
    " ##  ",
    "     ",

    "#### ",
    "   # ",
    "  #  ",
    "  #  ",
    " #   ",
    " #   ",
    "     ",

    " ##  ",
    "#  # ",
    " ##  ",
    "#  # ",
    "#  # ",
    " ##  ",
    "     ",

    " ##  ",
    "#  # ",
    "#  # ",
    " ### ",
    "   # ",
    " ##  ",
    "     "
};

/*--------------------------------------------------------*/

static int      dialog_ID;
static int      dialog_result = 3;
static int      color = 1;
static int      rot, maxres;

static void
item_callback(item_ID, client_data, call_data)
int             item_ID;
void           *client_data;
void           *call_data;
{
    if (*(int *) call_data == 1)
	dialog_result = (int) client_data;
}

static void
color_callback(item_ID, client_data, call_data)
int             item_ID;
void           *client_data;
void           *call_data;
{
    if (*(int *) call_data == 1)
	color = (int) client_data;
}

static void
rotate_callback(item_ID, client_data, call_data)
int             item_ID;
void           *client_data;
void           *call_data;
{
    if (*(int *) call_data == 1)
	rot = (int) client_data;
}

static void
ok_callback(item_ID, client_data, call_data)
int             item_ID;
void           *client_data;
void           *call_data;
{
    gimp_close_dialog(dialog_ID, 1);
}

static void
cancel_callback(item_ID, client_data, call_data)
int             item_ID;
void           *client_data;
void           *call_data;
{
    gimp_close_dialog(dialog_ID, 0);
}

int
size_dialog()
{
    static int      one = 1;
    int             frame, biggrp, group_ID, i[5];

    dialog_ID = gimp_new_dialog("PhotoCD Load Options");
    biggrp = gimp_new_row_group(dialog_ID, 0, DEFAULT, "fred");

    frame = gimp_new_frame(dialog_ID, biggrp, "channels");
    group_ID = gimp_new_column_group(dialog_ID, frame, RADIO, "bar");
    i[0] = gimp_new_radio_button(dialog_ID, group_ID, "gray");
    gimp_add_callback(dialog_ID, i[0], color_callback, (void *) 0);
    i[1] = gimp_new_radio_button(dialog_ID, group_ID, "color");
    gimp_add_callback(dialog_ID, i[1], color_callback, (void *) 1);
    gimp_change_item(dialog_ID, i[1], sizeof(int), &one);

    frame = gimp_new_frame(dialog_ID, biggrp, "rotate");
    group_ID = gimp_new_column_group(dialog_ID, frame, RADIO, "o");
    i[0] = gimp_new_radio_button(dialog_ID, group_ID, "0");
    gimp_add_callback(dialog_ID, i[0], rotate_callback, (void *) 0);
    i[1] = gimp_new_radio_button(dialog_ID, group_ID, "90 ccw");
    gimp_add_callback(dialog_ID, i[1], rotate_callback, (void *) 1);
    i[2] = gimp_new_radio_button(dialog_ID, group_ID, "180");
    gimp_add_callback(dialog_ID, i[2], rotate_callback, (void *) 2);
    i[3] = gimp_new_radio_button(dialog_ID, group_ID, "90 cw");
    gimp_add_callback(dialog_ID, i[3], rotate_callback, (void *) 3);
    gimp_change_item(dialog_ID, i[rot], sizeof(int), &one);

    frame = gimp_new_frame(dialog_ID, biggrp, "resolution");
    group_ID = gimp_new_row_group(dialog_ID, frame, RADIO, "foo");
    i[0] = gimp_new_radio_button(dialog_ID, group_ID, "BASE/16 (196x128)");
    gimp_add_callback(dialog_ID, i[0], item_callback, (void *) 1);
    i[1] = gimp_new_radio_button(dialog_ID, group_ID, "BASE/4 (384x256)");
    gimp_add_callback(dialog_ID, i[1], item_callback, (void *) 2);
    i[2] = gimp_new_radio_button(dialog_ID, group_ID, "BASE (768x512)");
    gimp_add_callback(dialog_ID, i[2], item_callback, (void *) 3);
    if (maxres > 3) {
	i[3] = gimp_new_radio_button(dialog_ID, group_ID,
				     "4BASE (1536x1024)");
	gimp_add_callback(dialog_ID, i[3], item_callback, (void *) 4);
	i[4] = gimp_new_radio_button(dialog_ID, group_ID,
				     "16BASE (3072x2048, RGB: 18 MB !)");
	gimp_add_callback(dialog_ID, i[4], item_callback, (void *) 5);
    }
    gimp_change_item(dialog_ID, i[2], sizeof(int), &one);

    gimp_add_callback(dialog_ID, gimp_ok_item_id(dialog_ID), ok_callback, 0);
    gimp_add_callback(dialog_ID, gimp_cancel_item_id(dialog_ID), cancel_callback, 0);

    if (gimp_show_dialog(dialog_ID))
	return dialog_result;
    else
	return -1;
}

/*--------------------------------------------------------*/

void
load_image(char *filename)
{
    int             left, top, width, height, size, rc;
    int             tx, ty, tcols, trows, x, y, i, c;
    Image           image;
    struct PCD_IMAGE img;
    unsigned char  *temp, nr[5];

    if (-1 == (rc = pcd_open(&img, filename))) {
	gimp_message(pcd_errmsg);
	gimp_quit();
    }
    if (rc) {
	/* load thumbnails */
	gimp_init_progress("Loading thumbnails");
	for (tcols = 1; tcols * tcols < rc; tcols++)
	    /* nothing */ ;
	tcols += tcols >> 2;
	if (tcols > 8)
	    tcols = 8;
	trows = (rc + tcols - 1) / tcols;

	image = gimp_new_image(filename, 100 * tcols, 100 * trows, RGB_IMAGE);
	temp = gimp_image_data(image);

	left = top = width = height = 0;
	for (ty = 0; ty < trows; ty++) {
	    for (tx = 0; tx < tcols && tx + ty * tcols < rc; tx++) {
		gimp_do_progress(tx + ty * tcols, rc);

		/* fill background with gray */
		for (y = 0; y < 99; y++) {
		    i = ((ty * 100 + y) * 100 * tcols + tx * 100) * 3;
		    if (y % 98)
			memset(temp + i, 192, 297);
		    else
			memset(temp + i + 3, 192, 297 - 6);
		}

		/* load image */
		rot = pcd_get_rot(&img, tx + ty * tcols);
		left = top = width = height = 0;
		if (-1 == pcd_select(&img, 1, tx + ty * tcols, 0, 0, rot,
				     &left, &top, &width, &height) ||
		    -1 == pcd_decode(&img)) {
		    gimp_message(pcd_errmsg);
		    gimp_quit();
		}
		if (rot & 1) {
		    for (y = 0; y < 96; y++) {
			i = ((ty * 100 + y + 2) * 100 * tcols + tx * 100 + 18) * 3;
			if (-1 == pcd_get_image_line(&img, y, temp + i,
						     PCD_TYPE_RGB, 1)) {
			    gimp_message(pcd_errmsg);
			    gimp_quit();
			}
		    }
		} else {
		    for (y = 0; y < 64; y++) {
			i = ((ty * 100 + y + 18) * 100 * tcols + tx * 100 + 2) * 3;
			if (-1 == pcd_get_image_line(&img, y, temp + i,
						     PCD_TYPE_RGB, 1)) {
			    gimp_message(pcd_errmsg);
			    gimp_quit();
			}
		    }
		}

		/* add the number to each of them:
		 * white, lower left corner */
		sprintf(nr, "%i", tx + ty * tcols + 1);
		for (c = 0; c < strlen(nr); c++)
		    for (y = 0; y < 7; y++)
			for (x = 0; x < 5; x++)
			    if (font[(nr[c] - '0') * 7 + y][x] == '#') {
				i = ((ty * 100 + y + 5) * 100 * tcols +
				     tx * 100 + c * 5 + x + 3) * 3;
				temp[i] = 0;
				temp[i + 1] = 0;
				temp[i + 2] = 0;
			    }
	    }
	}
	gimp_do_progress(1, 1);
	gimp_display_image(image);
	gimp_update_image(image);
	gimp_free_image(image);
	gimp_quit();
    }
    /* normal image */
    rot = pcd_get_rot(&img, 0);
    maxres = pcd_get_maxres(&img);
    size = size_dialog();
    if (-1 == size) {
	gimp_quit();
	return;			/* keep compiler happy */
    }
    if (size > 3) {
	/* the large resolutions require some time to decode... */
	temp = malloc(strlen(filename) + 11);
	if (!temp)
	    gimp_quit();

	sprintf(temp, "Loading %s:", filename);
	gimp_init_progress(temp);
	free(temp);
    }
    left = top = width = height = 0;
    if (-1 == pcd_select(&img, size, 0, !color, 0, rot, &left, &top, &width, &height) ||
	-1 == pcd_decode(&img)) {
	gimp_message(pcd_errmsg);
	gimp_quit();
    }
#if 0
    fprintf(stderr, "DEBUG: rot=%i, size=%i, geo=%ix%i+%i+%i\n",
	    rot, size, width, height, left, top);
#endif
    if (size > 3)
	gimp_do_progress(1, 2);
    image = gimp_new_image(filename, width, height,
			   color ? RGB_IMAGE : GRAY_IMAGE);
    if (-1 == pcd_get_image(&img, gimp_image_data(image),
			    color ? PCD_TYPE_RGB : PCD_TYPE_GRAY, 0)) {
	gimp_message(pcd_errmsg);
	gimp_quit();
    }
    pcd_close(&img);
    if (size > 3)
	gimp_do_progress(2, 2);
    gimp_display_image(image);
    gimp_update_image(image);
    gimp_free_image(image);
    gimp_quit();

    return;
}

/*--------------------------------------------------------*/

int
main(int argc, char *argv[])
{
    /*
     * Save the program name so we can use it later in reporting errors
     */
    prog_name = argv[0];

    /*
     * Call 'gimp_init' to initialize this filter.
     * 'gimp_init' makes sure that the filter was properly called and
     *  it opens pipes for reading and writing.
     */
    if (gimp_init(argc, argv)) {
	/*
	 * This is a file filter so all it needs to know about is loading
	 *  and saving images. So we'll install handlers for those two
	 *  messages.
	 */
	gimp_install_load_save_handlers(load_image, NULL);

	/*
	 * Run until something happens. That something could be getting
	 *  a 'QUIT' message or getting a load or save message.
	 */
	gimp_main_loop();
    }
    /* keep compiler happy */
    return 0;
}