//
//  Copyright (C) 1999 by
//  id Software, Chi Hoang, Lee Killough, Jim Flynn, Rand Phares, Ty Halderman
//
//  This program is free software; you can redistribute it and/or
//  modify it under the terms of the GNU General Public License
//  as published by the Free Software Foundation; either version 2
//  of the License, or (at your option) any later version.
//
//  This program is distributed in the hope that it will be useful,
//  but WITHOUT ANY WARRANTY; without even the implied warranty of
//  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
//  GNU General Public License for more details.
//
//
// DESCRIPTION:
//  Gamma correction LUT stuff.
//  Color range translation support
//  Functions to draw patches (by post) directly to screen.
//  Functions to blit a block to the screen.
//
//-----------------------------------------------------------------------------

#include "doomstat.h"
#include "r_main.h"
#include "m_bbox.h"
#include "w_wad.h"   /* needed for color translation lump lookup */
#include "v_trans.h"
#include "v_video.h"
#include "i_video.h"
#include "m_argv.h"
#include "m_swap.h"
#include "m_misc2.h"

// Each screen is [SCREENWIDTH*SCREENHEIGHT];
byte *screens[5];
int  dirtybox[4];

//jff 2/18/98 palette color ranges for translation
//jff 4/24/98 now pointers set to predefined lumps to allow overloading

char *cr_brick;
char *cr_tan;
char *cr_gray;
char *cr_green;
char *cr_brown;
char *cr_gold;
char *cr_red;
char *cr_blue;
char *cr_blue2;
char *cr_orange;
char *cr_yellow;
char *cr_black;
char *cr_purple;
char *cr_white;
// [FG] dark/shaded color translation table
char *cr_dark;

//jff 4/24/98 initialize this at runtime
char *colrngs[CR_LIMIT] = {0};
char *red2col[CR_LIMIT] = {0};

