program rip;
uses crt,dos;
var pcxhead:record
                     flag:byte;
                     version:byte;
                     encoding:byte;
                     bitsperpixel:byte;
                     xmin,ymin:integer;
                     xmax,ymax:integer;
                     hdpi:integer;
                     vdpi:integer;
                     colormap:array [1..48] of byte;
                     reserved:byte;
                     nplanes:byte;
                     bytesperline:integer;
                     paletteinfo:integer;
                     hscreensize:integer;
                     vscreensize:integer;
                     filler:array [1..54] of byte;
                 end;
   wsp,bajt:longint;
   g:file of byte;
   f:file;
   cash:array [0..20480] of byte;
   tpal:array [0..255] of byte;
   bpal:array [0..1920] of byte;
   btmp:array [0..8] of byte;
   buf:array[0..127] of byte;
   line,scan:array [0..1024] of byte;
   tmsk,tpixle:array [0..7] of byte;
   tmp,tmp_,w,w_,loa,b1,b1_,b2,col,col_:byte;
   a,posx,hip,x,y,z,pix,i,j,q,t,r,l,gfxmode:integer;
   xstep,szer,scren,pal,mul,err,opt,hig,tst:integer;
   plik,par,nam:string[14];
   h1,h2,nazwa:string;

const
  maxy=2;
  apc:array [0..4095] of byte=
  (
  0,96,96,96,96,96,96,96,96,96,97,97,97,97,97,97,176,160,144,112,113,113,113, 
  113,113,113,113,113,113,114,114,114,177,177,161,145,145,145,113,113,114, 
  114,114,114,114,114,114,114,177,177,161,162,146,146,146,146,146,114,114, 
  114,115,115,115,115,178,178,178,162,162,146,147,147,147,147,147,115,115, 
  115,115,116,178,179,179,163,163,163,163,147,147,147,148,148,148,148,116, 
  116,179,179,179,179,163,164,164,164,148,148,148,148,148,148,149,149,180, 
  180,180,180,164,164,164,164,165,149,149,149,149,149,149,149,180,180,180, 
  181,181,165,165,165,165,165,149,149,150,150,150,150,181,181,181,181,181, 
  165,165,166,166,166,166,166,150,150,150,150,181,182,182,182,182,182,166, 
  166,166,166,167,167,167,151,151,151,182,182,182,182,182,183,167,167,167, 
  167,167,167,167,167,152,152,183,183,183,183,183,183,183,167,167,168,168, 
  168,168,168,168,152,183,183,183,184,184,184,184,168,168,168,168,168,168, 
  169,169,169,184,184,184,184,184,184,184,185,169,169,169,169,169,169,169, 
  169,184,184,185,185,185,185,185,185,169,169,169,170,170,170,170,170,32,64, 
  80,80,96,96,96,97,97,97,97,97,97,97,97,97,224,1,97,97,97,97,97,97,97,97, 
  97,98,98,98,98,98,193,177,161,145,113,114,114,114,114,114,114,114,114,114, 
  115,115,194,178,178,162,146,146,146,114,114,115,115,115,115,115,115,115, 
  194,178,178,162,163,147,147,147,147,147,115,115,115,116,116,116,195,179, 
  179,179,163,163,147,148,148,148,148,148,116,116,116,116,195,179,180,180, 
  164,164,164,164,148,148,148,149,149,149,149,117,196,180,180,180,180,164, 
  165,165,165,149,149,149,149,149,149,150,197,181,181,181,181,165,165,165, 
  165,166,150,150,150,150,150,150,197,181,181,181,182,182,166,166,166,166, 
  166,150,150,151,151,151,182,182,182,182,182,182,166,166,167,167,167,167, 
  167,151,151,151,182,182,183,183,183,183,183,167,167,167,167,168,168,168, 
  152,152,183,183,183,183,183,183,184,168,168,168,168,168,168,168,168,153, 
  183,184,184,184,184,184,184,184,168,168,169,169,169,169,169,169,184,184, 
  184,184,185,185,185,185,169,169,169,169,169,169,170,170,185,185,185,185, 
  185,185,185,185,186,170,170,170,170,170,170,170,32,48,64,64,81,81,81,97, 
  97,97,97,97,97,98,98,98,17,33,65,81,81,97,97,97,98,98,98,98,98,98,98,98, 
  225,225,2,98,98,98,98,98,98,98,98,98,99,99,99,99,210,194,178,162,146,114, 
  115,115,115,115,115,115,115,115,115,116,194,195,179,179,163,147,147,147, 
  115,115,116,116,116,116,116,116,195,195,179,179,163,164,148,148,148,148, 
  148,116,116,116,117,117,196,196,180,180,180,164,164,148,149,149,149,149, 
  149,117,117,117,196,196,180,181,181,165,165,165,165,149,149,149,150,150, 
  150,150,197,197,181,181,181,181,165,166,166,166,150,150,150,150,150,150, 
  197,198,182,182,182,182,166,166,166,166,167,151,151,151,151,151,198,198, 
  182,182,182,183,183,167,167,167,167,167,151,151,152,152,199,183,183,183, 
  183,183,183,167,167,168,168,168,168,168,152,152,199,183,183,184,184,184, 
  184,184,168,168,168,168,169,169,169,153,200,184,184,184,184,184,184,185, 
  169,169,169,169,169,169,169,169,200,184,185,185,185,185,185,185,185,169, 
  169,170,170,170,170,170,201,185,185,185,185,186,186,186,186,170,170,170, 
  170,170,170,171,32,49,49,65,65,81,81,81,81,81,98,98,98,98,98,98,33,33,49, 
  65,65,82,82,82,98,98,98,98,98,98,99,99,18,18,34,66,82,82,98,98,98,99,99, 
  99,99,99,99,99,226,226,226,3,99,99,99,99,99,99,99,99,99,100,100,100,211, 
  211,195,179,163,147,115,116,116,116,116,116,116,116,116,116,211,195,196, 
  180,180,164,148,148,148,116,116,117,117,117,117,117,196,196,196,180,180, 
  164,165,149,149,149,149,149,117,117,117,118,197,197,197,181,181,181,165, 
  165,149,150,150,150,150,150,118,118,197,197,197,181,182,182,166,166,166, 
  166,150,150,150,151,151,151,198,198,198,182,182,182,182,166,167,167,167, 
  151,151,151,151,151,198,198,199,183,183,183,183,167,167,167,167,168,152, 
  152,152,152,199,199,199,183,183,183,184,184,168,168,168,168,168,152,152, 
  153,199,200,184,184,184,184,184,184,168,168,169,169,169,169,169,153,200, 
  200,184,184,185,185,185,185,185,169,169,169,169,170,170,170,201,201,185, 
  185,185,185,185,185,186,170,170,170,170,170,170,170,201,201,185,186,186, 
  186,186,186,186,186,170,170,171,171,171,171,33,49,49,49,65,65,65,81,82,82, 
  82,82,82,98,98,98,33,33,50,50,66,66,82,82,82,82,82,99,99,99,99,99,18,34, 
  34,50,66,66,83,83,83,99,99,99,99,99,99,100,18,19,19,35,67,83,83,99,99,99, 
  100,100,100,100,100,100,227,227,227,227,4,100,100,100,100,100,100,100,100, 
  100,101,101,212,212,212,196,180,164,148,116,117,117,117,117,117,117,117, 
  117,212,212,196,197,181,181,165,149,149,149,117,117,118,118,118,118,213, 
  197,197,197,181,181,165,166,150,150,150,150,150,118,118,118,197,198,198, 
  198,182,182,182,166,166,150,151,151,151,151,151,119,198,198,198,198,182, 
  183,183,167,167,167,167,151,151,151,152,152,199,199,199,199,183,183,183, 
  183,167,168,168,168,152,152,152,152,199,199,199,200,184,184,184,184,168, 
  168,168,168,169,153,153,153,200,200,200,200,184,184,184,185,185,169,169, 
  169,169,169,153,153,200,200,201,185,185,185,185,185,185,169,169,170,170, 
  170,170,170,201,201,201,185,185,186,186,186,186,186,170,170,170,170,171, 
  171,202,202,202,186,186,186,186,186,186,187,171,171,171,171,171,171,33,49, 
  49,49,65,66,66,66,82,82,82,82,82,82,83,83,34,34,50,50,50,66,66,66,82,83, 
  83,83,83,83,99,99,34,34,34,51,51,67,67,83,83,83,83,83,100,100,100,100,19, 
  19,35,35,51,67,67,84,84,84,100,100,100,100,100,100,19,19,20,20,36,68,84, 
  84,100,100,100,101,101,101,101,101,228,228,228,228,228,5,101,101,101,101, 
  101,101,101,101,101,102,213,213,213,213,197,181,165,149,117,118,118,118, 
  118,118,118,118,213,213,213,197,198,182,182,166,150,150,150,118,118,119, 
  119,119,214,214,198,198,198,182,182,166,167,151,151,151,151,151,119,119, 
  214,198,199,199,199,183,183,183,167,167,151,152,152,152,152,152,199,199, 
  199,199,199,183,184,184,168,168,168,168,152,152,152,153,199,200,200,200, 
  200,184,184,184,184,168,169,169,169,153,153,153,200,200,200,200,201,185, 
  185,185,185,169,169,169,169,170,154,154,201,201,201,201,201,185,185,185, 
  186,186,170,170,170,170,170,154,201,201,201,202,186,186,186,186,186,186, 
  170,170,171,171,171,171,202,202,202,202,186,186,187,187,187,187,187,171, 
  171,171,171,172,33,49,50,50,50,66,66,66,66,66,82,83,83,83,83,83,34,34,50, 
  50,50,66,67,67,67,83,83,83,83,83,83,84,34,35,35,51,51,51,67,67,67,83,84, 
  84,84,84,84,100,19,35,35,35,52,52,68,68,84,84,84,84,84,101,101,101,20,20, 
  20,36,36,52,68,68,85,85,85,101,101,101,101,101,228,20,20,21,21,37,69,85, 
  85,101,101,101,102,102,102,102,229,229,229,229,229,229,6,102,102,102,102, 
  102,102,102,102,102,229,214,214,214,214,198,182,166,150,118,119,119,119, 
  119,119,119,214,214,214,214,198,199,183,183,167,151,151,151,119,119,120, 
  120,215,215,215,199,199,199,183,183,167,168,152,152,152,152,152,120,215, 
  215,199,200,200,200,184,184,184,168,168,152,153,153,153,153,216,200,200, 
  200,200,200,184,185,185,169,169,169,169,153,153,153,200,200,201,201,201, 
  201,185,185,185,185,169,170,170,170,154,154,201,201,201,201,201,202,186, 
  186,186,186,170,170,170,170,171,155,202,202,202,202,202,202,186,186,186, 
  187,187,171,171,171,171,171,202,202,202,202,203,187,187,187,187,187,187, 
  171,171,172,172,172,34,34,50,50,50,50,66,66,66,67,67,83,83,83,83,83,34,34, 
  50,51,51,51,67,67,67,67,67,83,84,84,84,84,35,35,35,51,51,51,67,68,68,68, 
  84,84,84,84,84,84,35,35,36,36,52,52,52,68,68,68,84,85,85,85,85,85,20,20, 
  36,36,36,53,53,69,69,85,85,85,85,85,102,102,21,21,21,21,37,37,53,69,69,86, 
  86,86,102,102,102,102,229,229,21,21,22,22,38,70,86,86,102,102,102,103,103, 
  103,230,230,230,230,230,230,230,7,103,103,103,103,103,103,103,103,230,230, 
  215,215,215,215,199,183,167,151,119,120,120,120,120,120,215,215,215,215, 
  215,199,200,184,184,168,152,152,152,120,120,121,216,216,216,216,200,200, 
  200,184,184,168,169,153,153,153,153,153,216,216,216,200,201,201,201,185, 
  185,185,169,169,153,154,154,154,217,217,201,201,201,201,201,185,186,186, 
  170,170,170,170,154,154,217,201,201,202,202,202,202,186,186,186,186,170, 
  171,171,171,155,202,202,202,202,202,202,203,187,187,187,187,171,171,171, 
  171,172,202,203,203,203,203,203,203,187,187,187,188,188,172,172,172,172, 
  34,34,50,50,50,50,51,67,67,67,67,67,67,83,83,84,34,35,35,51,51,51,51,67, 
  67,67,68,68,84,84,84,84,35,35,35,51,52,52,52,68,68,68,68,68,84,85,85,85, 
  36,36,36,36,52,52,52,68,69,69,69,85,85,85,85,85,20,36,36,37,37,53,53,53, 
  69,69,69,85,86,86,86,86,21,21,21,37,37,37,54,54,70,70,86,86,86,86,86,103, 
  21,22,22,22,22,38,38,54,70,70,87,87,87,103,103,103,230,230,230,22,22,23, 
  23,39,71,87,87,103,103,103,104,104,231,231,231,231,231,231,231,231,8,104, 
  104,104,104,104,104,104,231,231,231,216,216,216,216,200,184,168,152,120, 
  121,121,121,121,216,216,216,216,216,216,200,201,185,185,169,153,153,153, 
  121,121,216,217,217,217,217,201,201,201,185,185,169,170,154,154,154,154, 
  217,217,217,217,201,202,202,202,186,186,186,170,170,154,155,155,218,218, 
  218,202,202,202,202,202,186,187,187,171,171,171,171,155,218,218,202,202, 
  203,203,203,203,187,187,187,187,171,172,172,172,219,203,203,203,203,203, 
  203,204,188,188,188,188,172,172,172,172,34,34,50,51,51,51,51,51,67,67,67, 
  67,68,68,84,84,35,35,35,51,51,51,51,52,68,68,68,68,68,68,84,84,35,35,36, 
  36,52,52,52,52,68,68,68,69,69,85,85,85,36,36,36,36,52,53,53,53,69,69,69, 
  69,69,85,86,86,21,37,37,37,37,53,53,53,69,70,70,70,86,86,86,86,21,21,37, 
  37,38,38,54,54,54,70,70,70,86,87,87,87,22,22,22,22,38,38,38,55,55,71,71, 
  87,87,87,87,87,22,22,23,23,23,23,39,39,55,71,71,88,88,88,104,104,231,231, 
  231,231,23,23,24,24,40,72,88,88,104,104,104,105,232,232,232,232,232,232, 
  232,232,232,9,105,105,105,105,105,105,232,232,232,232,217,217,217,217,201, 
  185,169,153,121,122,122,122,217,217,217,217,217,217,217,201,202,186,186, 
  170,154,154,154,122,217,217,218,218,218,218,202,202,202,186,186,170,171, 
  155,155,155,218,218,218,218,218,202,203,203,203,187,187,187,171,171,155, 
  156,218,219,219,219,203,203,203,203,203,187,188,188,172,172,172,172,219, 
  219,219,203,203,204,204,204,204,188,188,188,188,172,173,173,35,35,51,51, 
  51,51,51,51,67,67,68,68,68,68,68,68,35,35,35,51,52,52,52,52,52,68,68,68, 
  68,69,69,85,36,36,36,36,52,52,52,52,53,69,69,69,69,69,69,85,36,36,36,37, 
  37,53,53,53,53,69,69,69,70,70,86,86,37,37,37,37,37,53,54,54,54,70,70,70, 
  70,70,86,87,21,22,38,38,38,38,54,54,54,70,71,71,71,87,87,87,22,22,22,38, 
  38,39,39,55,55,55,71,71,71,87,88,88,23,23,23,23,23,39,39,39,56,56,72,72, 
  88,88,88,88,23,23,23,24,24,24,24,40,40,56,72,72,89,89,89,105,232,232,232, 
  232,232,24,24,25,25,41,73,89,89,105,105,105,232,233,233,233,233,233,233, 
  233,233,233,10,106,106,106,106,106,233,233,233,233,233,218,218,218,218,202, 
  186,170,154,122,123,123,218,218,218,218,218,218,218,218,202,203,187,187, 
  171,155,155,155,218,218,218,219,219,219,219,203,203,203,187,187,171,172, 
  156,156,219,219,219,219,219,219,203,204,204,204,188,188,188,172,172,156, 
  219,219,220,220,220,204,204,204,204,204,188,189,189,173,173,173,35,35,51, 
  51,51,51,51,52,52,68,68,68,68,68,68,68,35,36,36,52,52,52,52,52,52,68,68, 
  69,69,69,69,69,36,36,36,36,52,53,53,53,53,53,69,69,69,69,70,70,37,37,37, 
  37,37,53,53,53,53,54,70,70,70,70,70,70,37,37,37,37,38,38,54,54,54,54,70, 
  70,70,71,71,87,22,38,38,38,38,38,54,55,55,55,71,71,71,71,71,87,22,22,23, 
  39,39,39,39,55,55,55,71,72,72,72,88,88,23,23,23,23,39,39,40,40,56,56,56, 
  72,72,72,88,89,24,24,24,24,24,24,40,40,40,57,57,73,73,89,89,89,232,24,24, 
  24,25,25,25,25,41,41,57,73,73,90,90,90,233,233,233,233,233,233,25,25,26, 
  26,42,74,90,90,106,106,233,233,234,234,234,234,234,234,234,234,234,11,107, 
  107,107,107,234,234,234,234,234,234,219,219,219,219,203,187,171,155,123, 
  124,234,219,219,219,219,219,219,219,219,203,204,188,188,172,156,156,219, 
  219,219,219,220,220,220,220,204,204,204,188,188,172,173,157,220,220,220, 
  220,220,220,220,204,205,205,205,189,189,189,173,173,35,35,51,51,52,52,52, 
  52,52,52,68,68,68,69,69,69,36,36,36,52,52,52,52,52,53,53,69,69,69,69,69, 
  69,36,36,37,37,53,53,53,53,53,53,69,69,70,70,70,70,37,37,37,37,37,53,54, 
  54,54,54,54,70,70,70,70,71,37,38,38,38,38,38,54,54,54,54,55,71,71,71,71, 
  71,38,38,38,38,38,39,39,55,55,55,55,71,71,71,72,72,23,23,39,39,39,39,39, 
  55,56,56,56,72,72,72,72,72,23,23,23,24,40,40,40,40,56,56,56,72,73,73,73, 
  89,24,24,24,24,24,40,40,41,41,57,57,57,73,73,73,89,24,25,25,25,25,25,25, 
  41,41,41,58,58,74,74,90,90,233,233,25,25,25,26,26,26,26,42,42,58,74,74,91, 
  91,234,234,234,234,234,234,234,26,26,27,27,43,75,91,91,107,234,234,234,235, 
  235,235,235,235,235,235,235,235,12,108,108,108,235,235,235,235,235,235,235, 
  220,220,220,220,204,188,172,156,124,235,235,220,220,220,220,220,220,220, 
  220,204,205,189,189,173,157,220,220,220,220,220,221,221,221,221,205,205, 
  205,189,189,173,174,35,36,36,52,52,52,52,52,52,52,53,69,69,69,69,69,36,36, 
  36,52,52,53,53,53,53,53,53,69,69,69,70,70,37,37,37,37,53,53,53,53,53,54, 
  54,70,70,70,70,70,37,37,37,38,38,54,54,54,54,54,54,70,70,71,71,71,38,38, 
  38,38,38,38,54,55,55,55,55,55,71,71,71,71,38,38,39,39,39,39,39,55,55,55, 
  55,56,72,72,72,72,23,39,39,39,39,39,40,40,56,56,56,56,72,72,72,73,24,24, 
  24,40,40,40,40,40,56,57,57,57,73,73,73,73,24,24,24,24,25,41,41,41,41,57, 
  57,57,73,74,74,74,25,25,25,25,25,25,41,41,42,42,58,58,58,74,74,74,25,25, 
  26,26,26,26,26,26,42,42,42,59,59,75,75,91,234,234,234,26,26,26,27,27,27, 
  27,43,43,59,75,75,92,234,235,235,235,235,235,235,235,27,27,28,28,44,76,92, 
  92,235,235,235,235,236,236,236,236,236,236,236,236,236,13,109,109,236,236, 
  236,236,236,236,236,236,221,221,221,221,205,189,173,157,236,236,236,221, 
  221,221,221,221,221,221,221,205,206,190,190,174,36,36,36,52,52,52,52,52, 
  53,53,53,53,69,69,69,69,36,36,37,37,53,53,53,53,53,53,53,54,70,70,70,70, 
  37,37,37,37,53,53,54,54,54,54,54,54,70,70,70,71,37,38,38,38,38,54,54,54, 
  54,54,55,55,71,71,71,71,38,38,38,38,39,39,55,55,55,55,55,55,71,71,72,72, 
  39,39,39,39,39,39,39,55,56,56,56,56,56,72,72,72,39,39,39,40,40,40,40,40, 
  56,56,56,56,57,73,73,73,24,24,40,40,40,40,40,41,41,57,57,57,57,73,73,73, 
  24,25,25,25,41,41,41,41,41,57,58,58,58,74,74,74,25,25,25,25,25,26,42,42, 
  42,42,58,58,58,74,75,75,26,26,26,26,26,26,26,42,42,43,43,59,59,59,75,75, 
  26,26,26,27,27,27,27,27,27,43,43,43,60,60,76,76,235,235,235,235,27,27,27, 
  28,28,28,28,44,44,60,76,76,235,235,236,236,236,236,236,236,236,28,28,29,
  29,45,77,93,236,236,236,236,236,237,237,237,237,237,237,237,237,237,14,110, 
  237,237,237,237,237,237,237,237,237,222,222,222,222,206,190,174,36,36,36, 
  52,52,53,53,53,53,53,53,53,69,69,70,70,37,37,37,37,53,53,53,53,53,54,54, 
  54,54,70,70,70,37,37,37,38,38,54,54,54,54,54,54,54,55,71,71,71,38,38,38, 
  38,38,54,54,55,55,55,55,55,55,71,71,71,38,38,39,39,39,39,55,55,55,55,55, 
  56,56,72,72,72,39,39,39,39,39,40,40,56,56,56,56,56,56,72,72,73,40,40,40, 
  40,40,40,40,40,56,57,57,57,57,57,73,73,24,40,40,40,41,41,41,41,41,57,57, 
  57,57,58,74,74,25,25,25,41,41,41,41,41,42,42,58,58,58,58,74,74,25,25,26, 
  26,26,42,42,42,42,42,58,59,59,59,75,75,26,26,26,26,26,26,27,43,43,43,43, 
  59,59,59,75,76,26,27,27,27,27,27,27,27,43,43,44,44,60,60,60,76,27,27,27, 
  27,28,28,28,28,28,28,44,44,44,61,61,77,236,236,236,236,236,28,28,28,29,29, 
  29,29,45,45,61,77,236,236,236,237,237,237,237,237,237,237,29,29,30,30,46, 
  78,237,237,237,237,237,237,238,238,238,238,238,238,238,238,238,15);

