Files
86Box/src/printer/png.c
OBattler 46d0ed2baa Rewritten 808x CPU emulation core based on reenigne's XTCE, VisiOn, SnatchIt, and 8088 MPH now work correctly;
Fixed PC speaker sound volume in PIT mode 0;
A few CPU emulation clean-ups;
Hard disk controller changing redone in a less messy way;
Re-added the long-missing key send delay handling to the XT keyboard handler;
Fixed a bug that was causing SLiRP not to work when compiled with MingW/GCC 7.3.0-2 or newer;
Some serial mouse and port fixes;
A lot of changes to printer emulation, mostly based on DOSBox-X;
Printer PNG writer now uses statically linked libpng;
Added support for the HxC MFM floppy image format and upped 86F format version to 2.12;
Ported various things from PCem and some from VARCem;
Added the S3 86c801/805 emulation (patch from TheCollector1995);
Fixed and renamed the EGA monitor options;
Better synchronized the 808x to the PIT and the CGA;
Fixed the CGA wait state calculation;
Cleaned up some things in mem.c;
Fixed some things in the floppy emulation to make VisiOn get the correct errors from the copy protection disk;
Fixed several renderer-related bugs, including the SDL2 renderer's failure to take screenshots;
The Jenkins builds are now compiled with MingW/GCC 7.4.0-1 and include all the required DLL's.
2019-02-06 03:34:39 +01:00

265 lines
7.0 KiB
C