// Now where did these came from?
byte gammatable[5][256] =
{
  {1,2,3,4,5,6,7,8,9,10,11,12,13,14,15,16,
   17,18,19,20,21,22,23,24,25,26,27,28,29,30,31,32,
   33,34,35,36,37,38,39,40,41,42,43,44,45,46,47,48,
   49,50,51,52,53,54,55,56,57,58,59,60,61,62,63,64,
   65,66,67,68,69,70,71,72,73,74,75,76,77,78,79,80,
   81,82,83,84,85,86,87,88,89,90,91,92,93,94,95,96,
   97,98,99,100,101,102,103,104,105,106,107,108,109,110,111,112,
   113,114,115,116,117,118,119,120,121,122,123,124,125,126,127,128,
   128,129,130,131,132,133,134,135,136,137,138,139,140,141,142,143,
   144,145,146,147,148,149,150,151,152,153,154,155,156,157,158,159,
   160,161,162,163,164,165,166,167,168,169,170,171,172,173,174,175,
   176,177,178,179,180,181,182,183,184,185,186,187,188,189,190,191,
   192,193,194,195,196,197,198,199,200,201,202,203,204,205,206,207,
   208,209,210,211,212,213,214,215,216,217,218,219,220,221,222,223,
   224,225,226,227,228,229,230,231,232,233,234,235,236,237,238,239,
   240,241,242,243,244,245,246,247,248,249,250,251,252,253,254,255},

  {2,4,5,7,8,10,11,12,14,15,16,18,19,20,21,23,24,25,26,27,29,30,31,
   32,33,34,36,37,38,39,40,41,42,44,45,46,47,48,49,50,51,52,54,55,
   56,57,58,59,60,61,62,63,64,65,66,67,69,70,71,72,73,74,75,76,77,
   78,79,80,81,82,83,84,85,86,87,88,89,90,91,92,93,94,95,96,97,98,
   99,100,101,102,103,104,105,106,107,108,109,110,111,112,113,114,
   115,116,117,118,119,120,121,122,123,124,125,126,127,128,129,129,
   130,131,132,133,134,135,136,137,138,139,140,141,142,143,144,145,
   146,147,148,148,149,150,151,152,153,154,155,156,157,158,159,160,
   161,162,163,163,164,165,166,167,168,169,170,171,172,173,174,175,
   175,176,177,178,179,180,181,182,183,184,185,186,186,187,188,189,
   190,191,192,193,194,195,196,196,197,198,199,200,201,202,203,204,
   205,205,206,207,208,209,210,211,212,213,214,214,215,216,217,218,
   219,220,221,222,222,223,224,225,226,227,228,229,230,230,231,232,
   233,234,235,236,237,237,238,239,240,241,242,243,244,245,245,246,
   247,248,249,250,251,252,252,253,254,255},

  {4,7,9,11,13,15,17,19,21,22,24,26,27,29,30,32,33,35,36,38,39,40,42,
   43,45,46,47,48,50,51,52,54,55,56,57,59,60,61,62,63,65,66,67,68,69,
   70,72,73,74,75,76,77,78,79,80,82,83,84,85,86,87,88,89,90,91,92,93,
   94,95,96,97,98,100,101,102,103,104,105,106,107,108,109,110,111,112,
   113,114,114,115,116,117,118,119,120,121,122,123,124,125,126,127,128,
   129,130,131,132,133,133,134,135,136,137,138,139,140,141,142,143,144,
   144,145,146,147,148,149,150,151,152,153,153,154,155,156,157,158,159,
   160,160,161,162,163,164,165,166,166,167,168,169,170,171,172,172,173,
   174,175,176,177,178,178,179,180,181,182,183,183,184,185,186,187,188,
   188,189,190,191,192,193,193,194,195,196,197,197,198,199,200,201,201,
   202,203,204,205,206,206,207,208,209,210,210,211,212,213,213,214,215,
   216,217,217,218,219,220,221,221,222,223,224,224,225,226,227,228,228,
   229,230,231,231,232,233,234,235,235,236,237,238,238,239,240,241,241,
   242,243,244,244,245,246,247,247,248,249,250,251,251,252,253,254,254,
   255},

  {8,12,16,19,22,24,27,29,31,34,36,38,40,41,43,45,47,49,50,52,53,55,
   57,58,60,61,63,64,65,67,68,70,71,72,74,75,76,77,79,80,81,82,84,85,
   86,87,88,90,91,92,93,94,95,96,98,99,100,101,102,103,104,105,106,107,
   108,109,110,111,112,113,114,115,116,117,118,119,120,121,122,123,124,
   125,126,127,128,129,130,131,132,133,134,135,135,136,137,138,139,140,
   141,142,143,143,144,145,146,147,148,149,150,150,151,152,153,154,155,
   155,156,157,158,159,160,160,161,162,163,164,165,165,166,167,168,169,
   169,170,171,172,173,173,174,175,176,176,177,178,179,180,180,181,182,
   183,183,184,185,186,186,187,188,189,189,190,191,192,192,193,194,195,
   195,196,197,197,198,199,200,200,201,202,202,203,204,205,205,206,207,
   207,208,209,210,210,211,212,212,213,214,214,215,216,216,217,218,219,
   219,220,221,221,222,223,223,224,225,225,226,227,227,228,229,229,230,
   231,231,232,233,233,234,235,235,236,237,237,238,238,239,240,240,241,
   242,242,243,244,244,245,246,246,247,247,248,249,249,250,251,251,252,
   253,253,254,254,255},

  {16,23,28,32,36,39,42,45,48,50,53,55,57,60,62,64,66,68,69,71,73,75,76,
   78,80,81,83,84,86,87,89,90,92,93,94,96,97,98,100,101,102,103,105,106,
   107,108,109,110,112,113,114,115,116,117,118,119,120,121,122,123,124,
   125,126,128,128,129,130,131,132,133,134,135,136,137,138,139,140,141,
   142,143,143,144,145,146,147,148,149,150,150,151,152,153,154,155,155,
   156,157,158,159,159,160,161,162,163,163,164,165,166,166,167,168,169,
   169,170,171,172,172,173,174,175,175,176,177,177,178,179,180,180,181,
   182,182,183,184,184,185,186,187,187,188,189,189,190,191,191,192,193,
   193,194,195,195,196,196,197,198,198,199,200,200,201,202,202,203,203,
   204,205,205,206,207,207,208,208,209,210,210,211,211,212,213,213,214,
   214,215,216,216,217,217,218,219,219,220,220,221,221,222,223,223,224,
   224,225,225,226,227,227,228,228,229,229,230,230,231,232,232,233,233,
   234,234,235,235,236,236,237,237,238,239,239,240,240,241,241,242,242,
   243,243,244,244,245,245,246,246,247,247,248,248,249,249,250,250,251,
   251,252,252,253,254,254,255,255}
};