procedure param(pr:byte);
begin
par:=paramstr(pr);
 case par[2] of
  'h','H':hig:=238;
  'o','O':opt:=1;
  'm','M':mul:=1;
  'a','A':col:=0;
  'r','R':col:=1;
 else
  writeln('Illegal option: ',chr(39),par,chr(39)); halt;
 end;
end;


function rgb(r,g,b:byte):byte;
var l,d,c,ma,mi,r2,g2,b2:integer;
    h,lu1,lu2,lu3:real;
begin
h:=0; c:=0; l:=0;

if (r=g) and (g=b) then begin l:=r; c:=0; rgb:=l; exit; end;
if (r>=g) and (r>=b) then ma:=r;
if (r<=g) and (r<=b) then mi:=r;
if (g>=r) and (g>=b) then ma:=g;
if (g<=r) and (g<=b) then mi:=g;
if (b>=g) and (b>=r) then ma:=b;
if (b<=g) and (b<=r) then mi:=b;

d:=ma-mi;
if d=1 then begin l:=mi; c:=0; rgb:=l; exit; end;

r2:=r*100; g2:=g*100; b2:=b*100;
 if r=ma then h:=(g2-b2)/d
   else
 if g=ma then h:=200+((b2-r2)/d)
   else
 if b=ma then h:=400+((r2-g2)/d);

