void calc_light_table(palette *pal)
{
  white_light=(unsigned char *)malloc(256*64);
  green_light=(unsigned char *)malloc(256*64);
  for (int i=0; i<TTINTS; i++)
      tints[i]=(uchar *)malloc(256);

  jFILE fp("light.tbl","rb");
  int recalc=0;
  if (fp.open_failure()) recalc=1;
  else
  {
    if (fp.read_uint16()!=calc_crc((unsigned char *)pal->addr(),768))
      recalc=1;
    else
    {
      fp.read(white_light,256*64);
      fp.read(green_light,256*64);
      for (i=0; i<TTINTS; i++)
        fp.read(tints[i],256);
    }
  }

  if (recalc)
  {
    fprintf(stderr,"Palette has changed, recalculating light table...\n");
    fprintf(stderr,"white : ");
    for (int color=0; color<256; color++)
    {
      unsigned char r,g,b;
      pal->get(color,r,g,b);
      if (color%16==0)
        fprintf(stderr,"%d ",color);
      for (int intensity=63; intensity>=0; intensity--)
      {
    if (r>0 || g>0 || b>0)
          white_light[intensity*256+color]=pal->find_closest(r,g,b);
    else
          white_light[intensity*256+color]=0;
    if (r) r--;  if (g) g--;  if (b) b--;
      }
    }

    fprintf(stderr,"green : ");
    for (color=0; color<256; color++)
    {
      unsigned char r,g,b;
      pal->get(color,r,g,b);
      r=r*3/5; b=b*3/5; g+=7; if (g>255) g=255;
      if (color%16==0)
        fprintf(stderr,"%d ",color);
      for (int intensity=63; intensity>=0; intensity--)
      {
    if (r>0 || g>0 || b>0)
          green_light[intensity*256+color]=pal->find_closest(r,g,b);
    else
          green_light[intensity*256+color]=0;
    if (r) r--;
    if ((intensity&1)==1)
      if (g) g--;
    if (b) b--;
      }
    }

    dprintf("\ncalculating tints : \n");
    uchar t[TTINTS*6]={ 0,0,0,0,0,0, // normal
                   0,0,0,1,0,0,     // red
           0,0,0,1,1,0,     // yellow
           0,0,0,1,0,1,     // purple
           0,0,0,1,1,1,     // gray
           0,0,0,0,1,0,     // green
           0,0,0,0,0,1,     // blue
           0,0,0,0,1,1,     // cyan






           0,0,0,0,0,0   // reverse green  (night vision effect)
         } ;
    uchar *ti=t+6;
    uchar *c;
    for (i=0,c=tints[0]; i<256; i++,c++) *c=i;  // make the normal tint (maps everthing to itself)
    for (i=0,c=tints[TTINTS-1]; i<256; i++,c++)  // reverse green
    {
      int r=pal->red(i)/2,g=255-pal->green(i)-30,b=pal->blue(i)*3/5+50;
      if (g<0) g=0;
      if (b>255) b=0;
      *c=pal->find_closest(r,g,b);
    }

    // make the colored tints
    for (i=1; i<TTINTS-1; i++)
    {
      calc_tint(tints[i],ti[0],ti[1],ti[2],ti[3],ti[4],ti[5],pal);
      ti+=6;
    }



    jFILE f("light.tbl","wb");
    f.write_uint16(calc_crc((unsigned char *)pal->addr(),768));
    f.write(white_light,256*64);
    f.write(green_light,256*64);
    for (int i=0; i<TTINTS; i++)
      f.write(tints[i],256);
  }
}
Example #2
0
void calc_light_table(palette *pal)
{
    white_light_initial=(uint8_t *)malloc(256*64);
    white_light=white_light_initial;

//    green_light=(uint8_t *)malloc(256*64);
    int i = 0;
    for( ; i < TTINTS; i++ )
    {
        tints[i] = (uint8_t *)malloc( 256 );
    }

    char *lightpath;
    lightpath = (char *)malloc( strlen( get_save_filename_prefix() ) + 9 + 1 );
    sprintf( lightpath, "%slight.tbl", get_save_filename_prefix() );

    bFILE *fp=open_file( lightpath, "rb" );
    int recalc = 0;
    if( fp->open_failure() )
    {
        recalc = 1;
    }
    else
    {
        if (fp->read_uint16()!=calc_crc((uint8_t *)pal->addr(),768))
            recalc=1;
        else
        {
            fp->read(white_light,256*64);
//            fp->read(green_light,256*64);
            for (i=0; i<TTINTS; i++)
                fp->read(tints[i],256);
            fp->read(bright_tint,256);
//            trans_table=(uint8_t *)malloc(256*256);
//            fp.read(trans_table,256*256);
        }
    }
    delete fp;
    fp = NULL;

    if( recalc )
    {
        dprintf("Palette has changed, recalculating light table...\n");
        stat_man->push("white light",NULL);
        int color=0;
        for (; color<256; color++)
        {
            uint8_t r,g,b;
            pal->get(color,r,g,b);
            stat_man->update(color*100/256);
            for (int intensity=63; intensity>=0; intensity--)
            {
                if (r>0 || g>0 || b>0)
                    white_light[intensity*256+color]=pal->find_closest(r,g,b);
                else
                    white_light[intensity*256+color]=0;
                if (r) r--;  if (g) g--;  if (b) b--;
            }
        }
        stat_man->pop();

/*    stat_man->push("green light",NULL);
    for (color=0; color<256; color++)
    {
      stat_man->update(color*100/256);
      uint8_t r,g,b;
      pal->get(color,b,r,g);
      r=r*3/5; b=b*3/5; g+=7; if (g>255) g=255;

      for (int intensity=63; intensity>=0; intensity--)
      {
    if (r>0 || g>0 || b>0)
          green_light[intensity*256+color]=pal->find_closest(r,g,b);
    else
          green_light[intensity*256+color]=0;
    if (r) r--;
    if ((intensity&1)==1)
      if (g) g--;
    if (b) b--;
      }
    }
    stat_man->pop(); */

    stat_man->push("tints",NULL);
    uint8_t t[TTINTS*6]={ 0,0,0,0,0,0, // normal
                   0,0,0,1,0,0,     // red
           0,0,0,1,1,0,     // yellow
           0,0,0,1,0,1,     // purple
           0,0,0,1,1,1,     // gray
           0,0,0,0,1,0,     // green
           0,0,0,0,0,1,     // blue
           0,0,0,0,1,1,     // cyan






           0,0,0,0,0,0   // reverse green  (night vision effect)
         } ;
    uint8_t *ti=t+6;
    uint8_t *c;
    for (i=0,c=tints[0]; i<256; i++,c++) *c=i;  // make the normal tint (maps everthing to itself)
    for (i=0,c=tints[TTINTS-1]; i<256; i++,c++)  // reverse green
    {
      int r=pal->red(i)/2,g=255-pal->green(i)-30,b=pal->blue(i)*3/5+50;
      if (g<0) g=0;
      if (b>255) b=0;
      *c=pal->find_closest(r,g,b);
    }
    for (i=0; i<256; i++)
    {
      int r=pal->red(i)+(255-pal->red(i))/2,
          g=pal->green(i)+(255-pal->green(i))/2,
          b=pal->blue(i)+(255-pal->blue(i))/2;
      bright_tint[i]=pal->find_closest(r,g,b);
    }

    // make the colored tints
    for (i=1; i<TTINTS-1; i++)
    {
      stat_man->update(i*100/(TTINTS-1));
      calc_tint(tints[i],ti[0],ti[1],ti[2],ti[3],ti[4],ti[5],pal);
      ti+=6;
    }
    stat_man->pop();
/*    fprintf(stderr,"calculating transparency tables (256 total)\n");
    trans_table=(uint8_t *)malloc(256*256);

    uint8_t *tp=trans_table;
    for (i=0; i<256; i++)
    {
      uint8_t r1,g1,b1,r2,g2,b2;
      pal->get(i,r1,g1,b1);
      if ((i%16)==0)
        fprintf(stderr,"%d ",i);
      for (int j=0; j<256; j++,tp++)
      {
    if (r1==0 && r2==0 && b2==0)
      *tp=j;
    else
    {
      pal->get(j,r2,g2,b2);
      *tp=pal->find_closest((r2-r1)*3/7+r1,(g2-g1)*3/7+g1,(b2-b1)*3/7+b1);
    }
      }
    }*/


        bFILE *f = open_file( lightpath, "wb" );
        if( f->open_failure() )
            dprintf( "Unable to open file light.tbl for writing\n" );
        else
        {
            f->write_uint16(calc_crc((uint8_t *)pal->addr(),768));
            f->write(white_light,256*64);
//      f->write(green_light,256*64);
            for (int i=0; i<TTINTS; i++)
                f->write(tints[i],256);
            f->write(bright_tint,256);
//    f.write(trans_table,256*256);
        }
        delete f;
    }
    free( lightpath );
}