//
// V_InitColorTranslation
//
// Loads the color translation tables from predefined lumps at game start
// No return value
//
// Used for translating text colors from the red palette range
// to other colors. The first nine entries can be used to dynamically
// switch the output of text color thru the HUlib_drawText routine
// by embedding ESCn in the text to obtain color n. Symbols for n are
// provided in v_video.h.
//

typedef struct {
  const char *name;
  char **map1, **map2, **map_orig;
} crdef_t;

// killough 5/2/98: table-driven approach
static const crdef_t crdefs[] = {
  {"CRBRICK",  &cr_brick,   &colrngs[CR_BRICK ], &red2col[CR_BRICK ]},
  {"CRTAN",    &cr_tan,     &colrngs[CR_TAN   ], &red2col[CR_TAN   ]},
  {"CRGRAY",   &cr_gray,    &colrngs[CR_GRAY  ], &red2col[CR_GRAY  ]},
  {"CRGREEN",  &cr_green,   &colrngs[CR_GREEN ], &red2col[CR_GREEN ]},
  {"CRBROWN",  &cr_brown,   &colrngs[CR_BROWN ], &red2col[CR_BROWN ]},
  {"CRGOLD",   &cr_gold,    &colrngs[CR_GOLD  ], &red2col[CR_GOLD  ]},
  {"CRRED",    &cr_red,     &colrngs[CR_RED   ], &red2col[CR_RED   ]},
  {"CRBLUE",   &cr_blue,    &colrngs[CR_BLUE1 ], &red2col[CR_BLUE1 ]},
  {"CRORANGE", &cr_orange,  &colrngs[CR_ORANGE], &red2col[CR_ORANGE]},
  {"CRYELLOW", &cr_yellow,  &colrngs[CR_YELLOW], &red2col[CR_YELLOW]},
  {"CRBLUE2",  &cr_blue2,   &colrngs[CR_BLUE2 ], &red2col[CR_BLUE2 ]},
  {"CRBLACK",  &cr_black,   &colrngs[CR_BLACK ], &red2col[CR_BLACK ]},
  {"CRPURPLE", &cr_purple,  &colrngs[CR_PURPLE], &red2col[CR_PURPLE]},
  {"CRWHITE",  &cr_white,   &colrngs[CR_WHITE ], &red2col[CR_WHITE ]},
  {NULL}
};

// [FG] translate between blood color value as per EE spec
//      and actual color translation table index

static const int bloodcolor[] = {
  CR_RED,    // 0 - Red (normal)
  CR_GRAY,   // 1 - Grey
  CR_GREEN,  // 2 - Green
  CR_BLUE2,  // 3 - Blue
  CR_YELLOW, // 4 - Yellow
  CR_BLACK,  // 5 - Black
  CR_PURPLE, // 6 - Purple
  CR_WHITE,  // 7 - White
  CR_ORANGE, // 8 - Orange
};

int V_BloodColor(int blood)
{
  return bloodcolor[blood];
}

// killough 5/2/98: tiny engine driven by table above
void V_InitColorTranslation(void)
{
  register const crdef_t *p;

  int playpal_lump = W_GetNumForName("PLAYPAL");
  byte *playpal = W_CacheLumpNum(playpal_lump, PU_STATIC);
  boolean iwad_playpal = W_IsIWADLump(playpal_lump);

  int force_rebuild = M_CheckParm("-tranmap");

  // [crispy] preserve gray drop shadow in IWAD status bar numbers
  boolean keepgray = W_IsIWADLump(W_GetNumForName("sttnum0"));

  for (p = crdefs; p->name; p++)
  {
    int i, lumpnum = W_GetNumForName(p->name);

    *p->map_orig = W_CacheLumpNum(lumpnum, PU_STATIC);

    // [FG] color translation table provided by PWAD
    if (W_IsWADLump(lumpnum) && !force_rebuild)
    {
      *p->map1 = *p->map2 = *p->map_orig;
      continue;
    }

    // [FG] allocate new color translation table
    *p->map2 = malloc(256);

    // [FG] translate all colors to target color
    for (i = 0; i < 256; i++)
    {
      (*p->map2)[i] = V_Colorize(playpal, p - crdefs, (byte) i);
    }

    // [FG] override with original color translations
    if (iwad_playpal && !force_rebuild)
    {
      for (i = 0; i < 256; i++)
      {
        if (((*p->map_orig)[i] != (char) i) || (keepgray && i == 109))
        {
          (*p->map2)[i] = (*p->map_orig)[i];
        }
      }
    }

    *p->map1 = *p->map2;
  }
}