h:=h*0.6; if h<0 then h:=h+360;

lu1:=r*0.297;
lu2:=g*0.586;
lu3:=b*0.109;

l:=trunc(lu1+lu2+lu3);

case trunc(h) of
 21..31: c:=15;
 32..43: c:=14;
 44..63: c:=13;
 64..84: c:=12;
 85..147: c:=11;
 148..171: c:=10;
 172..191: c:=9;
 192..209: c:=8;
 210..261: c:=7;
 262..276: c:=6;
 277..297: c:=5;
 298..318: c:=4;
 319..334: c:=3;
 335..360: c:=2;
 0..14: c:=2;
 15..20: c:=1;
end;
rgb:=c*16+l;
end;

function lowhi(w:word):string;
begin
lowhi:=chr(w div 256)+chr(w mod 256);
end;

procedure clrline;
begin
fillchar(line,sizeof(line),0);
fillchar(scan,sizeof(scan),0);
if tst=0 then begin gotoxy(1,wherey); write(pcxhead.ymax-y+1,'  '); end;
end;

procedure bitplan(b1,b2,b3,b4:byte);
var mask,b0:byte;
begin

for l:=0 to 7 do begin
  mask:=tmsk[l]; b0:=0;
 if (b1 and mask)>0 then b0:=b0 or 1;
 if (b2 and mask)>0 then b0:=b0 or 2;
 if (b3 and mask)>0 then b0:=b0 or 4;
 if (b4 and mask)>0 then b0:=b0 or 8;

 tpixle[l]:=b0;
