Re: How to print to file in svgalib?

Search this archive.

From: Sergio Masci (sergio@titan.demon.co.uk)
Date: Mon 03 Jul 2000 - 19:36:19 IDT


> 
> 
> On Mon, 3 Jul 2000 lists@123india.com wrote:
> > 	I've been struggling to get an output of my screen graphics for
> > the last 2 months. I just need a picture in jpg/png ( a screenshot). How
> > do I do this?
> > 
> > 	I am running at 1024x768 at 256 colors (mode 12)
> > 
> > 	I can dump a 1024x768 byte memory tofile which I fill with
> > gl_getbox(0,0,1024,768,memory_dump_pointer)
> > 	
> > 	I can also get a color table of 768 bytes with
> > gl_getpalette(color_table_pointer)
> > 	
> > 	I save these dumps to file and use netpbm's
> > raw2gif -s 1024 768 -p color_table_file memory_dump_file > out.gif
> 
> Check that the raw files are really correct:
> Try clearing the screen and only drawing a single pixel of color 3 at
> (1,1) and then dump to file and check that the file is all zeros, except
> for a 3 in the 1025th byte.
> Set the palettet to something, and see if the color table file after you
> run your program has the values you programmed.
> 
> 
> -- 
> Matan Ziv-Av.                         matan@svgalib.org


I've attached a section of code that should do what you want (dumps a
section of the physical screen to a PNG file). I've had to change a few
lines to simplify it. If you have any problems with it look at the bit
that sets the PNG palette and the gl_getbox, everything else is 'as was'
in the working section of code from which it was lifted.

You will need to compile this against libpng 0.81 and the latest version
of zlib.

Regards
Sergio



/*----------------------------------------------------------------------*/
/*----------------------------------------------------------------------*/
void print_screen(int x1, int y1, int x2, int y2)
{
	FILE	*fp;

	png_struct
		*png_ptr;

	png_info
		*info_ptr;


static	int	cnt = 0;
	char	fname[256];

	int	xlen, ylen;


	xlen = x2 - x1 + 1;
	ylen = y2 - y1 + 1;


	sprintf(fname, "scrn%04.4d.png", cnt++);


	fp = fopen(fname, "wb");

	if (fp == NULL)
	{	report_error("error %d returned while trying to open PNG file '%s' for writing\n",
			errno, fname);
		abort();
	}

	// allocate the necessary structures
	png_ptr = (png_struct *)malloc(sizeof(png_struct));

	if (png_ptr == NULL)
	{	report_error("could not allocate png_struct\n");
		abort();
	}

	info_ptr = (png_info *)malloc(sizeof(png_info));

	if (info_ptr == NULL)
	{	report_error("could not allocate png_info\n");
		abort();
	}

	// set error handling
	if (setjmp(png_ptr->jmpbuf))
	{	report_error("error returned by png library\n");
		abort();
	}

	// initialize the structures
	png_info_init(info_ptr);
	png_write_init(png_ptr);

	// set up the output control
	png_init_io(png_ptr, fp);

	// set the file information here
	info_ptr->width      = xlen;
	info_ptr->height     = ylen;
	info_ptr->bit_depth  = 8;
	info_ptr->color_type = PNG_COLOR_TYPE_PALETTE;

//----------------------

	// set the palette
	info_ptr->valid |= PNG_INFO_PLTE;
	info_ptr->palette = (png_color *)malloc(256 * sizeof (png_color));
	info_ptr->num_palette = 256;


	int	xred, xgreen, xblue;
	int	adj;
	int	bits_per_colour;

	bits_per_colour = 6;

	adj = 8 - bits_per_colour;


	for (int j=0; j<256; j++)
	{
		vga_getpalette(0, &xred, &xgreen, &xblue);

		info_ptr->palette[j].red   = xred   << adj;
		info_ptr->palette[j].green = xgreen << adj;
		info_ptr->palette[j].blue  = xblue  << adj;
	}

	// write the file information
	png_write_info(png_ptr, info_ptr);


	char *dst;

	dst = (char *) malloc(xlen);

	for ( ; y1<=y2; y1++)
	{
		gl_getbox(0, y1, 1024, y1, dst);

		// Write a row at a time.
		png_write_rows(png_ptr, &dst, 1);
	}

	free dst;

//----------------------

	// write the rest of the file
	png_write_end(png_ptr, NULL);

	// clean up after the write, and free any memory allocated
	png_write_destroy(png_ptr);

	if (info_ptr->palette)
	{	// a palette was used, so free it
		free(info_ptr->palette);
	}

	// free the structures
	free(png_ptr);
	free(info_ptr);

	if (fclose(fp) == -1)
	{	report_error("error %d returned while trying to close PNG file\n",
			errno);
		abort();
	}
}


This archive was generated by hypermail 2.1.4 : Wed 21 Jan 2004 - 22:10:23 IST