void WriteGeneratedLumpWad(const char *filename)
{
    int i;
    const size_t num_lumps = arrlen(crdefs);
    lumpinfo_t *lumps = calloc(num_lumps, sizeof(*lumps));

    for (i = 0; i < num_lumps - 1; i++) // last entry is dummy
    {
        M_CopyLumpName(lumps[i].name, crdefs[i].name);
        lumps[i].data = *crdefs[i].map2;
        lumps[i].size = 256;
    }

    M_CopyLumpName(lumps[i].name, "TRANMAP");
    lumps[i].data = main_tranmap;
    lumps[i].size = 256 * 256;

    WriteLumpWad(filename, lumps, num_lumps);

    free(lumps);
}

//
// V_CopyRect
//
// Copies a source rectangle in a screen buffer to a destination
// rectangle in another screen buffer. Source origin in srcx,srcy,
// destination origin in destx,desty, common size in width and height.
// Source buffer specfified by srcscrn, destination buffer by destscrn.
//
// Marks the destination rectangle on the screen dirty.
//
// No return value.

void V_CopyRect(int srcx, int srcy, int srcscrn, int width,
		int height, int destx, int desty, int destscrn )
{
  byte *src;
  byte *dest;

#ifdef RANGECHECK
  if (srcx<0
      ||srcx+width >SCREENWIDTH
      || srcy<0
      || srcy+height>SCREENHEIGHT
      ||destx<0||destx/*+width*/>SCREENWIDTH
      || desty<0
      || desty/*+height*/>SCREENHEIGHT
      || (unsigned)srcscrn>4
      || (unsigned)destscrn>4)
    I_Error ("Bad V_CopyRect");
#endif

  // [FG] prevent framebuffer overflows
  if (destx + width > SCREENWIDTH)
    width = SCREENWIDTH - destx;
  if (desty + height > SCREENHEIGHT)
    height = SCREENHEIGHT - desty;

  if (hires)   // killough 11/98: hires support
    {
      width<<=1;
      height<<=1;
      src = screens[srcscrn]+SCREENWIDTH*4*srcy+srcx*2;
      dest = screens[destscrn]+SCREENWIDTH*4*desty+destx*2;

      for ( ; height>0 ; height--)
	{
	  memcpy (dest, src, width);
	  src += SCREENWIDTH*2;
	  dest += SCREENWIDTH*2;
	}
    }
  else
    {
      src = screens[srcscrn]+SCREENWIDTH*srcy+srcx;
      dest = screens[destscrn]+SCREENWIDTH*desty+destx;

      for ( ; height>0 ; height--)
	{
	  memcpy (dest, src, width);
	  src += SCREENWIDTH;
	  dest += SCREENWIDTH;
	}
    }
}

//
// V_DrawPatch
//
// Masks a column based masked pic to the screen.
//
// The patch is drawn at x,y in the buffer selected by scrn
// No return value
//
// V_DrawPatchFlipped
//
// Masks a column based masked pic to the screen.
// Flips horizontally, e.g. to mirror face.
//
// Patch is drawn at x,y in screenbuffer scrn.
// No return value
//
// killough 11/98: Consolidated V_DrawPatch and V_DrawPatchFlipped into one
//

