File: abr2gbr.c

package info (click to toggle)
abr2gbr 1%3A1.0.2-2.1
  • links: PTS
  • area: main
  • in suites: bullseye
  • size: 100 kB
  • sloc: ansic: 245; makefile: 154
file content (326 lines) | stat: -rw-r--r-- 7,944 bytes parent folder | download | duplicates (4)
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
/*
 * Converts PhotoShop .ABR and Paint Shop Pro .JBR brushes to GIMP .GBR
 * Copyright (C) 2001 Marco Lamberto <lm@sunnyspot.org>
 * Web page: http://the.sunnyspot.org/gimp/
 *
 * 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., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
 *
 * $Id:$
 */

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


#define GBRUSH_MAGIC    (('G' << 24) + ('I' << 16) + ('M' << 8) + ('P' << 0))


typedef struct _AbrHeader							AbrHeader;
typedef struct _AbrBrushHeader				AbrBrushHeader;
typedef struct _AbrSampledBrushHeader	AbrSampledBrushHeader;
typedef struct _GbrBrushHeader				GbrBrushHeader;
typedef struct _GbrBrush							GbrBrush;

struct _AbrHeader
{
	gshort version;
	gshort count;
};

struct _AbrBrushHeader
{
	gshort type;
	gint32 size;
};

struct _AbrSampledBrushHeader
{
	gint32 misc;
	gshort spacing;
	gchar antialiasing;
	gshort bounds[4];
	gint32 bounds_long[4];
	gshort depth;
	gboolean wide;
};

struct _GbrBrushHeader
{
	guint header_size;
	guint version;
	guint width;
	guint height;
	guint depth;
	guint magic_number;
	guint spacing;
};

struct _GbrBrush
{
	GbrBrushHeader header;
	gchar *name;
	gchar *description;
	gchar *data;
	gint size;
};


gchar				abr_read_char		(FILE *abr);
gshort			abr_read_short	(FILE *abr);
gint32 			abr_read_long		(FILE *abr);
void				abr_load				(const gchar *file);
GbrBrush *	abr_brush_load	(FILE *abr);
void				gbr_brush_save	(GbrBrush *brush, const gchar *file, const gint id);
void				gbr_brush_free	(GbrBrush *brush);
gint				main						(gint argc, gchar *argv[]);


gchar
abr_read_char(FILE *abr)
{
	g_return_val_if_fail ( abr != NULL, 0 );

	return fgetc(abr);
}

gshort
abr_read_short(FILE *abr)
{
	gshort val;

	g_return_val_if_fail ( abr != NULL, 0 );

	fread(&val, sizeof(val), 1, abr);
	
	return GUINT16_SWAP_LE_BE(val);
}

gint32
abr_read_long(FILE *abr)
{
	gint32 val;

	g_return_val_if_fail ( abr != NULL, 0 );
	
	fread(&val, sizeof(val), 1, abr);

	return GUINT32_SWAP_LE_BE(val);
}