end;
end;

procedure pixel(col:byte);
var ofs,px,p:integer;
    b1,b2,b3,b4:byte;
begin;

inc(bajt);

line[x]:=col; x:=x+1;
 if bajt>(pcxhead.bytesperline*pcxhead.nplanes)-1 then begin
  if gfxmode<5 then begin
ofs:=pcxhead.bytesperline; px:=0; p:=0;
for t:=0 to pcxhead.bytesperline-1 do begin
 bitplan(line[t],line[ofs+t],line[2*ofs+t],line[3*ofs+t]);
if y<=pcxhead.ymax then
 for l:=0 to 7 do begin scan[posx+l+px]:=tpixle[l]; tpal[tpixle[l]]:=1; end;
 inc(px,8);
end;
if tst=0 then begin
    px:=hip;
     for t:=0 to 39 do begin
      b1:=scan[px]+(hip div 2)-pal; inc(px,xstep);
      b2:=scan[px]+(hip div 2)-pal; inc(px,xstep);
      tmp:=(b1 shl 4) or b2;
      cash[wsp]:=tmp; inc(wsp);
     end;
end;
  end;
 bajt:=0; y:=y+1; x:=0; clrline; end;
end;

procedure pcx(name:string);
begin

fillchar(tpal,sizeof(tpal),0);
assign(g,name);reset(g);
for q:=0 to 127 do begin
read(g,w);buf[q]:=w; end;