void V_DrawPatchGeneral(int x, int y, int scrn, patch_t *patch,
			boolean flipped)
{
  int  w = SHORT(patch->width), col = w-1, colstop = -1, colstep = -1;
  
  if (!flipped)
    col = 0, colstop = w, colstep = 1;

  y -= SHORT(patch->topoffset);
  x -= SHORT(patch->leftoffset);

  x += WIDESCREENDELTA; // [crispy] horizontal widescreen offset

#ifdef RANGECHECK_NOTHANKS
  if (x<0
      ||x+SHORT(patch->width) >SCREENWIDTH
      || y<0
      || y+SHORT(patch->height)>SCREENHEIGHT
      || (unsigned)scrn>4)
      return;      // killough 1/19/98: commented out printfs
#endif

  if (hires)       // killough 11/98: hires support (well, sorta :)
    {
      byte *desttop = screens[scrn]+y*SCREENWIDTH*4+x*2;

      for ( ; col != colstop ; col += colstep, desttop+=2, x++)
	{
	  const column_t *column = 
	    (const column_t *)((byte *)patch + LONG(patch->columnofs[col]));

	  // [FG] prevent framebuffer overflows
	  {
	    // [FG] too far left
	    if (x < 0)
	      continue;
	    // [FG] too far right, too wide
	    if (x >= SCREENWIDTH)
	      break;
	  }

	  // step through the posts in a column
	  while (column->topdelta != 0xff)
	    {
	      // killough 2/21/98: Unrolled and performance-tuned

	      register const byte *source = (byte *) column + 3;
	      register byte *dest = desttop + column->topdelta*SCREENWIDTH*4;
	      register int count = column->length;

	      // [FG] prevent framebuffer overflows
	      {
		int topy = y + column->topdelta;
		// [FG] too high
		while (topy < 0 && count)
		  count--, source++, dest += SCREENWIDTH*4, topy++;
		// [FG] too low, too tall
		while (topy + count > SCREENHEIGHT && count)
		  count--;
	      }

	      if ((count-=4)>=0)
		do
		  {
		    register byte s0,s1;
		    s0 = source[0];
		    s1 = source[1];
		    dest[0] = s0;
		    dest[SCREENWIDTH*4] = s1;
		    dest[SCREENWIDTH*2] = s0;
		    dest[SCREENWIDTH*6] = s1;
		    dest[1] = s0;
		    dest[SCREENWIDTH*4+1] = s1;
		    dest[SCREENWIDTH*2+1] = s0;
		    dest[SCREENWIDTH*6+1] = s1;
		    dest += SCREENWIDTH*8;
		    s0 = source[2];
		    s1 = source[3];
		    source += 4;
		    dest[0] = s0;
		    dest[SCREENWIDTH*4] = s1;
		    dest[1] = s0;
		    dest[SCREENWIDTH*4+1] = s1;
		    dest[SCREENWIDTH*2] = s0;
		    dest[SCREENWIDTH*6] = s1;
		    dest[SCREENWIDTH*2+1] = s0;
		    dest[SCREENWIDTH*6+1] = s1;
		    dest += SCREENWIDTH*8;
		  }
		while ((count-=4)>=0);
	      if (count+=4)
		do
		  {
		    dest[0] = dest[SCREENWIDTH*2] = dest[1] =
		      dest[SCREENWIDTH*2+1] = *source++;
		    dest += SCREENWIDTH*4;
		  }
		while (--count);
//	      column = (column_t *)(source+1); //killough 2/21/98 even faster
	      // [FG] back to Vanilla code, we may not run through the entire column
	      column = (column_t *)((byte *)column + column->length + 4);
	    }
	}
    }
  else
    {
      byte *desttop = screens[scrn]+y*SCREENWIDTH+x;

      for ( ; col != colstop ; col += colstep, desttop++, x++)
	{
	  const column_t *column = 
	    (const column_t *)((byte *)patch + LONG(patch->columnofs[col]));

	  // [FG] prevent framebuffer overflows
	  {
	    // [FG] too far left
	    if (x < 0)
	      continue;
	    // [FG] too far right, too wide
	    if (x >= SCREENWIDTH)
	      break;
	  }

	  // step through the posts in a column
	  while (column->topdelta != 0xff)
	    {
	      // killough 2/21/98: Unrolled and performance-tuned

	      register const byte *source = (byte *) column + 3;
	      register byte *dest = desttop + column->topdelta*SCREENWIDTH;
	      register int count = column->length;

	      // [FG] prevent framebuffer overflows
	      {
		int topy = y + column->topdelta;
		// [FG] too high
		while (topy < 0 && count)
		  count--, source++, dest += SCREENWIDTH, topy++;
		// [FG] too low, too tall
		while (topy + count > SCREENHEIGHT && count)
		  count--;
	      }

	      if ((count-=4)>=0)
		do
		  {
		    register byte s0,s1;
		    s0 = source[0];
		    s1 = source[1];
		    dest[0] = s0;
		    dest[SCREENWIDTH] = s1;
		    dest += SCREENWIDTH*2;
		    s0 = source[2];
		    s1 = source[3];
		    source += 4;
		    dest[0] = s0;
		    dest[SCREENWIDTH] = s1;
		    dest += SCREENWIDTH*2;
		  }
		while ((count-=4)>=0);
	      if (count+=4)
		do
		  {
		    *dest = *source++;
		    dest += SCREENWIDTH;
		  }
		while (--count);
//	      column = (column_t *)(source+1); //killough 2/21/98 even faster
	      // [FG] back to Vanilla code, we may not run through the entire column
	      column = (column_t *)((byte *)column + column->length + 4);
	    }
	}
    }
}