GbrBrush *
abr_brush_load(FILE *abr)
{
	AbrBrushHeader abr_brush_hdr;
	GbrBrush *gbr = NULL;

	g_return_val_if_fail ( abr != NULL, NULL );

	abr_brush_hdr.type = abr_read_short(abr);
	abr_brush_hdr.size = abr_read_long(abr);
	g_print(" + BRUSH\n | << type: %d  block size: %d bytes\n",
		abr_brush_hdr.type, abr_brush_hdr.size);

	switch (abr_brush_hdr.type) {
	case 1: /* computed brush */
		/* FIXME: support it! */
		g_print("WARNING: computed brush unsupported, skipping.\n");
		fseek(abr, abr_brush_hdr.size, SEEK_CUR);
		break;
	case 2: /* sampled brush */
		{
			AbrSampledBrushHeader abr_sampled_brush_hdr;
			gint width, height;
			gint size;
			gint i = 0;

			abr_sampled_brush_hdr.misc					= abr_read_long(abr);
			abr_sampled_brush_hdr.spacing				= abr_read_short(abr);
			abr_sampled_brush_hdr.antialiasing	= abr_read_char(abr);

			for (i = 0; i < 4; i++)
				abr_sampled_brush_hdr.bounds[i] = abr_read_short(abr);
			for (i = 0; i < 4; i++) {
				abr_sampled_brush_hdr.bounds_long[i] = abr_read_long(abr);
			}
			abr_sampled_brush_hdr.depth					= abr_read_short(abr);

			height = abr_sampled_brush_hdr.bounds_long[2] - 
				abr_sampled_brush_hdr.bounds_long[0]; /* bottom - top */
			width = abr_sampled_brush_hdr.bounds_long[3] -
				abr_sampled_brush_hdr.bounds_long[1]; /* right - left */
			size = width * (abr_sampled_brush_hdr.depth >> 3) * height;

			/* FIXME: support wide brushes */
			abr_sampled_brush_hdr.wide = height > 16384;
			if (abr_sampled_brush_hdr.wide)
				g_print("WARING: wide brushes not supported\n");

			gbr = g_new0(GbrBrush, 1);
			gbr->header.version				= g_htonl(2);
			gbr->header.width					= g_htonl(width);
			gbr->header.height				= g_htonl(height);
			gbr->header.depth					= g_htonl(abr_sampled_brush_hdr.depth >> 3);
			gbr->header.magic_number	= g_htonl(GBRUSH_MAGIC);
			gbr->header.spacing				= g_htonl(abr_sampled_brush_hdr.spacing);
			gbr->data									= g_new0(gchar, size);
			gbr->size									= size;

			/* data decoding */
			{
				gshort comp;

				comp = abr_read_char(abr);
				g_print(" | << size: %dx%d %d bit (%d bytes) %s\n",
					width, height, abr_sampled_brush_hdr.depth, size,
					comp ? "compressed" : "raw");
				if (!comp) {
					fread(gbr->data, size, 1, abr);
				} else {
					gint32 n;
					gchar ch;
					gint i, j, c;
					gshort *cscanline_len;
					gchar *data = gbr->data;

					/* read compressed size foreach scanline */
					cscanline_len = g_new0(gshort, height);
					for (i = 0; i < height; i++)
						cscanline_len[i] = abr_read_short(abr);
						
					/* unpack each scanline data */
					for (i = 0; i < height; i++) {
						for (j = 0; j < cscanline_len[i];) {
							n = abr_read_char(abr);
							j++;
							if (n >= 128)			/* force sign */
								n -= 256;
							if (n < 0) {			/* copy the following char -n + 1 times */
								if (n == -128)	/* it's a nop */
									continue;
								n = -n + 1;
								ch = abr_read_char(abr);
								j++;
								for (c = 0; c < n; c++, data++)
									*data = ch;
							} else {					/* read the following n + 1 chars (no compr) */
								for (c = 0; c < n + 1; c++, j++, data++)
									*data = (guchar)abr_read_char(abr);
							}
						}
					}
					g_free(cscanline_len);
				}
			}
		}
		break;
	default:
		g_print("WARNING: unknown brush type, skipping.\n");
		fseek(abr, abr_brush_hdr.size, SEEK_CUR);
	}

	return gbr;
}

void
gbr_brush_free(GbrBrush *brush)
{
	g_free(brush->name);
	g_free(brush->description);
	g_free(brush->data);
	g_free(brush);
}

void
abr_load(const gchar *file)
{
	FILE *abr;
	AbrHeader abr_hdr;
	GbrBrush *brush;
	gint i;

	if (!(abr = fopen(file, "rb"))) {
		g_print("ERROR: can't open '%s'\n", file);
		return;
	}

	abr_hdr.version = abr_read_short(abr);
	abr_hdr.count = abr_read_short(abr);

	g_print("FILE %s\n + HEADER\n | << ver: %d  brushes: %d\n",
		file, abr_hdr.version, abr_hdr.count);

	if (abr_hdr.version > 1) {
		g_print("ERROR: unable to decode abr format version %d\n", abr_hdr.version);
	} else {
		for (i = 0; i < abr_hdr.count; i++) {
			brush = abr_brush_load(abr);
			if (!brush) {
				g_print("ERROR: load error (brush #%d)\n", i);
				break;
			}
			gbr_brush_save(brush, file, i);
			gbr_brush_free(brush);
		}
	}
		
	fclose(abr);
}

void
gbr_brush_save(GbrBrush *brush, const gchar *file, const gint id)
{
	FILE *gbr;
	gchar *p;

	p = strrchr(file, '.');
	brush->name = g_strndup(file, p - file);
	//g_print("%s\n", brush->name);
	p = brush->name;
	brush->name = g_strdup_printf("%s_%03d.gbr", p, id);
	brush->description = g_strdup_printf("%s %03d", p, id);
	g_free(p);
	//g_print("%s\n", brush->name);
	//g_print("%s\n", brush->description);

	brush->header.header_size = g_htonl(sizeof(GbrBrushHeader) +
		strlen(brush->description) + 1);
	gbr = fopen(brush->name, "wb");
	fwrite(&brush->header, sizeof(GbrBrushHeader), 1, gbr);
	fwrite(brush->description, strlen(brush->description) + 1, 1, gbr);
	fwrite(brush->data, brush->size, 1, gbr);
	g_print(" | >> %s (%d bytes)\n", brush->name,
		brush->size + sizeof(GbrBrushHeader) + strlen(brush->description) + 1);
	fclose(gbr);
}


gint
main(gint argc, gchar *argv[])
{
	gint i;
	
	if (argc < 2) {
		g_print("usage: %s {file1.abr} ...\n", argv[0]);
		return -1;
	}
	
	for (i = 1; i < argc; i++)
		abr_load(argv[i]);
	g_print("DONE\n");

	return 0;
}