]>
iEval git - xfishtank.git/blob - ppmtofish/ppmtofish.c
4 #define COLS_PER_LINE 7
5 #define NUMS_PER_LINE 16
7 extern unsigned char *ReadGifBitmap();
11 unsigned int width
, height
;
15 unsigned int red
, green
, blue
;
22 * dscan is a quick replacement for:
24 * fscanf (fp, "%d", &ip);
41 fprintf(stderr
, "dscanf: EOF");
44 else if (nerrors
++ > 4)
60 v
= i
- '0' + (v
* 10);
73 fprintf(stderr
, "dscanf: non numeric character (0x%x) '%c'\n", i
, i
);
86 ReadEbmBitmap(fp
, w
, h
, colrs
)
89 struct rgb_color
*colrs
;
92 unsigned int bmap_size
;
96 unsigned char *bit_data
;
103 if ((fscanf(fp
, "%12s %d %d %d\n", line
, w
, h
, &scale
) == 4) &&
104 (strcmp(line
, "xstitch_data") == 0)) {
105 for (i
= 0; i
< 3; i
++) {
106 fscanf(fp
, "%s", line
);
107 if (strcmp(line
, "red") == 0) {
109 } else if (strcmp(line
, "green") == 0) {
111 } else if (strcmp(line
, "blue") == 0) {
114 fprintf(stderr
, "File has improper format\n");
115 return ((unsigned char *) NULL
);
118 fscanf(fp
, "[^\n]\n");
119 for (i
= 0; i
< scale
; i
++) {
121 if (fscanf(fp
, "%d %d %d%[^\n]\n", ptr
[0], ptr
[1], ptr
[2], line
) < 3) {
122 fprintf(stderr
, "Bad color data #%d\n", i
);
125 colrs
[i
].green
= green
;
126 colrs
[i
].blue
= blue
;
129 bmap_size
= (*w
) * (*h
);
130 bit_data
= (unsigned char *) malloc(bmap_size
);
131 if (bit_data
== NULL
) {
132 fprintf(stderr
, "Cannot allocate space for bitmap\n");
135 for (i
= 0; i
< bmap_size
; i
++) {
136 if (dscan(fp
, &temp
) == -1)
138 if (temp
> (scale
- 1))
140 *bitp
= (unsigned char) temp
;
144 return ((unsigned char *) NULL
);
158 out
= out
| ((in
>> 24) & 0xff);
159 out
= out
| ((in
>> 8) & 0xff00);
160 out
= out
| ((in
<< 8) & 0xff0000);
161 out
= out
| ((in
<< 24) & 0xff000000);
168 ReadCompactEbmBitmap(fp
, w
, h
, colrs
)
171 struct rgb_color
*colrs
;
174 unsigned int bmap_size
;
176 unsigned char *bit_data
;
179 struct rgb_color color_data
;
187 fread(&Hdr
, sizeof(struct ebmhdr
), 1, fp
);
188 if (strcmp(Hdr
.magic
, "xstitch") == 0) {
190 * a horrible hack to deal with different byte ordering on
191 * different machines. Is based on assumption of only 2
192 * possible byte orderings, and never more than 256 colors.
194 if (Hdr
.num_colors
> 256) {
196 Hdr
.width
= swapint(Hdr
.width
);
197 Hdr
.height
= swapint(Hdr
.height
);
198 Hdr
.num_colors
= swapint(Hdr
.num_colors
);
202 scale
= Hdr
.num_colors
;
203 for (i
= 0; i
< Hdr
.num_colors
; i
++) {
204 fread(&colrs
[i
], sizeof(struct rgb_color
), 1, fp
);
206 colrs
[i
].red
= swapint(colrs
[i
].red
);
207 colrs
[i
].green
= swapint(colrs
[i
].green
);
208 colrs
[i
].blue
= swapint(colrs
[i
].blue
);
209 colrs
[i
].pix_len
= swapint(colrs
[i
].pix_len
);
211 if (colrs
[i
].pix_len
!= 0) {
212 if (colrs
[i
].pix_len
> 1023) {
213 fprintf(stderr
, "bad pattern\n");
214 return ((unsigned char *) NULL
);
216 fread(pix_file
, sizeof(char), colrs
[i
].pix_len
, fp
);
220 bmap_size
= (*w
) * (*h
);
221 bit_data
= (unsigned char *) malloc(bmap_size
);
222 if (bit_data
== NULL
) {
223 fprintf(stderr
, "Cannot allocate space for bitmap\n");
225 fread(bit_data
, sizeof(unsigned char), ((*w
) * (*h
)), fp
);
228 for (i
= 0; i
< bmap_size
; i
++) {
229 if ((int) *bitp
> (scale
- 1))
230 *bitp
= (unsigned char) 0;
234 return ((unsigned char *) NULL
);
242 MapColors(data1
, colrs1
, data2
, colrs2
, width
, height
, Cnt
)
243 unsigned char *data1
;
244 struct rgb_color
*colrs1
;
245 unsigned char *data2
;
246 struct rgb_color
*colrs2
;
252 int Mapping
[MAXCOLORS
];
254 struct rgb_color
*fcolrs
;
256 fcolrs
= (struct rgb_color
*) malloc((MAXCOLORS
+ 1) * sizeof(struct rgb_color
));
260 for (i
= 0; i
< (width
* height
); i
++) {
262 for (j
= 0; j
< fcnt
; j
++) {
263 if ((fcolrs
[j
].red
== colrs1
[indx
].red
) &&
264 (fcolrs
[j
].green
== colrs1
[indx
].green
) &&
265 (fcolrs
[j
].blue
== colrs1
[indx
].blue
)) {
270 fcolrs
[j
].red
= colrs1
[indx
].red
;
271 fcolrs
[j
].green
= colrs1
[indx
].green
;
272 fcolrs
[j
].blue
= colrs1
[indx
].blue
;
275 if (fcnt
> MAXCOLORS
) {
276 fprintf(stderr
, "Error: cannot use more than %d colors in your fish\n",
284 for (i
= 0; i
< (width
* height
); i
++) {
286 *ptr
= (unsigned char) (Mapping
[indx
]);
291 for (i
= 0; i
< (width
* height
); i
++) {
293 for (j
= 0; j
< fcnt
; j
++) {
294 if ((fcolrs
[j
].red
== colrs2
[indx
].red
) &&
295 (fcolrs
[j
].green
== colrs2
[indx
].green
) &&
296 (fcolrs
[j
].blue
== colrs2
[indx
].blue
)) {
301 fcolrs
[j
].red
= colrs2
[indx
].red
;
302 fcolrs
[j
].green
= colrs2
[indx
].green
;
303 fcolrs
[j
].blue
= colrs2
[indx
].blue
;
306 if (fcnt
> MAXCOLORS
) {
307 fprintf(stderr
, "Error: cannot use more than %d colors in your fish\n",
315 for (i
= 0; i
< (width
* height
); i
++) {
317 *ptr
= (unsigned char) (Mapping
[indx
]);
321 for (i
= 0; i
< fcnt
; i
++) {
322 fcolrs
[i
].red
= fcolrs
[i
].red
* 256;
323 fcolrs
[i
].green
= fcolrs
[i
].green
* 256;
324 fcolrs
[i
].blue
= fcolrs
[i
].blue
* 256;
326 for (i
= fcnt
; i
< 16; i
++) {
348 struct rgb_color colrs1
[MAXCOLORS
];
349 struct rgb_color colrs2
[MAXCOLORS
];
350 struct rgb_color
*fcolrs
;
351 unsigned char *data1
, *data2
;
352 int width1
, height1
, width2
, height2
;
358 fprintf(stderr
, "Usage: %s <fish1> <fish2> [<outprefix>]\n", argv
[0]);
362 ifd
= fopen(argv
[1], "r");
364 fprintf(stderr
, "Error: cannot open (%s) for read\n", argv
[1]);
368 data1
= ReadCompactEbmBitmap(ifd
, &width1
, &height1
, colrs1
);
371 data1
= ReadEbmBitmap(ifd
, &width1
, &height1
, colrs1
);
375 data1
= ReadGifBitmap(ifd
, &width1
, &height1
, colrs1
);
380 fprintf(stderr
, "Error: (%s) has unknown format\n", argv
[1]);
384 ifd
= fopen(argv
[2], "r");
386 fprintf(stderr
, "Error: cannot open (%s) for read\n", argv
[2]);
390 data2
= ReadCompactEbmBitmap(ifd
, &width2
, &height2
, colrs2
);
393 data2
= ReadEbmBitmap(ifd
, &width2
, &height2
, colrs2
);
397 data2
= ReadGifBitmap(ifd
, &width2
, &height2
, colrs2
);
402 fprintf(stderr
, "Error: (%s) has unknown format\n", argv
[2]);
407 if ((width1
!= width2
) || (height1
!= height2
)) {
408 fprintf(stderr
, "Error: fish must have same dimensions\n");
412 fcolrs
= MapColors(data1
, colrs1
, data2
, colrs2
, width1
, height1
, &fcnt
);
414 if (data1
[0] != data2
[0]) {
415 fprintf(stderr
, "Error: fish must have same backgrounds\n");
423 while ((argv
[1][cnt
] == argv
[2][cnt
]) && (argv
[1][cnt
] != '.') && (argv
[2][cnt
] != '.')) {
427 fprintf(stderr
, "Error: files need common prefix\n");
430 outname
= (char *) malloc(cnt
+ 1);
431 strncpy(outname
, argv
[1], cnt
);
434 sprintf(outfile
, "%s.h", outname
);
436 ofd
= fopen(outfile
, "w");
438 fprintf(stderr
, "Error: cannot open (%s) for writing\n", outfile
);
442 fprintf(ofd
, "#define %s_width\t\t%d\n", outname
, width1
);
443 fprintf(ofd
, "#define %s_height\t\t%d\n", outname
, height1
);
444 fprintf(ofd
, "#define %s_colors\t\t%d\n", outname
, fcnt
);
445 fprintf(ofd
, "#define %s_back\t\t%d\n", outname
, (int) data1
[0]);
446 fprintf(ofd
, "int\t%s_reds[] = {", outname
);
448 for (i
= 0; i
< fcnt
; i
++) {
449 if (i
== (fcnt
- 1)) {
450 fprintf(ofd
, "%d};\n", fcolrs
[i
].red
);
452 fprintf(ofd
, "%d, ", fcolrs
[i
].red
);
453 if (++num_cnt
>= COLS_PER_LINE
) {
459 fprintf(ofd
, "int\t%s_greens[] = {", outname
);
461 for (i
= 0; i
< fcnt
; i
++) {
462 if (i
== (fcnt
- 1)) {
463 fprintf(ofd
, "%d};\n", fcolrs
[i
].green
);
465 fprintf(ofd
, "%d, ", fcolrs
[i
].green
);
466 if (++num_cnt
>= COLS_PER_LINE
) {
472 fprintf(ofd
, "int\t%s_blues[] = {", outname
);
474 for (i
= 0; i
< fcnt
; i
++) {
475 if (i
== (fcnt
- 1)) {
476 fprintf(ofd
, "%d};\n", fcolrs
[i
].blue
);
478 fprintf(ofd
, "%d, ", fcolrs
[i
].blue
);
479 if (++num_cnt
>= COLS_PER_LINE
) {
485 fprintf(ofd
, "unsigned char\t%s_rasterA[] = {\n", outname
);
487 for (i
= 0; i
< (width1
* height1
); i
++) {
488 fprintf(ofd
, "0x%02x,", data1
[i
]);
489 if (++num_cnt
>= NUMS_PER_LINE
) {
494 fprintf(ofd
, "};\n");
495 fprintf(ofd
, "unsigned char\t%s_rasterB[] = {\n", outname
);
497 for (i
= 0; i
< (width1
* height1
); i
++) {
498 fprintf(ofd
, "0x%02x,", data2
[i
]);
499 if (++num_cnt
>= NUMS_PER_LINE
) {
504 fprintf(ofd
, "};\n");
This page took 0.089478 seconds and 4 git commands to generate.