/*
* VARCem Virtual ARchaeological Computer EMulator.
* An emulator of (mostly) x86-based PC systems and devices,
* using the ISA,EISA,VLB,MCA and PCI system buses, roughly
* spanning the era between 1981 and 1995.
*
* This file is part of the VARCem Project.
*
* Provide centralized access to the PNG image handler.
*
* Version: @(#)png.c 1.0.5 2018/11/19
*
* Author: Fred N. van Kempen, <decwiz@yahoo.com>
*
* Copyright 2018 Fred N. van Kempen.
*
* Redistribution and use in source and binary forms, with
* or without modification, are permitted provided that the
* following conditions are met:
*
* 1. Redistributions of source code must retain the entire
* above notice, this list of conditions and the following
* disclaimer.
*
* 2. Redistributions in binary form must reproduce the above
* copyright notice, this list of conditions and the
* following disclaimer in the documentation and/or other
* materials provided with the distribution.
*
* 3. Neither the name of the copyright holder nor the names
* of its contributors may be used to endorse or promote
* products derived from this software without specific
* prior written permission.
*
* THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
* "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
* LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A
* PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
* HOLDER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
* SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
* LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
* DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
* THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
* (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
* OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
*/
#include <stdio.h>
#include <stdint.h>
#include <string.h>
#include <stdlib.h>
#include <wchar.h>
#include <errno.h>
#define PNG_DEBUG 0
#include <png.h>
#include "../86box.h"
#include "../plat.h"
#include "../plat_dynld.h"
#include "../ui.h"
#include "../video/video.h"
#include "png_struct.h"
#ifdef _WIN32
# define PATH_PNG_DLL "libpng16-16.dll"
#else
# define PATH_PNG_DLL "libpng16.so"
#endif
# define PNGFUNC(x) png_ ## x
#ifdef ENABLE_ESCP_LOG
int png_do_log = ENABLE_ESCP_LOG;
static void
png_log(const char *fmt, ...)
{
va_list ap;
if (escp_do_log) {
va_start(ap, fmt);
pclog_ex(fmt, ap);
va_end(ap);
}
}
#else
#define png_log(fmt, ...)
#endif
static void
error_handler(png_structp arg, const char *str)
{
png_log("PNG: stream 0x%08lx error '%s'\n", arg, str);
}
static void
warning_handler(png_structp arg, const char *str)
{
png_log("PNG: stream 0x%08lx warning '%s'\n", arg, str);
}
/* Write the given image as an 8-bit GrayScale PNG image file. */
int
png_write_gray(wchar_t *fn, int inv, uint8_t *pix, int16_t w, int16_t h)
{
png_structp png = NULL;
png_infop info = NULL;
png_bytep row;
int16_t x, y;
FILE *fp;
/* Create the image file. */
fp = plat_fopen(fn, L"wb");
if (fp == NULL) {
/* Yes, this looks weird. */
if (fp == NULL)
png_log("PNG: file %ls could not be opened for writing!\n", fn);
else
error:
png_log("PNG: fatal error, bailing out, error = %i\n", errno);
if (png != NULL)
PNGFUNC(destroy_write_struct)(&png, &info);
if (fp != NULL)
(void)fclose(fp);
return(0);
}
/* Initialize PNG stuff. */
png = PNGFUNC(create_write_struct)(PNG_LIBPNG_VER_STRING, NULL,
error_handler, warning_handler);
if (png == NULL) {
png_log("PNG: create_write_struct failed!\n");
goto error;
}
info = PNGFUNC(create_info_struct)(png);
if (info == NULL) {
png_log("PNG: create_info_struct failed!\n");
goto error;
}
PNGFUNC(init_io)(png, fp);
PNGFUNC(set_IHDR)(png, info, w, h, 8, PNG_COLOR_TYPE_GRAY,
PNG_INTERLACE_NONE, PNG_COMPRESSION_TYPE_DEFAULT,
PNG_FILTER_TYPE_DEFAULT);
PNGFUNC(write_info)(png, info);
/* Create a buffer for one scanline of pixels. */
row = (png_bytep)malloc(PNGFUNC(get_rowbytes)(png, info));
/* Process all scanlines in the image. */
for (y = 0; y < h; y++) {
for (x = 0; x < w; x++) {
/* Copy the pixel data. */
if (inv)
row[x] = 255 - pix[(y * w) + x];
else
row[x] = pix[(y * w) + x];
}
/* Write image to the file. */
PNGFUNC(write_rows)(png, &row, 1);
}
/* No longer need the row buffer. */
free(row);
PNGFUNC(write_end)(png, NULL);
PNGFUNC(destroy_write_struct)(&png, &info);
/* Clean up. */
(void)fclose(fp);
return(1);
}
/* Write the given BITMAP-format image as an 8-bit RGBA PNG image file. */
void
png_write_rgb(wchar_t *fn, uint8_t *pix, int16_t w, int16_t h, uint16_t pitch, PALETTE palcol)
{
png_structp png = NULL;
png_infop info = NULL;
png_bytep* rows;
png_color palette[256];
FILE *fp;
int i;
/* Create the image file. */
fp = plat_fopen(fn, L"wb");
if (fp == NULL) {
png_log("PNG: File %ls could not be opened for writing!\n", fn);
error:
if (png != NULL)
PNGFUNC(destroy_write_struct)(&png, &info);
if (fp != NULL)
(void)fclose(fp);
return;
}
/* Initialize PNG stuff. */
png = PNGFUNC(create_write_struct)(PNG_LIBPNG_VER_STRING, NULL,
error_handler, warning_handler);
if (png == NULL) {
png_log("PNG: create_write_struct failed!\n");
goto error;
}
info = PNGFUNC(create_info_struct)(png);
if (info == NULL) {
png_log("PNG: create_info_struct failed!\n");
goto error;
}
/* Finalize the initing of png library */
PNGFUNC(init_io)(png, fp);
PNGFUNC(set_compression_level)(png, 9);
/* set other zlib parameters */
PNGFUNC(set_compression_mem_level)(png, 8);
PNGFUNC(set_compression_strategy)(png, PNG_Z_DEFAULT_STRATEGY);
PNGFUNC(set_compression_window_bits)(png, 15);
PNGFUNC(set_compression_method)(png, 8);
PNGFUNC(set_compression_buffer_size)(png, 8192);
PNGFUNC(set_IHDR)(png, info, w, h, 8, PNG_COLOR_TYPE_PALETTE,
PNG_INTERLACE_NONE, PNG_COMPRESSION_TYPE_DEFAULT,
PNG_FILTER_TYPE_DEFAULT);
for (i = 0; i < 256; i++) {
palette[i].red = palcol[i].r;
palette[i].green = palcol[i].g;
palette[i].blue = palcol[i].b;
}
PNGFUNC(set_PLTE)(png, info, palette, 256);
/* Create a buffer for scanlines of pixels. */
rows = (png_bytep *)malloc(sizeof(png_bytep) * h);
for (i = 0; i < h; i++) {
/* Create a buffer for this scanline. */
rows[i] = (pix + (i * pitch));
}
PNGFUNC(set_rows)(png, info, rows);
PNGFUNC(write_png)(png, info, 0, NULL);
/* Clean up. */
(void)fclose(fp);
PNGFUNC(destroy_write_struct)(&png, &info);
/* No longer need the row buffers. */
free(rows);
}