]>
iEval git - xfishtank.git/blob - rasttofish/rasttofish.c
1 /* rasttofish.c - read a Sun rasterfile and produce an xfish header file
3 ** All code to read sun rasters came from rasttopnm, his copyright follows.
5 ** Copyright (C) 1989, 1991 by Jef Poskanzer.
7 ** Permission to use, copy, modify, and distribute this software and its
8 ** documentation for any purpose and without fee is hereby granted, provided
9 ** that the above copyright notice appear in all copies and that both that
10 ** copyright notice and this permission notice appear in supporting
11 ** documentation. This software is provided "as is" without express or
16 #define ARGS(alist) alist
18 #define ARGS(alist) ()
27 #define DEFAULT_NAME "fish"
30 unsigned int red
, green
, blue
;
35 pm_readbiglong(in
, lP
)
41 if ((c
= getc(in
)) == EOF
)
43 *lP
= (c
& 0xff) << 24;
44 if ((c
= getc(in
)) == EOF
)
46 *lP
|= (c
& 0xff) << 16;
47 if ((c
= getc(in
)) == EOF
)
49 *lP
|= (c
& 0xff) << 8;
50 if ((c
= getc(in
)) == EOF
)
57 pr_load_header(in
, hP
)
59 struct rasterfile
*hP
;
61 if (pm_readbiglong(in
, &(hP
->ras_magic
)) == -1)
63 if (hP
->ras_magic
!= RAS_MAGIC
)
65 if (pm_readbiglong(in
, &(hP
->ras_width
)) == -1)
67 if (pm_readbiglong(in
, &(hP
->ras_height
)) == -1)
69 if (pm_readbiglong(in
, &(hP
->ras_depth
)) == -1)
71 if (pm_readbiglong(in
, &(hP
->ras_length
)) == -1)
73 if (pm_readbiglong(in
, &(hP
->ras_type
)) == -1)
75 if (pm_readbiglong(in
, &(hP
->ras_maptype
)) == -1)
77 if (pm_readbiglong(in
, &(hP
->ras_maplength
)) == -1)
83 pr_load_colormap(in
, hP
, colormap
)
85 struct rasterfile
*hP
;
88 if (colormap
== NULL
|| hP
->ras_maptype
== RMT_NONE
) {
91 if (fread(junk
, 1, hP
->ras_maplength
, in
) != hP
->ras_maplength
)
94 colormap
->type
= hP
->ras_maptype
;
95 switch (hP
->ras_maptype
) {
97 colormap
->length
= hP
->ras_maplength
/ 3;
98 colormap
->map
[0] = (unsigned char *) malloc(colormap
->length
);
99 if (colormap
->map
[0] == NULL
)
101 colormap
->map
[1] = (unsigned char *) malloc(colormap
->length
);
102 if (colormap
->map
[1] == NULL
) {
103 free(colormap
->map
[0]);
106 colormap
->map
[2] = (unsigned char *) malloc(colormap
->length
);
107 if (colormap
->map
[2] == NULL
) {
108 free(colormap
->map
[0]);
109 free(colormap
->map
[1]);
112 if (fread(colormap
->map
[0], 1, colormap
->length
, in
) != colormap
->length
||
113 fread(colormap
->map
[1], 1, colormap
->length
, in
) != colormap
->length
||
114 fread(colormap
->map
[2], 1, colormap
->length
, in
) != colormap
->length
) {
115 free(colormap
->map
[0]);
116 free(colormap
->map
[1]);
117 free(colormap
->map
[2]);
123 colormap
->length
= hP
->ras_maplength
;
124 colormap
->map
[0] = (unsigned char *) malloc(colormap
->length
);
125 if (colormap
->map
[0] == NULL
)
127 colormap
->map
[2] = colormap
->map
[1] = colormap
->map
[0];
128 if (fread(colormap
->map
[0], 1, hP
->ras_maplength
, in
) != hP
->ras_maplength
) {
129 free(colormap
->map
[0]);
135 fprintf(stderr
, "unknown colormap type\n");
144 mem_create(w
, h
, depth
)
150 p
= (struct pixrect
*) malloc(sizeof(struct pixrect
));
157 m
= p
->pr_data
= (struct mpr_data
*) malloc(sizeof(struct mpr_data
));
162 /* According to the documentation, linebytes is supposed to be rounded
163 ** up to a longword (except on 386 boxes). However, this turns out
164 ** not to be the case. In reality, all of Sun's code rounds up to
165 ** a short, not a long.
167 m
->md_linebytes
= (w
* depth
+ 15) / 16 * 2;
171 m
->md_image
= (unsigned char *) malloc(m
->md_linebytes
* h
);
172 if (m
->md_image
== NULL
) {
186 free(p
->pr_data
->md_image
);
195 pr_load_image(in
, hP
, colormap
)
197 struct rasterfile
*hP
;
198 colormap_t
*colormap
;
201 unsigned char *beimage
;
202 register unsigned char *bep
;
203 register unsigned char *bp
;
204 register unsigned char c
;
206 register int j
, count
;
208 p
= mem_create(hP
->ras_width
, hP
->ras_height
, hP
->ras_depth
);
212 switch (hP
->ras_type
) {
214 fprintf(stderr
, "old rasterfile type is not supported\n");
218 fprintf(stderr
, "tiff rasterfile type is not supported\n");
222 fprintf(stderr
, "iff rasterfile type is not supported\n");
225 case RT_EXPERIMENTAL
:
226 fprintf(stderr
, "experimental rasterfile type is not supported\n");
231 /* Ignore hP->ras_length. */
232 i
= p
->pr_size
.y
* p
->pr_data
->md_linebytes
;
233 if (fread(p
->pr_data
->md_image
, 1, i
, in
) != i
) {
239 case RT_BYTE_ENCODED
:
240 beimage
= (unsigned char *) malloc(hP
->ras_length
);
241 if (beimage
== NULL
) {
245 if (fread(beimage
, 1, hP
->ras_length
, in
) != hP
->ras_length
) {
251 bp
= p
->pr_data
->md_image
;
252 for (i
= 0; i
< hP
->ras_length
;) {
255 count
= (*bep
++) + 1;
261 for (j
= 0; j
< count
; ++j
)
274 fprintf(stderr
, "unknown rasterfile type\n");
283 ReadRaster(ifp
, w
, h
, colrs
)
286 struct rgb_color
*colrs
;
288 struct rasterfile header
;
289 colormap_t pr_colormap
;
293 unsigned char *rdata
;
300 /* Read in the rasterfile. First the header. */
301 if (pr_load_header(ifp
, &header
) != 0) {
302 fprintf(stderr
, "unable to read in rasterfile header\n");
306 *w
= header
.ras_width
;
307 *h
= header
.ras_height
;
309 depth
= header
.ras_depth
;
312 fprintf(stderr
, "invalid width\n");
316 fprintf(stderr
, "invalid height\n");
320 /* If there is a color map, read it. */
321 if (header
.ras_maplength
!= 0) {
322 if (pr_load_colormap(ifp
, &header
, &pr_colormap
) != 0) {
323 fprintf(stderr
, "no colormap\n");
328 /* Check the depth and color map. */
331 if (header
.ras_maptype
!= RMT_EQUAL_RGB
) {
332 fprintf(stderr
, "not an RGB colormap\n");
338 fprintf(stderr
, "Can only handle depth 8 images\n");
342 for (i
= 0; i
< maxval
; i
++) {
343 colrs
[i
].red
= pr_colormap
.map
[0][i
];
344 colrs
[i
].green
= pr_colormap
.map
[1][i
];
345 colrs
[i
].blue
= pr_colormap
.map
[2][i
];
348 /* Now load the data. The pixrect returned is a memory pixrect. */
349 if ((pr
= pr_load_image(ifp
, &header
, NULL
)) == NULL
) {
350 fprintf(stderr
, "unable to read image from the rasterfile\n");
354 linesize
= ((struct mpr_data
*) pr
->pr_data
)->md_linebytes
;
355 data
= ((struct mpr_data
*) pr
->pr_data
)->md_image
;
357 rdata
= (unsigned char *) malloc((*w
) * (*h
));
359 for (j
= 0; j
< (*h
); j
++) {
360 dptr
= (unsigned char *) data
;
361 for (i
= 0; i
< (*w
); i
++) {
372 MapColors(data1
, colrs1
, data2
, colrs2
, width
, height
, Cnt
)
373 unsigned char *data1
;
374 struct rgb_color
*colrs1
;
375 unsigned char *data2
;
376 struct rgb_color
*colrs2
;
382 int Mapping
[MAXCOLORS
];
384 struct rgb_color
*fcolrs
;
386 fcolrs
= (struct rgb_color
*) malloc((MAXCOLORS
+ 1) * sizeof(struct rgb_color
));
390 for (i
= 0; i
< (width
* height
); i
++) {
392 for (j
= 0; j
< fcnt
; j
++) {
393 if ((fcolrs
[j
].red
== colrs1
[indx
].red
) &&
394 (fcolrs
[j
].green
== colrs1
[indx
].green
) &&
395 (fcolrs
[j
].blue
== colrs1
[indx
].blue
)) {
400 fcolrs
[j
].red
= colrs1
[indx
].red
;
401 fcolrs
[j
].green
= colrs1
[indx
].green
;
402 fcolrs
[j
].blue
= colrs1
[indx
].blue
;
405 if (fcnt
> MAXCOLORS
) {
406 fprintf(stderr
, "Error: cannot use more than %d colors in your fish\n",
414 for (i
= 0; i
< (width
* height
); i
++) {
416 *ptr
= (unsigned char) (Mapping
[indx
]);
421 for (i
= 0; i
< (width
* height
); i
++) {
423 for (j
= 0; j
< fcnt
; j
++) {
424 if ((fcolrs
[j
].red
== colrs2
[indx
].red
) &&
425 (fcolrs
[j
].green
== colrs2
[indx
].green
) &&
426 (fcolrs
[j
].blue
== colrs2
[indx
].blue
)) {
431 fcolrs
[j
].red
= colrs2
[indx
].red
;
432 fcolrs
[j
].green
= colrs2
[indx
].green
;
433 fcolrs
[j
].blue
= colrs2
[indx
].blue
;
436 if (fcnt
> MAXCOLORS
) {
437 fprintf(stderr
, "Error: cannot use more than %d colors in your fish\n",
445 for (i
= 0; i
< (width
* height
); i
++) {
447 *ptr
= (unsigned char) (Mapping
[indx
]);
451 for (i
= 0; i
< fcnt
; i
++) {
452 fcolrs
[i
].red
= fcolrs
[i
].red
* 256;
453 fcolrs
[i
].green
= fcolrs
[i
].green
* 256;
454 fcolrs
[i
].blue
= fcolrs
[i
].blue
* 256;
456 for (i
= fcnt
; i
< 16; i
++) {
477 struct rgb_color colrs1
[MAXCOLORS
];
478 struct rgb_color colrs2
[MAXCOLORS
];
479 struct rgb_color
*fcolrs
;
481 unsigned char *ptr
, *ptr1
, *ptr2
;
482 unsigned char *data1
, *data2
;
483 int width1
, height1
, width2
, height2
;
489 ifp
= fopen(argv
[1], "r");
495 data
= ReadRaster(ifp
, &width1
, &height1
, colrs1
);
497 /* Mirror around Y axis, and copy */
498 for (i
= 0; i
< MAXCOLORS
; i
++) {
499 colrs2
[i
].red
= colrs1
[i
].red
;
500 colrs2
[i
].green
= colrs1
[i
].green
;
501 colrs2
[i
].blue
= colrs1
[i
].blue
;
505 data1
= (unsigned char *) malloc(width1
* height1
);
506 data2
= (unsigned char *) malloc(width1
* height1
);
509 for (j
= 0; j
< height1
; j
++) {
510 ptr
= (unsigned char *) (data
+ ((j
+ 1) * width1
) - 1);
511 for (i
= 0; i
< width1
; i
++) {
518 fcolrs
= MapColors(data1
, colrs1
, data2
, colrs2
, width1
, height1
, &fcnt
);
525 while ((argv
[1][cnt
] != '.') && (argv
[1][cnt
] != '\0')) {
530 outname
= (char *) malloc(strlen(DEFAULT_NAME
) + 1);
531 strcpy(outname
, DEFAULT_NAME
);
533 outname
= (char *) malloc(cnt
+ 1);
534 strncpy(outname
, argv
[1], cnt
);
538 sprintf(outfile
, "%s.h", outname
);
540 ofd
= fopen(outfile
, "w");
542 fprintf(stderr
, "Error: cannot open (%s) for writing\n", outfile
);
546 fprintf(ofd
, "#define %s_width\t\t%d\n", outname
, width1
);
547 fprintf(ofd
, "#define %s_height\t\t%d\n", outname
, height1
);
548 fprintf(ofd
, "#define %s_colors\t\t%d\n", outname
, fcnt
);
549 fprintf(ofd
, "#define %s_back\t\t%d\n", outname
, (int) data1
[0]);
550 fprintf(ofd
, "int\t%s_reds[] = {", outname
);
551 for (i
= 0; i
< fcnt
; i
++) {
552 if (i
== (fcnt
- 1)) {
553 fprintf(ofd
, "%d};\n", fcolrs
[i
].red
);
555 fprintf(ofd
, "%d, ", fcolrs
[i
].red
);
558 fprintf(ofd
, "int\t%s_greens[] = {", outname
);
559 for (i
= 0; i
< fcnt
; i
++) {
560 if (i
== (fcnt
- 1)) {
561 fprintf(ofd
, "%d};\n", fcolrs
[i
].green
);
563 fprintf(ofd
, "%d, ", fcolrs
[i
].green
);
566 fprintf(ofd
, "int\t%s_blues[] = {", outname
);
567 for (i
= 0; i
< fcnt
; i
++) {
568 if (i
== (fcnt
- 1)) {
569 fprintf(ofd
, "%d};\n", fcolrs
[i
].blue
);
571 fprintf(ofd
, "%d, ", fcolrs
[i
].blue
);
574 fprintf(ofd
, "unsigned char\t%s_rasterA[] = {\n", outname
);
575 for (i
= 0; i
< (width1
* height1
); i
++) {
576 fprintf(ofd
, "0x%02x,", data1
[i
]);
578 fprintf(ofd
, "};\n");
579 fprintf(ofd
, "unsigned char\t%s_rasterB[] = {\n", outname
);
580 for (i
= 0; i
< (width1
* height1
); i
++) {
581 fprintf(ofd
, "0x%02x,", data2
[i
]);
583 fprintf(ofd
, "};\n");
This page took 0.075736 seconds and 4 git commands to generate.