{COLOR RESORTER-> Sorts the colors of a .GFX new}
{   - (c) Ansgar Scherp, Joachim Gelhaus }
uses dos,crt;

{$I _NORMVGA}

var start_col:byte;
    colors:array[0..255] of byte;
    colora:array[0..255] of boolean;
    x,y,i,b:byte;
    ch:char;
    quellef,zielf,mpaf:file of byte;
    quelle,ziel,pals,mpas:string;
    clipboard:boolean;
    minipalette:boolean;
    palet:array[0..255,1..3] of byte;

function FileExists(FileName: String): Boolean;
var
  F: file;
begin
  {$I-}
  Assign(F, FileName);
  Reset(F);
  Close(F);
  {$I+}
  FileExists := (IOResult = 0) and (FileName <> '');
end;  { FileExists }

begin
  for i:=0 to 255 do colora[i]:=false;
  writelN;
  writeln('Color Resorter Ver. 20.06.95');
  writeln;
  write('Sourcefile[.GFX]: '); readln(quelle);
     if Pos('.',quelle)=0 then quelle:=quelle+'.GFX';
     if not fileexists(quelle) then begin
       writelN(quelle,' not found...');
       halt(4);
     end;

  write('Destfile[.GFX]: '); readln(ziel);
     if ziel='' then ziel:=copy(quelle,1,pos('.',quelle))+'GFX';
     if Pos('.',ziel)=0 then ziel:=ziel+'.GFX';

  write('Palette mit remappen [Y]: '); ch:=upcase(readkey);
  if ch=chr(13) then ch:='Y'; writeln(ch);
  if (ch='Y') then begin
    write('Original Palette[.PAL]: '); readln(pals);
    if pals='' then pals:=copy(quelle,1,pos('.',quelle))+'PAL';
    if Pos('.',pals)=0 then begin
      if fileexists(pals+'.PAL') then pals:=pals+'.PAL';
      if fileexists(pals+'.MPA') then pals:=pals+'.MPA';
    end;
    if not fileexists(pals) then begin
      writeln(pals,' nothing found...');
      halt(4);
    end;
    if pos('.PAL',pals)>0 then load_palette_only(pals) else load_mini_palette_only(pals);
    write('Destpalette[.MPA]: '); readln(mpas);
    if mpas='' then mpas:=copy(ziel,1,pos('.',ziel))+'MPA';
    if Pos('.',mpas)=0 then mpas:=mpas+'.MPA';
    assign(mpaf,mpas);
    rewrite(mpaf);
  end;
  for x:=0 to 255 do write(pal[x,1]);

  write('Neue Startfarbe[0-255]: '); readln(start_col);

  write('Color 0 not resorting? [Y]: '); ch:=upcase(readkey);
  if ch=chr(13) then ch:='Y'; writeln(ch);
  if (ch='Y') then begin
    colors[0]:=0;
    colora[0]:=true;
  end;

  clipboard:=false;
  write('Clipboard Format [N]: '); ch:=upcase(readkey);
  if ch=chr(13) then ch:='N'; writeln(ch);
  if (ch='Y') then begin
    Clipboard:=true;
  end;

  minipalette:=true;
  write('Create minipalette? [Y]: '); ch:=upcase(readkey);
  if ch=chr(13) then ch:='Y'; writeln(ch);
  if (ch='N') then begin
    minipalette:=false;
  end;

  writelN('Convert ',quelle,' to ',ziel);
  if mpas<>'' then writelN('Palette ',pals,' -> ',mpas);
  writeln;
  writeln('Current Status:');
  assign(quellef,quelle);
  reset(quellef);
  assign(zielf,ziel);
  rewrite(zielf);
  repeat
    read(quellef,b);
    if (clipboard=false) or (filepos(quellef)<filesize(quellef)-16) then begin
      if colora[b]=false then begin
        colors[b]:=start_col;
        colora[b]:=true;
        if (mpas<>'') and (minipalette=true) then write(mpaf,start_col,pal[b,1],pal[b,2],pal[b,3]);
        palet[start_col,1]:=pal[b,1];
        palet[start_col,2]:=pal[b,2];
        palet[start_col,3]:=pal[b,3];
        inc(start_col);
       end;
       write(zielf,colors[b]);
    end else write(zielf,b);
    gotoxy(1,wherey); writeln(filepos(quellef),' of ',filesize(quellef),' bytes.');
                      write('Last used color: ',start_col); gotoxy(1,wherey-1);
  until eof(quellef);

  close(quellef); close(zielf);
  if mpas<>'' then close(mpaf);
  if minipalette=false then begin
    assign(mpaf,mpas);
    rewrite(mpaf);
    for x:=0 to 255 do begin
      write(mpaf,palet[x,1],palet[x,2],palet[x,3]);
    end;
    close(mpaf);
  end;
  writeln('ready');
end.