//
// V_DrawPatchTranslated
//
// Masks a column based masked pic to the screen.
// Also translates colors from one palette range to another using
// the color translation lumps loaded in V_InitColorTranslation
//
// The patch is drawn at x,y in the screen buffer scrn. Color translation
// is performed thru the table pointed to by outr.
//
// jff 1/15/98 new routine to translate patch colors
//

void V_DrawPatchTranslated(int x, int y, int scrn, patch_t *patch, char *outr)
{
  int col, w;

  //jff 2/18/98 if translation not needed, just use the old routine
  if (outr == NULL)
    {
      V_DrawPatch(x,y,scrn,patch);
      return;                            // killough 2/21/98: add return
    }

  y -= SHORT(patch->topoffset);
  x -= SHORT(patch->leftoffset);

  x += WIDESCREENDELTA; // [crispy] horizontal widescreen offset

#ifdef RANGECHECK_NOTHANKS
  if (x<0
      ||x+SHORT(patch->width) >SCREENWIDTH
      || y<0
      || y+SHORT(patch->height)>SCREENHEIGHT
      || (unsigned)scrn>4)
    return;    // killough 1/19/98: commented out printfs
#endif

  col = 0;
  w = SHORT(patch->width);

  if (hires)       // killough 11/98: hires support (well, sorta :)
    {
      byte *desttop = screens[scrn]+y*SCREENWIDTH*4+x*2;

      for ( ; col<w ; col++, desttop+=2)
	{
	  const column_t *column =
	    (const column_t *)((byte *)patch + LONG(patch->columnofs[col]));

	  // [FG] prevent framebuffer overflows
	  {
	    // [FG] too far left
	    if (x < 0)
	      continue;
	    // [FG] too far right, too wide
	    if (x >= SCREENWIDTH)
	      break;
	  }

	  // step through the posts in a column
	  while (column->topdelta != 0xff)
	    {
	      // killough 2/21/98: Unrolled and performance-tuned

	      register const byte *source = (byte *) column + 3;
	      register byte *dest = desttop + column->topdelta*SCREENWIDTH*4;
	      register int count = column->length;

	      // [FG] prevent framebuffer overflows
	      {
		int topy = y + column->topdelta;
		// [FG] too high
		while (topy < 0 && count)
		  count--, source++, dest += SCREENWIDTH*4, topy++;
		// [FG] too low, too tall
		while (topy + count > SCREENHEIGHT && count)
		  count--;
	      }

	      if ((count-=4)>=0)
		do
		  {
		    register byte s0,s1;
		    s0 = source[0];
		    s1 = source[1];
		    s0 = outr[s0];
		    s1 = outr[s1];
		    dest[0] = s0;
		    dest[SCREENWIDTH*4] = s1;
		    dest[SCREENWIDTH*2] = s0;
		    dest[SCREENWIDTH*6] = s1;
		    dest[1] = s0;
		    dest[SCREENWIDTH*4+1] = s1;
		    dest[SCREENWIDTH*2+1] = s0;
		    dest[SCREENWIDTH*6+1] = s1;
		    dest += SCREENWIDTH*8;
		    s0 = source[2];
		    s1 = source[3];
		    s0 = outr[s0];
		    s1 = outr[s1];
		    source += 4;
		    dest[0] = s0;
		    dest[SCREENWIDTH*4] = s1;
		    dest[SCREENWIDTH*2] = s0;
		    dest[SCREENWIDTH*6] = s1;
		    dest[1] = s0;
		    dest[SCREENWIDTH*4+1] = s1;
		    dest[SCREENWIDTH*2+1] = s0;
		    dest[SCREENWIDTH*6+1] = s1;
		    dest += SCREENWIDTH*8;
		  }
		while ((count-=4)>=0);
	      if (count+=4)
		do
		  {
		    dest[0] = dest[SCREENWIDTH*2] = dest[1] =
		      dest[SCREENWIDTH*2+1] = outr[*source++];
		    dest += SCREENWIDTH*4;
		  }
		while (--count);
//	      column = (column_t *)(source+1);
	      // [FG] back to Vanilla code, we may not run through the entire column
	      column = (column_t *)((byte *)column + column->length + 4);
	    }
	}
    }
  else
    {
      byte *desttop = screens[scrn]+y*SCREENWIDTH+x;

      for ( ; col<w ; col++, desttop++)
	{
	  const column_t *column =
	    (const column_t *)((byte *)patch + LONG(patch->columnofs[col]));

	  // [FG] prevent framebuffer overflows
	  {
	    // [FG] too far left
	    if (x < 0)
	      continue;
	    // [FG] too far right, too wide
	    if (x >= SCREENWIDTH)
	      break;
	  }

	  // step through the posts in a column
	  while (column->topdelta != 0xff)
	    {
	      // killough 2/21/98: Unrolled and performance-tuned

	      register const byte *source = (byte *) column + 3;
	      register byte *dest = desttop + column->topdelta*SCREENWIDTH;
	      register int count = column->length;

	      // [FG] prevent framebuffer overflows
	      {
		int topy = y + column->topdelta;
		// [FG] too high
		while (topy < 0 && count)
		  count--, source++, dest += SCREENWIDTH, topy++;
		// [FG] too low, too tall
		while (topy + count > SCREENHEIGHT && count)
		  count--;
	      }

	      if ((count-=4)>=0)
		do
		  {
		    register byte s0,s1;
		    s0 = source[0];
		    s1 = source[1];

		    //jff 2/18/98 apply red->range color translation
		    //2/18/98 don't brightness map for speed

		    s0 = outr[s0];
		    s1 = outr[s1];
		    dest[0] = s0;
		    dest[SCREENWIDTH] = s1;
		    dest += SCREENWIDTH*2;
		    s0 = source[2];
		    s1 = source[3];
		    s0 = outr[s0];
		    s1 = outr[s1];
		    source += 4;
		    dest[0] = s0;
		    dest[SCREENWIDTH] = s1;
		    dest += SCREENWIDTH*2;
		  }
		while ((count-=4)>=0);
	      if (count+=4)
		do
		  {
		    *dest = outr[*source++];
		    dest += SCREENWIDTH;
		  }
		while (--count);
//	      column = (column_t *)(source+1);
	      // [FG] back to Vanilla code, we may not run through the entire column
	      column = (column_t *)((byte *)column + column->length + 4);
	    }
	}

    }
}