move(buf,pcxhead,128);
if pcxhead.flag<>$a then begin writeln('PCX unknown !'); halt; end;

if pcxhead.ymax>238 then pcxhead.ymax:=238;
gfxmode:=pcxhead.nplanes*pcxhead.bitsperpixel;
if gfxmode=1 then gfxmode:=4;

posx:=((scren-1)-pcxhead.xmax) div 2;

  x:=0; y:=0;
 while y<>pcxhead.ymax+1 do begin
   read(g,w); r:=1;
    if (w and $c0)=$c0 then begin r:=w and $3f; read(g,w); end;
      for q:=1 to r do pixel(w);
 end;

{* liczy kolory *}
col_:=0; for i:=0 to 255 do if tpal[i]<>0 then inc(col_);
pal:=0; if col_>8 then pal:=1;

{writeln(col_,',');}

port[$3c8]:=0;
 if gfxmode<5 then begin
{   for q:=1 to 48 do port[$3c9]:=pcxhead.colormap[q] shr 2;}
end;

close(g);
end;

begin
tmsk[0]:=$80;
tmsk[1]:=$40;
tmsk[2]:=$20;
tmsk[3]:=$10;
tmsk[4]:=$8;
tmsk[5]:=$4;
tmsk[6]:=$2;
tmsk[7]:=$1;

