9 #include <intuition/intuition.h>
10 #include <libraries/dos.h>
11 #include <exec/types.h>
12 #include <exec/memory.h>
13 #include <graphics/gfxbase.h>
14 #include <graphics/view.h>
21 #if !defined(MCH_AMIGA) && !defined(LATTICE)
25 #include <proto/intuition.h>
26 #include <proto/graphics.h>
27 #include <proto/dos.h>
28 #include <proto/exec.h>
30 #include <iffpragmas.h>
34 #define MIN(x, y) ((x) < (y) ? (x) : (y))
35 #define ABS(x) ((x) >= 0
? (x) : -(x))
36 #define MAX(x,y) ((x) > (y) ? (x) : (y))
37 #define index(r, g, b) ((((r) >> 4
) << 8
) | (((g) >> 4
) << 4
) | ((b) >> 4
))
39 ((ABS (((cusage[a].i >> 8
) & 0xf
) - ((cusage[b].i >> 8
) & 0xf
))) * 30
)
41 ((ABS (((cusage[a].i >> 4
) & 0xf
) - ((cusage[b].i >> 4
) & 0xf
))) * 59
)
43 ((ABS ((cusage[a].i & 0xf
) - (cusage[b].i & 0xf
))) * 11
)
44 #define mapx(x) (x + offsetx)
45 #define mapy(y) (sh - (y) - 1
- offsety)
46 #define fmapy(y) (y + offsety)
47 #define inside(x, y) ((x) >= 0
&& (y) >= 0
&& (x) < sw && (y) < sh)
49 #define RED(x) ((2
<< 4
) | x)
50 #define GREEN(x) ((3
<< 4
) | x)
51 #define BLUE(x) ((1
<< 4
) | x)
52 #define SQ(x) (SQtmp = (x), SQtmp * SQtmp)
55 #define MAX_Y_NTSC 241
58 #define BASENAME(x) (strrchr((x),'/') ? strrchr((x),'/')+1
:
59 (strrchr((x),':') ? strrchr((x),':')+1
: (x)))
61 struct GfxBase *GfxBase = NULL;
68 0, 0, 0, 0, 6, 1, 0, 0, CUSTOMSCREEN, NULL,
"GetAmi", NULL, NULL
71 static struct Image
img = {
72 0, 0, 10, 10, 0, NULL, 0, 0, NULL
76 NULL, 0, 0, 10, 10, GADGHCOMP | GADGIMAGE, RELVERIFY, BOOLGADGET, (APTR)&img,
77 NULL, NULL, 0, NULL, 1, NULL
81 0, 0, 0, 0, 1, 0, GADGETUP | MENUPICK | MENUVERIFY | REQSET | REQCLEAR,
82 SMART_REFRESH | BACKDROP | BORDERLESS,
83 &CloseGadget, NULL, NULL, NULL, NULL, 0, 0, 0, 0, CUSTOMSCREEN
90 static struct Window *
w1 = NULL, *
w2 = NULL, *
w3 = NULL;
91 static struct RastPort *
rp1 = NULL, *
rp2 = NULL, *
rp3 = NULL;
92 static struct Screen *
s1 = NULL;
93 static struct Screen *
s2 = NULL;
94 static struct Screen *
s3 = NULL;
95 static struct ViewPort *
vp1 = NULL, *
vp2 = NULL, *
vp3 = NULL;
98 static unsigned int R[32],
G[32],
B[32];
111 static USHORT RenderPtr[] = {
135 extern struct Window *OpenWindow ();
136 extern struct Screen *OpenScreen ();
137 extern struct Library *OpenLibrary ();
138 extern char *
mktemp(
char *);
141 UBYTE handleIDCMP (
void);
146 { 1, 0, JAM2, 0, 0, NULL,
"Save", NULL },
147 { 1, 0, JAM2, 0, 0, NULL,
"Save As", NULL },
148 { 1, 0, JAM2, 0, 0, NULL,
"Quit", NULL }
152 { &actions[1], 0, 0, 68, 8, ITEMTEXT | ITEMENABLED | HIGHBOX | COMMSEQ, 0,
153 (APTR)&txt[0], NULL,
'S', NULL, MENUNULL
155 { &actions[2], 0, 10, 68, 8, ITEMTEXT | ITEMENABLED | HIGHBOX | COMMSEQ, 0,
156 (APTR)&txt[1], NULL,
'A', NULL, MENUNULL
158 { NULL, 0, 20, 68, 8, ITEMTEXT | ITEMENABLED | HIGHBOX | COMMSEQ, 0,
159 (APTR)&txt[2], NULL,
'Q', NULL, MENUNULL
164 NULL, 10, 0, 56, 0, MENUENABLED,
"Actions", &actions[0]
171 fnam, ufnam, 0,
sizeof(fnam), 0, 0, 0, 0, 0, 0, NULL, 0, NULL
174 static struct IntuiText
ftxt = {
175 0, 1, JAM2, 9, 10, NULL,
"File:", NULL
178 static SHORT fbcoord[] = {1, 1, 146, 1, 146, 26, 1, 26, 1, 1};
181 0, 0, 0, 1, JAM1, 5, fbcoord, NULL
185 NULL, 58, 10, 80, 8, GADGHCOMP, ENDGADGET, STRGADGET | REQGADGET,
186 NULL, NULL, NULL, 0, (APTR)&fgadinfo, 0, NULL
192 static struct Requester
freq = {
193 NULL, 0, 0,
FREQW,
FREQH, 0, 0, &fgad, &fbord, &ftxt, NULL, 1, NULL,
194 {NULL}, NULL, NULL, {NULL}
197 void main(
int,
char **);
198 int compare (
struct cuse *,
struct cuse *);
199 int find_most_used (
void);
200 int display_rle (
void);
201 void show_rgb (
void);
210 void AdjustScreen(
register struct Screen *,
int *,
int *);
212 void FixColors(
void);
213 void ResetColors(
void);
215 int CalcHAMWidth(
int);
216 int CalcHeight(
int,
int);
220 #define OptSetAPen1(rp, col) curcol1 = (col); if(curcol1) SetAPen((rp), curcol1)
221 #define OptSetAPen2(rp, col) curcol2 = (col); if(curcol2) SetAPen((rp), curcol2)
222 #define OptSetAPen3(rp, col) curcol3 = (col); if(curcol3) SetAPen((rp), curcol3)
224 #define OptWritePixel1(rp, x, y) if(curcol1) WritePixel((rp), (x), (y))
225 #define OptWritePixel2(rp, x, y) if(curcol2) WritePixel((rp), (x), (y))
226 #define OptWritePixel3(rp, x, y) if(curcol3) WritePixel((rp), (x), (y))
231 int i, SW=0, SH=0, dump=0, load=0, dx, dy, max_x, max_y;
232 ULONG signalmask, signals;
236 struct Preferences prefs;
244 #if !defined(MCH_AMIGA) && !defined(LATTICE)
245 (
void)signal(SIGINT, (
void (*) (
int))
_abort);
247 while ((i = getopt(argc, argv,
"w:h:o:dlb3fH")) != EOF) {
285 "Usage: %s [-dlfb3H] [-w width] [-h height] [-o IFFfile] [file]\n",
296 fprintf (stderr,
"cannot open input file %s\n",
infile),
_abort ();
300 fprintf (stderr,
"cannot create a temp name for tfile\n"),
_abort ();
302 if ((
tfd = fopen (
tfile,
"w+")) == NULL)
303 fprintf (stderr,
"cannot open output file %s\n",
tfile),
_abort ();
306 (
struct GfxBase *) OpenLibrary (
"graphics.library", 0L)) == NULL)
307 fprintf (stderr,
"Can't open graphics.library\n"),
_abort ();
308 pal = (GfxBase->DisplayFlags & PAL) ? 1 : 0;
311 OpenLibrary (
"intuition.library", 0L)) == NULL)
312 fprintf (stderr,
"Can't open intuition.library\n"),
_abort ();
315 if ((IFFBase = OpenLibrary(IFFNAME, IFFVERSION)) == NULL) {
317 actions[0].Flags &= ~ITEMENABLED;
318 actions[1].Flags &= ~ITEMENABLED;
323 if (find_most_used () == -1)
324 fprintf (stderr,
"error in find_most_used\n"),
_abort ();
327 f = fopen(
"cmap",
"r");
329 fprintf(stderr,
"Can't find cmap file\n");
331 fread((
char *)
R,
sizeof(
R), 1, f);
332 fread((
char *)
G,
sizeof(
G), 1, f);
333 fread((
char *)
B,
sizeof(
B), 1, f);
338 f = fopen(
"cmap",
"w");
340 fprintf(stderr,
"Can't create cmap file\n");
342 fwrite((
char *)
R,
sizeof(
R), 1, f);
343 fwrite((
char *)
G,
sizeof(
G), 1, f);
344 fwrite((
char *)
B,
sizeof(
B), 1, f);
353 sh = CalcHeight(height, pal);
360 fprintf (stderr,
"found 0!! colors\n"),
_abort ();
363 screen.Width = sw, screen.Height = sh, screen.Depth = sd;
371 window.Width = sw, window.Height = sh;
376 sh = CalcHeight(height, pal);
379 screen.Width = sw, screen.Height = sh, screen.Depth = sd;
381 screen.ViewModes = ((sh >
MAX_Y_PAL) ? LACE : 0) | HAM;
383 screen.ViewModes = ((sh >
MAX_Y_NTSC) ? LACE : 0) | HAM;
385 window.Width = sw, window.Height = sh;
389 if (screen.ViewModes & HIRES) {
390 actions[i].Width += COMMWIDTH;
392 actions[i].Width += LOWCOMMWIDTH;
396 GetPrefs(&prefs,
sizeof(prefs));
397 Ptr = (USHORT *)AllocMem(POINTERSIZE *
sizeof(USHORT), MEMF_CHIP);
399 fprintf (stderr,
"Can't allocate chip memory for pointer\n");
404 if ((s3 = OpenScreen (&screen)) == NULL)
405 fprintf (stderr,
"Can't open screen 3\n"),
_abort ();
406 AdjustScreen(
s3, &dx, &dy);
407 ShowTitle (s3, FALSE);
409 if ((w3 = OpenWindow (&window)) == NULL)
410 fprintf (stderr,
"Can't open window 3\n"),
_abort ();
411 vp3 = &(s3->ViewPort);
413 SetMenuStrip(w3, &menu);
415 if ((s2 = OpenScreen (&screen)) == NULL)
416 fprintf (stderr,
"Can't open screen 2\n"),
_abort ();
417 AdjustScreen(
s2, &dx, &dy);
418 ShowTitle (s2, FALSE);
420 if ((w2 = OpenWindow (&window)) == NULL)
421 fprintf (stderr,
"Can't open window 2\n"),
_abort ();
422 vp2 = &(s2->ViewPort);
424 SetMenuStrip(w2, &menu);
426 if ((s1 = OpenScreen (&screen)) == NULL)
427 fprintf (stderr,
"Can't open screen 1\n"),
_abort ();
428 AdjustScreen(
s1, &dx, &dy);
429 ShowTitle (s1, FALSE);
431 if ((w1 = OpenWindow (&window)) == NULL)
432 fprintf (stderr,
"Can't open window 1\n"),
_abort ();
433 vp1 = &(s1->ViewPort);
435 SetMenuStrip(w1, &menu);
438 if (s1->ViewPort.Modes & LACE){
442 if (s1->ViewPort.Modes & HIRES){
446 freq.LeftEdge = (sw -
FREQW) / 2;
448 freq.LeftEdge = (max_x -
FREQW) / 2;
451 freq.TopEdge = (sh -
FREQH) / 2;
453 freq.TopEdge = (max_y -
FREQH) / 2;
457 for (i = 0; i <
nused; i++) {
459 SetRGB4 (
vp1, (
long) i, (
long) i, 0L, 0L);
460 SetRGB4 (
vp2, (
long) i, 0L, (
long) i, 0L);
461 SetRGB4 (
vp3, (
long) i, 0L, 0L, (
long) i);
463 SetRGB4 (
vp1, (
long) i, (
long)
R[i], (
long)
G[i], (
long)
B[i]);
467 memcpy(Ptr, RenderPtr, POINTERSIZE *
sizeof(USHORT));
468 SetPointer(w1, Ptr, 16, 16, dx - 1, dy);
470 SetPointer(w2, Ptr, 16, 16, dx - 1, dy);
471 SetPointer(w3, Ptr, 16, 16, dx - 1, dy);
476 memcpy(Ptr, prefs.PointerMatrix, POINTERSIZE *
sizeof(USHORT));
477 SetPointer(w1, Ptr, 16, 16, dx - 1 + prefs.XOffset, dy + prefs.YOffset);
479 SetPointer(w2, Ptr, 16, 16, dx - 1 + prefs.XOffset, dy + prefs.YOffset);
480 SetPointer(w3, Ptr, 16, 16, dx - 1 + prefs.XOffset, dy + prefs.YOffset);
492 signalmask = (1L << w1->UserPort->mp_SigBit) |
493 (1L << w2->UserPort->mp_SigBit) |
494 (1L << w3->UserPort->mp_SigBit);
496 signalmask = 1L << w1->UserPort->mp_SigBit;
500 SetTaskPri(FindTask(0L), 20L);
504 if (!(havemenu || havereq)) {
507 done = handleIDCMP();
508 if (!(havemenu || havereq)) {
511 done |= handleIDCMP();
512 if (!(havemenu || havereq)) {
515 done |= handleIDCMP();
517 if (!threescreens || havemenu || havereq) {
518 signals = Wait(signalmask);
519 if (signals & signalmask) {
520 done = handleIDCMP();
540 find_most_used (
void)
545 unsigned char r, g, b;
546 int ncolors, i, j, x, y, packed, pix, col;
553 for (i = 3; i < rle_f_hdr
.ncolors; i++)
563 depth = 0, packed = 0;
569 packed = como ? 1 : 0;
574 for (i = 0; i < 3; i++)
576 rle_f_hdr
.xmin + 1)) == NULL) {
577 for (j = 0; j < i; j++)
578 free ((
char *)
sl[j]);
597 for (i = 0; i <
MIN (3, ncolors); i++)
599 for (i =
MIN (3, ncolors); i < ncolors; i++)
617 for (x = 0; x <
width; x++)
618 r =
sl[0][x], g =
sl[1][x], b =
sl[2][x], cusage[
index(r, g, b)]
.j++;
619 }
else if (ncolors == 1 && !packed) {
620 for (x = 0; x <
width; x++) {
627 for (col = 0, x = 0; x <
width; col = (col + 1) & 7, x++) {
628 pix = (
sl[0][x / 8] & (1 << (7 - col))) ? 0: 255;
629 r = pix, g = pix, b = pix, cusage[
index(r, g, b)]
.j++;
633 for (i = 0; i < 3; i++)
634 free((
char *)
sl[i]);
637 for (i = 0; i < 4096; i++) {
642 printf (
"colors used: %d\n",
nused);
644 qsort ((
char *) cusage, 4096,
sizeof (
struct cuse), compare);
651 for (i = 0; i < 16; i++) {
652 R[i] =
G[i] =
B[i] = i;
658 for (i = 0; i < 32; i++) {
659 R[i] = (cusage[i]
.i >> 8) & 0xf;
660 G[i] = (cusage[i]
.i >> 4) & 0xf;
661 B[i] = cusage[i]
.i & 0xf;
668 for (i = nused - 1; i >= 0; i--) {
671 for (j = 0; j < i; j++)
672 if (rdist (i, j) + gdist (i, j) + bdist (i, j) <= 1) {
673 cusage[i].j = 0, nleft--;
677 printf (
"colors left %d\n", nleft);
680 for (i = 0, j = 0; j < 16 && i <
nused; i++)
682 R[j] = (cusage[i]
.i >> 8) & 0xf;
683 G[j] = (cusage[i]
.i >> 4) & 0xf;
684 B[j] = cusage[i]
.i & 0xf;
692 static unsigned char r,
g,
b;
699 int ncolors, i, j, packed, pix, col;
706 for (i = 3; i < rle_f_hdr
.ncolors; i++)
716 depth = 0, packed = 0;
722 packed = como ? 1 : 0;
729 for (i = 0; i < 3; i++)
731 rle_f_hdr
.xmin + 1)) == NULL) {
732 for (j = 0; j < i; j++)
733 free ((
char *)
sl[j]);
747 }
else if (ncolors == 1 && !packed) {
755 for (col = 0,
x = 0;
x <
width; col = (col + 1) & 7,
x++) {
756 pix = (
sl[0][
x / 8] & (1 << (7 - col))) ? 0: 255;
757 r = pix,
g = pix,
b = pix, show_rgb ();
761 for (i = 0; i < 3; i++)
762 free((
char *)
sl[i]);
771 long d, td, d1, d2, d3, rd, gd, bd;
773 static unsigned char pr, pg, pb;
786 r = g = b = 0.35 * r + 0.55 * g + 0.10 * b;
788 r =
g =
b = (35 *
r + 55 *
g + 10 *
b) / 100;
792 r >>= 4,
g >>= 4,
b >>= 4;
794 r = (r >> 4) & 0xf, g = (g >> 4) & 0xf, b = (b >> 4) & 0xf;
803 for (i = 0; i <
nused; i++) {
804 if (
R[i] == (
int)
r &&
G[i] == (
int)
g &&
B[i] == (
int)
b) {
811 for (i = 0, d = 3276700, j = -1; i < 16; i++) {
818 if (d == 0 ||
x == 0) {
820 pr =
R[j], pg =
G[j], pb =
B[j];
822 rd =
SQ (
r - pr), gd =
SQ (
g - pg), bd =
SQ (
b - pb);
823 d1 = rd + gd, d2 = rd + bd, d3 = gd + bd;
824 if (d <= d1 && d <= d2 && d <= d3) {
826 pr =
R[j], pg =
G[j], pb =
B[j];
827 }
else if (d1 <= d2 && d1 <= d3) {
830 }
else if (d2 <= d3 && d2 <= d1) {
885 FreeMem(Ptr, POINTERSIZE *
sizeof(USHORT));
888 CloseLibrary ((
struct Library *)GfxBase);
908 struct IntuiMessage *message = NULL;
909 USHORT code, selection;
910 ULONG class, itemnum;
913 while ((message = (
struct IntuiMessage *)GetMsg(w1->UserPort)) ||
914 (threescreens && (message=(
struct IntuiMessage *)GetMsg(w2->UserPort))) ||
915 (threescreens && (message=(
struct IntuiMessage *)GetMsg(w3->UserPort)))) {
916 class = message->Class;
917 code = message->Code;
924 if (threescreens) ScreenToFront(s1);
925 ActivateGadget(&fgad, w1, &freq);
938 while (selection != MENUNULL) {
939 itemnum = ITEMNUM(selection);
943 if (threescreens) ScreenToFront(s1);
947 strcpy(fnam, infile);
949 if ( (t >= 4) && (strcmp(&fnam[t-4],
".rle") == 0)) {
952 strcat(fnam,
".ilbm");
957 if (threescreens) ScreenToFront(s1);
967 selection = ((
struct MenuItem *)ItemAddress
968 (&menu, (LONG)selection))->NextSelect;
976 ReplyMsg((
struct Message *)message);
993 name = malloc(strlen(
fnam) + 4);
995 fprintf(stderr,
"malloc failed\n");
1000 SaveBitMap(name, s1->BitMap,vp1->ColorMap->ColorTable,(isham ? 0x81 : 1));
1003 SaveBitMap(name, s2->BitMap,vp2->ColorMap->ColorTable,(isham ? 0x81 : 1));
1006 SaveBitMap(name, s3->BitMap,vp3->ColorMap->ColorTable,(isham ? 0x81 : 1));
1009 SaveBitMap(fnam, s1->BitMap,vp1->ColorMap->ColorTable,(isham ? 0x81 : 1));
1021 SetBPen(
rp1, 0L); SetBPen(
rp2, 0L); SetBPen(
rp3, 0L);
1022 SetRGB4(
vp1, 1L, 15L, 15L, 15L);
1023 SetRGB4(
vp2, 1L, 15L, 15L, 15L);
1024 SetRGB4(
vp3, 1L, 15L, 15L, 15L);
1028 SetRGB4(
vp1, 1L, (
R[0]+8) % 16, (
G[0]+8) % 16, (
B[0]+8) % 16);
1036 SetRGB4(
vp1, 1L, 1L, 1L, 1L);
1037 SetRGB4(
vp2, 1L, 1L, 1L, 1L);
1038 SetRGB4(
vp3, 1L, 1L, 1L, 1L);
1040 SetRGB4(
vp1, 1L, (
long)
R[1], (
long)
G[1], (
long)
B[1]);
1045 CalcWidth(
int width)
1047 if (width <= 320)
return 320;
1048 if (width <=
MAX_X)
return width;
1049 if (width <= 640)
return 640;
1054 CalcHAMWidth(
int width)
1056 if (width <= 320)
return 320;
1061 CalcHeight(
int height,
int pal)
1064 if (height <= 256)
return 256;
1066 if (height <= 512)
return 512;
1069 if (height <= 200)
return 200;
1071 if (height <= 400)
return 400;
#define RLE_SET_BIT(glob, bit)
static unsigned int R[32]
static struct RastPort * rp1
static struct Window * w1
static struct IntuiText ftxt
#define OptWritePixel3(rp, x, y)
#define OptWritePixel2(rp, x, y)
void main(int argc, char **argv)
static struct Gadget fgad
static struct ViewPort * vp2
static struct Window * w3
int rle_get_setup(rle_hdr *the_hdr)
static struct NewScreen screen
int rle_getrow(rle_hdr *the_hdr, scanline)
static struct Screen * s3
static struct StringInfo fgadinfo
static struct Window * w2
static struct NewWindow window
#define OptSetAPen2(rp, col)
static unsigned int B[32]
rle_pixel ** buildmap(rle_hdr *the_hdr, int minmap, double orig_gamma, double new_gamma)
void rle_putrow(rows, int rowlen, rle_hdr *the_hdr)
enum rle_dispatch dispatch
struct Library * IntuitionBase
static struct RastPort * rp3
static struct RastPort * rp2
static struct IntuiText txt[]
static struct ViewPort * vp3
char * rle_getcom(char *name, rle_hdr *the_hdr) const
static unsigned int G[32]
#define RLE_CLR_BIT(glob, bit)
void rle_put_setup(rle_hdr *the_hdr)
const char * rle_putcom(char *value, rle_hdr *the_hdr) const
static struct Border fbord
static struct Requester freq
#define OptSetAPen3(rp, col)
#define OptSetAPen1(rp, col)
static struct Screen * s2
static struct Screen * s1
static struct MenuItem actions[]
static struct ViewPort * vp1
static struct Gadget CloseGadget
#define OptWritePixel1(rp, x, y)