void V_DrawPatchFullScreen(int scrn, patch_t *patch)
{
    int x = (SCREENWIDTH - SHORT(patch->width)) / 2 - WIDESCREENDELTA;

    patch->leftoffset = 0;
    patch->topoffset = 0;

    // [crispy] fill pillarboxes in widescreen mode
    if (SCREENWIDTH != NONWIDEWIDTH)
    {
       memset(screens[scrn], 0, (SCREENWIDTH<<hires) * (SCREENHEIGHT<<hires));
    }

    V_DrawPatch(x, 0, scrn, patch);
}

//
// V_DrawBlock
//
// Draw a linear block of pixels into the view buffer. 
//
// The bytes at src are copied in linear order to the screen rectangle
// at x,y in screenbuffer scrn, with size width by height.
//
// The destination rectangle is marked dirty.
//
// No return value.
// 

void V_DrawBlock(int x, int y, int scrn, int width, int height, byte *src)
{
#ifdef RANGECHECK
  if (x<0
      ||x+width >SCREENWIDTH
      || y<0
      || y+height>SCREENHEIGHT
      || (unsigned)scrn>4 )
    I_Error ("Bad V_DrawBlock");
#endif

  if (hires)   // killough 11/98: hires support
    {
      byte *dest = screens[scrn] + y*SCREENWIDTH*4+x*2;

      if (width)
	while (height--)
	  {
	    byte *d = dest;
	    int t = width;
	    do
	      d[SCREENWIDTH*2] = d[SCREENWIDTH*2+1] = d[0] = d[1] = *src++;
	    while (d += 2, --t);
	    dest += SCREENWIDTH*4;
	}
    }
  else
    {
      byte *dest = screens[scrn] + y*SCREENWIDTH+x;

      while (height--)
	{
	  memcpy (dest, src, width);
	  src += width;
	  dest += SCREENWIDTH;
	}
    }
}

//
// V_GetBlock
//
// Gets a linear block of pixels from the view buffer.
//
// The pixels in the rectangle at x,y in screenbuffer scrn with size
// width by height are linearly packed into the buffer dest.
// No return value
//