writeln; writeln('RIP Converter V3.1 --- (c) 1998 Madman/Madteam'); writeln;
mul:=0; wsp:=0; bajt:=0; x:=0; y:=0;

if paramcount<1 then begin
 writeln('Usage: RIPCNV [-hmar] file');
 writeln('       -h: height 238');
 writeln('       -m: multi color RIP');
{ writeln('       -o: optymalization pallete');}
 writeln('       -a: APAC color table');
 writeln('       -r: ROCKY color table (default)'); halt;
end;
 szer:=80; xstep:=4; scren:=320; col:=1; pal:=0; mul:=0; opt:=0; hig:=200;
 tst:=0;
 x:=0; if paramcount>1 then for x:=1 to paramcount-1 do param(x);
 plik:=paramstr(x+1);

 if pos('.',plik)>0 then plik:=copy(plik,1,pos('.',plik)-1);
 write('File:',plik); write('  Info:'); readln(nazwa);

 clrline;
if mul=0 then begin hip:=2; tst:=1; pcx(plik+'.9'); tst:=0; pcx(plik+'.9'); end;

if mul=1 then begin               { 238 plikow }
 for err:=0 to (hig div maxy)-1 do begin
   str(err,nam); t:=length(nam);
   nam:='0000'+nam; delete(nam,1,t); nam:=plik+nam+'.pcx';
   hip:=2; tst:=1; pcx(nam); tst:=0; pcx(nam);
    l:=1; for i:=0 to 7+pal do begin
           x:=pcxhead.colormap[l] shr 4;
           y:=pcxhead.colormap[l+1] shr 4;
           z:=pcxhead.colormap[l+2] shr 4;
           if col=0 then q:=apc[x*256+y*16+z] else q:=rgb(x,y,z);
            btmp[i]:=q; inc(l,3);
          end;
 for j:=0 to maxy-1 do for i:=0 to 7 do bpal[err*(8*maxy)+j*8+i]:=btmp[i+pal];
 end;

