Esempio n. 1
0
int main(int argc, char **argv)
{
   char *pal_file, *output_dir;

   if (argc != 3)
      usage();

   pal_file = argv[1];
   output_dir = argv[2];

   ReadPalette(pal_file);

   fprintf(stderr, "Reserving special colors...\n");
   BlockIndices();

   fprintf(stderr, "Computing lighting palettes...\n");
   MakeLightPalettes();

   fprintf(stderr, "Computing blend bixlat palettes...\n");
   MakeBlendBiXlat(_blend25, 25, 75);
   MakeBlendBiXlat(_blend50, 50, 50);
   MakeBlendBiXlat(_blend75, 75, 25);

   fprintf(stderr, "Writing %s...\n", output_file);
   WritePalette(output_dir);

   fprintf(stderr, "Done.\n");
   return 0;
}
Esempio n. 2
0
void FCEU_ResetPalette(void)
{
 if(FCEUGameInfo)
 {
   ChoosePalette();
   WritePalette();
 }
}
Esempio n. 3
0
void ScreenShot()
{
  unsigned char b1;
  unsigned short int w1;
  int i,n; //zero 5.7.99
  char fnamestr[13];
  static int ss=0;
  FILE *pcxf;

//--- zero 5.7.99
  n=0;
  do
  {
    sprintf(fnamestr,"%d.pcx",n);
    pcxf=fopen(fnamestr,"r");
    i=(int)pcxf;
    if(pcxf) fclose(pcxf);
    n++;
  } while(i);
  n--;

  // Takes a snapshot of the current screen.

   sprintf(fnamestr,"%d.pcx",n);
//---

   pcxf=fopen(&fnamestr,"wb");
   ss++;

// Write PCX header

   b1=10; fwrite(&b1, 1, 1, pcxf); // manufacturer always = 10
   b1=5; fwrite(&b1, 1, 1, pcxf);  // version = 3.0, >16 colors
   b1=1; fwrite(&b1, 1, 1, pcxf);  // encoding always = 1
   b1=8; fwrite(&b1, 1, 1, pcxf);  // 8 bits per pixel, for 256 colors
   w1=0; fwrite(&w1, 1, 2, pcxf);  // xmin = 0;
   w1=0; fwrite(&w1, 1, 2, pcxf);  // ymin = 0;
 w1=sx-1; fwrite(&w1, 1, 2, pcxf);  // xmax = 319;
 w1=sy-1; fwrite(&w1, 1, 2, pcxf);  // ymax = 199;
 w1=sx; fwrite(&w1, 1, 2, pcxf);  // hres = 320;
 w1=sy; fwrite(&w1, 1, 2, pcxf);  // vres = 200;

 fwrite(screen,1,48,pcxf);

 b1=0; fwrite(&b1, 1, 1, pcxf);   // reserved always = 0.
 b1=1; fwrite(&b1, 1, 1, pcxf);   // number of color planes. Just 1 for 8bit.
 w1=sx; fwrite(&w1, 1, 2, pcxf); // number of bytes per line

 w1=0; fwrite(&w1, 1, 1, pcxf);
 fwrite(screen, 1, 59, pcxf);          // filler

 for (w1=0; w1<sy; w1++)
     WritePCXLine(screen+((w1+16)*tsx)+16, sx, pcxf);

 WritePalette(pcxf);
 fclose(pcxf);
 timer_count=0;
}
Esempio n. 4
0
int imFileFormatLED::WriteImageInfo()
{
  this->file_data_type = IM_BYTE;
  this->file_color_mode = imColorModeSpace(this->user_color_mode);
  this->file_color_mode |= IM_TOPDOWN;

  if (WritePalette() != IM_ERR_NONE)
    return IM_ERR_ACCESS;

  imBinFilePrintf(handle, "(%d, %d\n", this->width, this->height);

  return IM_ERR_NONE;
}
Esempio n. 5
0
// Converted from Kevin Horton's qbasic palette generator.
static void CalculatePalette(void)
{
	//PRECONDITION: ntsc palette is enabled
 	if(!ntsccol_enable)
		return;

	int x,z;
	int r,g,b;
	double s,luma,theta;
	static uint8 cols[16]={0,24,21,18,15,12,9,6,3,0,33,30,27,0,0,0};
	static uint8 br1[4]={6,9,12,12};
	static double br2[4]={.29,.45,.73,.9};
	static double br3[4]={0,.24,.47,.77};

	for(x=0;x<=3;x++)
		for(z=0;z<16;z++)
		{
			s=(double)ntsctint/128;
			luma=br2[x];
			if(z==0)  {s=0;luma=((double)br1[x])/12;}

			if(z>=13)
			{
				s=luma=0;
				if(z==13)
					luma=br3[x];
			}

			theta=(double)M_PI*(double)(((double)cols[z]*10+ (((double)ntschue/2)+300) )/(double)180);
			r=(int)((luma+s*sin(theta))*256);
			g=(int)((luma-(double)27/53*s*sin(theta)+(double)10/53*s*cos(theta))*256);
			b=(int)((luma-s*cos(theta))*256);


			if(r>255) r=255;
			if(g>255) g=255;
			if(b>255) b=255;
			if(r<0) r=0;
			if(g<0) g=0;
			if(b<0) b=0;

			palette_ntsc[(x<<4)+z].r=r;
			palette_ntsc[(x<<4)+z].g=g;
			palette_ntsc[(x<<4)+z].b=b;
		}

	//can't call FCEU_ResetPalette(), it would be re-entrant
	//see precondition for this function
	WritePalette();
}
Esempio n. 6
0
/* Converted from Kevin Horton's qbasic palette generator. */
static void CalculatePalette(void)
{
 int x,z;
 int r,g,b;
 double s,luma,theta;
 static uint8 cols[16]={0,24,21,18,15,12,9,6,3,0,33,30,27,0,0,0};
 static uint8 br1[4]={6,9,12,12};
 static double br2[4]={.29,.45,.73,.9};
 static double br3[4]={0,.24,.47,.77};

 for(x=0;x<=3;x++)
  for(z=0;z<16;z++)
  {
   s=(double)ntsctint/128;
   luma=br2[x];
   if(z==0)  {s=0;luma=((double)br1[x])/12;}

   if(z>=13)
   {
    s=luma=0;
    if(z==13)
     luma=br3[x];
   }

   theta=(double)M_PI*(double)(((double)cols[z]*10+ (((double)ntschue/2)+300) )/(double)180);
   r=(int)((luma+s*sin(theta))*256);
   g=(int)((luma-(double)27/53*s*sin(theta)+(double)10/53*s*cos(theta))*256);
   b=(int)((luma-s*cos(theta))*256);


   if(r>255) r=255;
   if(g>255) g=255;
   if(b>255) b=255;
   if(r<0) r=0;
   if(g<0) g=0;
   if(b<0) b=0;

   paletten[(x<<4)+z].r=r;
   paletten[(x<<4)+z].g=g;
   paletten[(x<<4)+z].b=b;
  }
 WritePalette();
}
Esempio n. 7
0
void OutputVSPpcx()
{ int i,j,_x,_y;
  unsigned char *ptr;
  char b1;
  short int w1,w2;

  // First figure out the filename

  i=0;
  ptr=&vspname;
  while (*ptr != '.')
  {
    strbuf[i]=*ptr;
    ptr++;
    i++;
  }
  strbuf[i++]='.';
  strbuf[i++]='P';
  strbuf[i++]='C';
  strbuf[i++]='X';
  strbuf[i++]=0;

  pcxf=fopen(strbuf,"wb");

// Write PCX header

   b1=10; fwrite(&b1, 1, 1, pcxf); // manufacturer always = 10
   b1=5; fwrite(&b1, 1, 1, pcxf);  // version = 3.0, >16 colors
   b1=1; fwrite(&b1, 1, 1, pcxf);  // encoding always = 1
   b1=8; fwrite(&b1, 1, 1, pcxf);  // 8 bits per pixel, for 256 colors
   w1=0; fwrite(&w1, 1, 2, pcxf);  // xmin = 0;
   w1=0; fwrite(&w1, 1, 2, pcxf);  // ymin = 0;
 w1=319; fwrite(&w1, 1, 2, pcxf);  // xmax = 319;

 // At this point we need to figure out how many vertical rows tall the
 // PCX needs to be in order to accomidate the number of tiles in the VSP.

 w2=numtiles/18; w2++;
 w2=(w2*17); fwrite(&w2, 1, 2, pcxf);  // ymax = ?;
 w1=320; fwrite(&w1, 1, 2, pcxf);      // hres = 320;
 w2++; fwrite(&w2, 1, 2, pcxf);        // vres = ?;

 fwrite(screen, 1, 48, pcxf);  // 16-color palette data. Who knows what's
                               // actually in here. It doesn't matter since
                               // the 256-color palette is stored elsewhere.

 b1=0; fwrite(&b1, 1, 1, pcxf);   // reserved always = 0.
 b1=1; fwrite(&b1, 1, 1, pcxf);   // number of color planes. Just 1 for 8bit.
 w1=320; fwrite(&w1, 1, 2, pcxf); // number of bytes per line

 w1=0; fwrite(&w1, 1, 1, pcxf);
 fwrite(screen, 1, 59, pcxf);          // filler

 // The header is written. Now we need to generate a large buffer where we'll
 // "draw" the PCX, and then encode the data and save the PCX.

 ptr=(char *) malloc(320*w2);
 memset(ptr,255,(320*w2));            // Give the PCX a white background

 // Now we "draw" the tiles into the buffer.

 for (i=0; i<numtiles; i++)
 {
    j=i/18;
    _y=1+(j*17);
    j=i-(j*18);
    _x=1+(j*17);
    acopytile(_x,_y,vsp+(i*256),ptr);
 }

 // And now we save the rest of the PCX.

 for (w1=0; w1<w2; w1++)
     WritePCXLine(ptr+(w1*320),320,pcxf);

  WritePalette(pcxf);
  fclose(pcxf);

  vfree(ptr);
}
Esempio n. 8
0
int WriteGIFFrame(FILE *fp, byte *p, int w, int h,
                  byte *rmap, byte *gmap, byte *bmap,
                  int numcols,
                  int frames, int time, ColorStyleType colorstyle) // time in 0.01 sec
{ int   RWidth, RHeight;
  int   LeftOfs, TopOfs;
  int   ColorMapSize, InitCodeSize, Background, BitsPerPixel;
  int   i,j,f;
  int   nc;
  byte *pic8;
  int   Interlace;
  int   cmap;

  struct GifStore *aGifStore = (struct GifStore *)malloc(sizeof(struct GifStore));
  if (aGifStore==NULL)
  { AVSerror("WriteGIFFrame: out of memory");
    return AVS_ERROR;
  }
  memset(aGifStore, 0, sizeof(struct GifStore));

  pic8 = p;

  Interlace = 0;
  Background = 0;

  for (i=0; i<256; i++)
    aGifStore->pc2nc[i] = aGifStore->r1[i] = aGifStore->g1[i] = aGifStore->b1[i] = 0;

  /* compute number of unique colors */
  nc = 0;

  for (i=0; i<numcols; i++)
  { /* see if color #i is already used */
    for (j=0; j<i; j++)
    { if (rmap[i] == rmap[j] && gmap[i] == gmap[j] && bmap[i] == bmap[j])
         break;
    }

    if (j==i)    /* wasn't found */
    { aGifStore->pc2nc[i] = (byte)nc;
      aGifStore->r1[nc] = rmap[i];
      aGifStore->g1[nc] = gmap[i];
      aGifStore->b1[nc] = bmap[i];
      nc++;
    }
    else
      aGifStore->pc2nc[i] = aGifStore->pc2nc[j];
  }

  nc = 256;

  /* figure out 'BitsPerPixel' */
  for (i=1; i<8; i++)
    if ( (1<<i) >= nc)
      break;

  BitsPerPixel = i;

  ColorMapSize = 1 << BitsPerPixel;

  RWidth  = w;
  RHeight = h;
  LeftOfs = TopOfs = 0;

  if (BitsPerPixel <= 1) InitCodeSize = 2;
                    else InitCodeSize = BitsPerPixel;

  if (!fp)
  { AVSerror("WriteGIFFrame: file not open for writing");
    free(aGifStore);
    return AVS_ERROR;
  }

  if (frames<1) frames=1;

  for(f=0; f<frames; f++)
  { /* image extension */
    // Graphic Control Extension starts with 21 F9 04
    fputc(0x21, fp); fputc(0xf9, fp); fputc(0x04, fp);
    fputc(0x00, fp);   // no transparency
    putword2(time, fp); // time in 0.01 sec
    fputc(0x00, fp);   // transparent color (not used)
    fputc(0x00, fp);

    fputc( 0x2c, fp );              /* image separator */

    /* Write the Image header */
    putword2(LeftOfs, fp);
    putword2(TopOfs,  fp);
    putword2(RWidth,   fp);
    putword2(RHeight,  fp);

    if (colorstyle==csLocalPalette)
      cmap = 0x80 | 0x07;
    else
      cmap = 0;

    if (Interlace)
      fputc(cmap | 0x40, fp);   /* Use local Colormap, maybe Interlace */
    else
      fputc(cmap | 0x00, fp);

    if (colorstyle==csLocalPalette)
      WritePalette(fp, aGifStore, 256);

    fputc(InitCodeSize, fp);
    compress((char)(InitCodeSize+1), fp, pic8, w*h, aGifStore);
    pic8 += w * h;

    fputc(0,fp);                      /* Write out a Zero-length packet (EOF) */

  };

  /* GIF file terminator (0x3b) should be written in caller*/

  free(aGifStore);
  if (ferror(fp))
    return AVS_ERROR;
  return AVS_OK;
}
Esempio n. 9
0
int WriteGIFHeader(FILE *fp, int w, int h, byte *rmap, byte *gmap, byte *bmap,
             int numcols, ColorStyleType colorstyle, char *comment)
{ int   RWidth, RHeight;
  int   LeftOfs, TopOfs;
  int   ColorMapSize;
  int   InitCodeSize, BitsPerPixel;
  int   Background;
  int   i,j;
  int   nc;
  int   Interlace;

  struct GifStore *aGifStore = (struct GifStore *)malloc(sizeof(struct GifStore));
  if (aGifStore==NULL)
  { AVSerror("WriteGIFHeader: out of memory");
    return AVS_ERROR;
  }
  memset(aGifStore, 0, sizeof(struct GifStore));

  Interlace = 0;
  Background = 0;

  for (i=0; i<256; i++)
    aGifStore->pc2nc[i] = aGifStore->r1[i] = aGifStore->g1[i] = aGifStore->b1[i] = 0;

  /* compute number of unique colors */
  nc = 0;

  for (i=0; i<numcols; i++)
  { /* see if color #i is already used */
    for (j=0; j<i; j++)
    { if (rmap[i] == rmap[j] && gmap[i] == gmap[j] && bmap[i] == bmap[j])
        break;
    }

    if (j==i)    /* wasn't found */
    { aGifStore->pc2nc[i] = (byte)nc;
      aGifStore->r1[nc] = rmap[i];
      aGifStore->g1[nc] = gmap[i];
      aGifStore->b1[nc] = bmap[i];
      nc++;
    }
    else
      aGifStore->pc2nc[i] = aGifStore->pc2nc[j];
  }

  /* figure out 'BitsPerPixel' */
  for (i=1; i<8; i++)
    if ( (1<<i) >= nc)
      break;

  BitsPerPixel = (char)i;

  ColorMapSize = 1 << BitsPerPixel;

  RWidth  = w;
  RHeight = h;
  LeftOfs = TopOfs = 0;

  if (BitsPerPixel <= 1) InitCodeSize = 2;
                    else InitCodeSize = BitsPerPixel;

  if (!fp)
  { AVSerror("WriteGIFHeader: file not open for writing");
    free(aGifStore);
    return AVS_ERROR;
  }

  fwrite("GIF89a", (size_t) 1, (size_t) 6, fp);    /* the GIF magic number */

  putword2(RWidth, fp);           /* screen descriptor */
  putword2(RHeight, fp);

  if (colorstyle==csGlobalPalette)
  { i = 0x80;	                   /* Yes, there is a color map */
    i |= (8-1)<<4;                 /* OR in the color resolution (hardwired 8) */
    i |= (BitsPerPixel - 1);       /* OR in the # of bits per pixel */
  }
  else
    i = 0;
  fputc(i,fp);

  fputc(Background, fp);         /* background color */

  fputc(0, fp);                  /* future expansion byte */

  if (colorstyle==csGlobalPalette)
    WritePalette(fp, aGifStore, ColorMapSize);
  WriteComment(fp, comment);

  free(aGifStore);

  return AVS_OK;
}
Esempio n. 10
0
FIBITMAP* LFPQuantizer::Quantize(FIBITMAP *dib, int ReserveSize, RGBQUAD *ReservePalette) {

	if (ReserveSize > 0 && ReservePalette != NULL) {
		AddReservePalette(ReservePalette, ReserveSize);
	}

	const unsigned width = FreeImage_GetWidth(dib);
	const unsigned height = FreeImage_GetHeight(dib);

	FIBITMAP *dib8 = FreeImage_Allocate(width, height, 8);
	if (dib8 == NULL) {
		return NULL;
	}

	const unsigned src_pitch = FreeImage_GetPitch(dib);
	const unsigned dst_pitch = FreeImage_GetPitch(dib8);

	const BYTE * const src_bits = FreeImage_GetBits(dib);
	BYTE * const dst_bits = FreeImage_GetBits(dib8);

	unsigned last_color = -1;
	int last_index = 0;

	if (FreeImage_GetBPP(dib) == 24) {

		// Getting the source pixel as an unsigned int is much faster than
		// working with FI_RGBA_xxx and shifting. However, this may fail
		// for the very last pixel, since its rgbReserved member (alpha)
		// may actually point to an address beyond the bitmap's memory. So,
		// we do not process the last scanline in the first loop.

		// Process all but the last scanline.
		for (unsigned y = 0; y < height - 1; ++y) {
			BYTE *dst_line = dst_bits + y * dst_pitch;
			const BYTE *src_line = src_bits + y * src_pitch;
			for (unsigned x = 0; x < width; ++x) {
				const unsigned color = *((unsigned *) src_line) & 0x00FFFFFF;
				if (color != last_color) {
					last_color = color;
					last_index = GetIndexForColor(color);
					if (last_index == -1) {
						FreeImage_Unload(dib8);
						return NULL;
					}
				}
				dst_line[x] = last_index;
				src_line += 3;
			}
		}

		// Process all but the last pixel of the last scanline.
		BYTE *dst_line = dst_bits + (height - 1) * dst_pitch;
		const BYTE *src_line = src_bits + (height - 1) * src_pitch;
		for (unsigned x = 0; x < width - 1; ++x) {
			const unsigned color = *((unsigned *) src_line) & 0x00FFFFFF;
			if (color != last_color) {
				last_color = color;
				last_index = GetIndexForColor(color);
				if (last_index == -1) {
					FreeImage_Unload(dib8);
					return NULL;
				}
			}
			dst_line[x] = last_index;
			src_line += 3;
		}

		// Process the last pixel (src_line should already point to it).
		const unsigned color = 0 | src_line[FI_RGBA_BLUE] << FI_RGBA_BLUE_SHIFT
				| src_line[FI_RGBA_GREEN] << FI_RGBA_GREEN_SHIFT
				| src_line[FI_RGBA_RED] << FI_RGBA_RED_SHIFT;
		if (color != last_color) {
			last_color = color;
			last_index = GetIndexForColor(color);
			if (last_index == -1) {
				FreeImage_Unload(dib8);
				return NULL;
			}
		}
		dst_line[width - 1] = last_index;

	} else {
		for (unsigned y = 0; y < height; ++y) {
			BYTE *dst_line = dst_bits + y * dst_pitch;
			const BYTE *src_line = src_bits + y * src_pitch;
			for (unsigned x = 0; x < width; ++x) {
				const unsigned color = *((unsigned *) src_line) & 0x00FFFFFF;
				if (color != last_color) {
					last_color = color;
					last_index = GetIndexForColor(color);
					if (last_index == -1) {
						FreeImage_Unload(dib8);
						return NULL;
					}
				}
				dst_line[x] = last_index;
				src_line += 4;
			}
		}
	}

	WritePalette(FreeImage_GetPalette(dib8));
	return dib8;
}
Esempio n. 11
0
int imFileFormatRAS::WriteImageInfo()
{
  this->file_data_type = IM_BYTE;
  this->file_color_mode = imColorModeSpace(this->user_color_mode);

  if (imStrEqual(this->compression, "RLE"))
    this->comp_type = RAS_BYTE_ENCODED;
  else
    this->comp_type = RAS_STANDARD;

  // Force the palette, even for Binary and Gray.
  this->map_type = RAS_EQUAL_RGB;

  if (this->file_color_mode == IM_BINARY)
  {
    this->bpp = 1;
    this->convert_bpp = 1;
  }
  else if (this->file_color_mode == IM_RGB)
  {
    this->file_color_mode |= IM_PACKED;
    this->map_type = RAS_NONE;
    this->bpp = 24;

    if (imColorModeHasAlpha(this->user_color_mode))
    {
      this->file_color_mode |= IM_ALPHA;
      this->bpp = 32;
    }
  }
  else
    this->bpp = 8;

  this->file_color_mode |= IM_TOPDOWN;

  this->line_raw_size = imFileLineSizeAligned(this->width, this->bpp, 2);
  this->line_buffer_extra = 2; // room enough for padding

  if (this->comp_type == RAS_BYTE_ENCODED)
  {
    // allocates more than enough since compression algoritm can be ineficient
    this->line_buffer_extra += 2*this->line_raw_size;
  }

  /* writes the RAS file header */

  unsigned int dword_value = RAS_ID;
  imBinFileWrite(handle, &dword_value, 1, 4); /* identifier */
  dword_value = this->width;
  imBinFileWrite(handle, &dword_value, 1, 4); /* image width */
  dword_value = this->height;
  imBinFileWrite(handle, &dword_value, 1, 4); /* image height */
  dword_value = this->bpp;
  imBinFileWrite(handle, &dword_value, 1, 4); /* bits per pixel */
  dword_value = this->height * this->line_raw_size;
  imBinFileWrite(handle, &dword_value, 1, 4); /* image lenght */
  dword_value = this->comp_type;
  imBinFileWrite(handle, &dword_value, 1, 4); /* compression information */
  dword_value = this->map_type;
  imBinFileWrite(handle, &dword_value, 1, 4); /* palette information */
  dword_value = (this->map_type == RAS_NONE)? 0: this->palette_count * 3;
  imBinFileWrite(handle, &dword_value, 1, 4); /* palette lenght */

  /* tests if everything was ok */
  if (imBinFileError(handle))
    return IM_ERR_ACCESS;

  if (this->map_type != RAS_NONE)
    return WritePalette();

  return IM_ERR_NONE;
}