void V_GetBlock(int x, int y, int scrn, int width, int height, byte *dest)
{
  byte *src;

#ifdef RANGECHECK
  if (x<0
      ||x+width >SCREENWIDTH
      || y<0
      || y+height>SCREENHEIGHT
      || (unsigned)scrn>4 )
    I_Error ("Bad V_DrawBlock");
#endif

  if (hires)   // killough 11/98: hires support
    y<<=2, x<<=1, width<<=1, height<<=1;

  src = screens[scrn] + y*SCREENWIDTH+x;
  while (height--)
    {
      memcpy (dest, src, width);
      src += SCREENWIDTH << hires;
      dest += width;
    }
}

// [FG] non hires-scaling variant of V_DrawBlock, used in disk icon drawing

void V_PutBlock(int x, int y, int scrn, int width, int height, byte *src)
{
  byte *dest;

#ifdef RANGECHECK
  if (x<0
      ||x+width >SCREENWIDTH
      || y<0
      || y+height>SCREENHEIGHT
      || (unsigned)scrn>4 )
    I_Error ("Bad V_DrawBlock");
#endif

  if (hires)
    y<<=2, x<<=1, width<<=1, height<<=1;

  dest = screens[scrn] + y*SCREENWIDTH+x;

  while (height--)
    {
      memcpy (dest, src, width);
      dest += SCREENWIDTH << hires;
      src += width;
    }
}

void V_DrawHorizLine(int x, int y, int scrn, int width, byte color)
{
  byte *dest;
  int height = 1;

  // [crispy] prevent framebuffer overflows
  if (x + width > (unsigned)SCREENWIDTH)
    width = SCREENWIDTH - x;

  if (hires)
    y<<=2, x<<=1, width<<=1, height<<=1;

  dest = screens[scrn] + y * SCREENWIDTH + x;

  while (height--)
  {
    memset(dest, color, width);
    dest += SCREENWIDTH << hires;
  }
}

void V_ShadeScreen(void)
{
  int y;
  byte *dest = screens[0];
  const int targshade = 20, step = 2;
  static int oldtic = -1;
  static int screenshade;

  // [FG] start a new sequence
  if (gametic - oldtic > targshade / step)
  {
    screenshade = 0;
  }

  for (y = 0; y < (SCREENWIDTH << hires) * (SCREENHEIGHT << hires); y++)
  {
    dest[y] = colormaps[0][screenshade * 256 + dest[y]];
  }

  if (screenshade < targshade && gametic != oldtic)
  {
    screenshade += step;

    if (screenshade > targshade)
      screenshade = targshade;
  }
  
  oldtic = gametic;
}

//
// V_Init
//
// Allocates the 4 full screen buffers in low DOS memory
// No return value
//
// killough 11/98: rewritten to support hires

void V_Init(void)
{
   // haleyjd
   int size = hires ? SCREENWIDTH*SCREENHEIGHT*4 : SCREENWIDTH*SCREENHEIGHT;
   static byte *s;
   
   if(s)
   {
      Z_Free(s);
   }
   
   screens[3] = (screens[2] = (screens[1] = s = Z_Calloc(size,3,PU_STATIC,0)) + size) + size;
}

//----------------------------------------------------------------------------
//
// $Log: v_video.c,v $
// Revision 1.10  1998/05/06  11:12:48  jim
// Formattted v_video.*
//
// Revision 1.9  1998/05/03  22:53:16  killough
// beautification, simplify translation lookup
//
// Revision 1.8  1998/04/24  08:09:39  jim
// Make text translate tables lumps
//
// Revision 1.7  1998/03/02  11:41:58  killough
// Add cr_blue_status for blue statusbar numbers
//
// Revision 1.6  1998/02/24  01:40:12  jim
// Tuned HUD font
//
// Revision 1.5  1998/02/23  04:58:17  killough
// Fix performance problems
//
// Revision 1.4  1998/02/19  16:55:00  jim
// Optimized HUD and made more configurable
//
// Revision 1.3  1998/02/17  23:00:36  jim
// Added color translation machinery and data
//
// Revision 1.2  1998/01/26  19:25:08  phares
// First rev with no ^Ms
//
// Revision 1.1.1.1  1998/01/19  14:03:05  rand
// Lee's Jan 19 sources
//
//----------------------------------------------------------------------------