{assign(f,plik+'.pal'); rewrite(f,1);
for i:=0 to (hig div maxy)-1 do blockwrite(f,bpal[i*8*maxy],8,err); close(f);}
pcxhead.ymax:=hig-1;
end;

h1:='RIP1.0  '+lowhi(0);                { RIP header }
h2:=lowhi(szer)+lowhi(pcxhead.ymax+1)+lowhi(length(nazwa))+'T:'+nazwa+chr(9)+'CM:';
if pal=0 then h2:=h2+chr(0);

l:=1; for i:=0 to 7+pal do begin        { paleta kolorow }
  x:=pcxhead.colormap[l] shr 4;
  y:=pcxhead.colormap[l+1] shr 4;
  z:=pcxhead.colormap[l+2] shr 4;
  if col=0 then q:=apc[x*256+y*16+z] else q:=rgb(x,y,z);
  h2:=h2+chr(q); inc(l,3);
 end;

if mul=1 then begin h1[8]:='0'; h1[4]:='2'; end;   { znacznik MULTI i v2.0 }

tst:=0; hip:=0; pal:=0; pcx(plik+'.16');
h1:=h1+lowhi(length(h1)+length(h2)+2)+h2;

if opt=1 then begin
x:=0; y:=(pcxhead.ymax+1)*40;
 for i:=0 to pcxhead.ymax do begin
  for j:=0 to 39 do begin
   tmp:=cash[x+j+i*40]; tmp_:=cash[y+j+i*40];
   b1:=tmp shr 4; b1_:=tmp and $f; b2:=tmp_ shr 4;
   col:=bpal[b1] and $f; col_:=bpal[b1_] and $f;
   w:=((((col+col_) div 2)+b2) and $fe) and $f;

{   writeln(w,' - ',b2);}

   tmp:=cash[x+j+i*40+1];
   b1:=tmp shr 4; b2:=tmp_ and $f;
   col:=bpal[b1] and $f;
   w_:=((((col+col_) div 2)+b2) and $fe) and $f;

   cash[y+j+i*40]:=(w shl 4) or w_;
  end;
 end;
end;

assign(f,plik+'.rip'); rewrite(f,1);
blockwrite(f,h1[1],length(h1),i);
blockwrite(f,cash,wsp,i);
if mul=1 then
 for i:=0 to (hig div maxy)-1 do blockwrite(f,bpal[i*8*maxy],8,err);
close(f);
end.