SLUDGE: Refactor png specific functions in backdrop

This commit is contained in:
yinsimei 2017-05-28 20:51:21 +02:00 committed by Eugene Sandulenko
parent 9f7d301ef4
commit 6808650f3e

View File

@ -1006,118 +1006,128 @@ bool loadParallax(unsigned short v, unsigned short fracX, unsigned short fracY)
extern int viewportOffsetX, viewportOffsetY;
#if 0
bool loadImage(GLubyte *loadhere, int &picWidth, int &picHeight, int &realPicWidth, int &realPicHeight, Common::SeekableReadStream *stream, int x, int y, bool reserve) {
int t1, t2, n;
unsigned short c;
GLubyte *target;
int32_t transCol = reserve ? -1 : 63519;
bool loadPng(GLubyte *loadhere, int &picWidth, int &picHeight, int &realPicWidth, int &realPicHeight, Common::SeekableReadStream *stream, bool reserve) {
long file_pointer = stream->pos();
png_structp png_ptr;
png_infop info_ptr, end_info;
int fileIsPNG = true;
// Is this a PNG file?
char tmp[10];
size_t bytes_read = stream->read(tmp, 8);
if (bytes_read != 8 && stream->err()) {
debugOut("Reading error in loadHSI.\n");
}
if (png_sig_cmp((png_byte *) tmp, 0, 8)) {
// No, it's old-school HSI
fileIsPNG = false;
stream->seek(file_pointer, SEEK_SET);
picWidth = realPicWidth = get2bytes(stream);
picHeight = realPicHeight = get2bytes(stream);
} else {
// Read the PNG header
png_ptr = png_create_read_struct(PNG_LIBPNG_VER_STRING, NULL, NULL, NULL);
if (!png_ptr) {
return false;
}
info_ptr = png_create_info_struct(png_ptr);
if (!info_ptr) {
png_destroy_read_struct(&png_ptr, (png_infopp) NULL, (png_infopp) NULL);
return false;
}
end_info = png_create_info_struct(png_ptr);
if (!end_info) {
png_destroy_read_struct(&png_ptr, &info_ptr, (png_infopp)NULL);
return false;
}
png_init_io(png_ptr, fp); // Tell libpng which file to read
png_set_sig_bytes(png_ptr, 8); // 8 bytes already read
png_read_info(png_ptr, info_ptr);
png_uint_32 width, height;
int bit_depth, color_type, interlace_type, compression_type, filter_method;
png_get_IHDR(png_ptr, info_ptr, &width, &height, &bit_depth, &color_type, &interlace_type, &compression_type, &filter_method);
picWidth = realPicWidth = width;
picHeight = realPicHeight = height;
if (bit_depth < 8) png_set_packing(png_ptr);
png_set_expand(png_ptr);
if (color_type == PNG_COLOR_TYPE_GRAY || color_type == PNG_COLOR_TYPE_GRAY_ALPHA) png_set_gray_to_rgb(png_ptr);
if (bit_depth == 16) png_set_strip_16(png_ptr);
png_set_add_alpha(png_ptr, 0xff, PNG_FILLER_AFTER);
png_read_update_info(png_ptr, info_ptr);
png_get_IHDR(png_ptr, info_ptr, &width, &height, &bit_depth, &color_type, &interlace_type, &compression_type, &filter_method);
return false;
}
if (png_sig_cmp((png_byte *) tmp, 0, 8)) {
stream->seek(file_pointer, SEEK_SET);
return false;
}
png_ptr = png_create_read_struct(PNG_LIBPNG_VER_STRING, NULL, NULL, NULL);
if (!png_ptr) {
return false;
}
info_ptr = png_create_info_struct(png_ptr);
if (!info_ptr) {
png_destroy_read_struct(&png_ptr, (png_infopp) NULL, (png_infopp) NULL);
return false;
}
end_info = png_create_info_struct(png_ptr);
if (!end_info) {
png_destroy_read_struct(&png_ptr, &info_ptr, (png_infopp)NULL);
return false;
}
png_init_io(png_ptr, stream); // Tell libpng which file to read
png_set_sig_bytes(png_ptr, 8); // 8 bytes already read
png_read_info(png_ptr, info_ptr);
png_uint_32 width, height;
int bit_depth, color_type, interlace_type, compression_type, filter_method;
png_get_IHDR(png_ptr, info_ptr, &width, &height, &bit_depth, &color_type, &interlace_type, &compression_type, &filter_method);
picWidth = realPicWidth = width;
picHeight = realPicHeight = height;
if (bit_depth < 8) png_set_packing(png_ptr);
png_set_expand(png_ptr);
if (color_type == PNG_COLOR_TYPE_GRAY || color_type == PNG_COLOR_TYPE_GRAY_ALPHA) png_set_gray_to_rgb(png_ptr);
if (bit_depth == 16) png_set_strip_16(png_ptr);
png_set_add_alpha(png_ptr, 0xff, PNG_FILLER_AFTER);
png_read_update_info(png_ptr, info_ptr);
png_get_IHDR(png_ptr, info_ptr, &width, &height, &bit_depth, &color_type, &interlace_type, &compression_type, &filter_method);
if (reserve) {
if (! resizeBackdrop(realPicWidth, realPicHeight)) return false;
if (!resizeBackdrop(realPicWidth, realPicHeight)) return false;
}
unsigned char *row_pointers[realPicHeight];
for (int i = 0; i < realPicHeight; i++)
row_pointers[i] = loadhere + 4 * i * picWidth;
png_read_image(png_ptr, (png_byte **) row_pointers);
png_read_end(png_ptr, NULL);
png_destroy_read_struct(&png_ptr, &info_ptr, &end_info);
}
bool loadByteArray(GLubyte *loadhere, int &picWidth, int &picHeight, int &realPicWidth, int &realPicHeight, Common::SeekableReadStream *stream, bool reserve) {
int32_t transCol = reserve ? -1 : 63519;
int t1, t2, n;
unsigned short c;
picWidth = realPicWidth = get2bytes(stream);
picHeight = realPicHeight = get2bytes(stream);
if (reserve) {
if (!resizeBackdrop(realPicWidth, realPicHeight)) return false;
}
for (t2 = 0; t2 < realPicHeight; t2 ++) {
t1 = 0;
while (t1 < realPicWidth) {
c = (unsigned short) get2bytes(stream);
if (c & 32) {
n = getch(stream) + 1;
c -= 32;
} else {
n = 1;
}
while (n --) {
GLubyte *target = loadhere + 4 * picWidth * t2 + t1 * 4;
if (c == transCol || c == 2015) {
target[0] = (GLubyte)0;
target[1] = (GLubyte)0;
target[2] = (GLubyte)0;
target[3] = (GLubyte)0;
} else {
target[0] = (GLubyte)redValue(c);
target[1] = (GLubyte)greenValue(c);
target[2] = (GLubyte)blueValue(c);
target[3] = (GLubyte)255;
}
t1++;
}
}
}
}
bool loadImage(GLubyte *loadhere, int &picWidth, int &picHeight, int &realPicWidth, int &realPicHeight, Common::SeekableReadStream *stream, int x, int y, bool reserve) {
if (!loadPng(loadhere, picWidth, picHeight, realPicWidth, realPicHeight, stream, reserve)) {
if (!loadByteArray(loadhere, picWidth, picHeight, realPicWidth, realPicHeight, stream, reserve)) {
return false;
}
}
if (x == IN_THE_CENTRE) x = (sceneWidth - realPicWidth) >> 1;
if (y == IN_THE_CENTRE) y = (sceneHeight - realPicHeight) >> 1;
if (x < 0 || x + realPicWidth > sceneWidth || y < 0 || y + realPicHeight > sceneHeight) return false;
if (fileIsPNG) {
unsigned char *row_pointers[realPicHeight];
for (int i = 0; i < realPicHeight; i++)
row_pointers[i] = backdropTexture + 4 * i * picWidth;
png_read_image(png_ptr, (png_byte **)row_pointers);
png_read_end(png_ptr, NULL);
png_destroy_read_struct(&png_ptr, &info_ptr, &end_info);
} else {
for (t2 = 0; t2 < realPicHeight; t2 ++) {
t1 = 0;
while (t1 < realPicWidth) {
c = (unsigned short) get2bytes(fp);
if (c & 32) {
n = fgetc(fp) + 1;
c -= 32;
} else {
n = 1;
}
while (--n) {
target = backdropTexture + 4 * picWidth * t2 + t1 * 4;
if (c == transCol || c == 2015) {
target[0] = (GLubyte) 0;
target[1] = (GLubyte) 0;
target[2] = (GLubyte) 0;
target[3] = (GLubyte) 0;
} else {
target[0] = (GLubyte) redValue(c);
target[1] = (GLubyte) greenValue(c);
target[2] = (GLubyte) blueValue(c);
target[3] = (GLubyte) 255;
}
t1++;
}
}
}
}
return true;
}
@ -1140,7 +1150,7 @@ void makeGlArray(GLuint &tmpTex, const GLubyte *texture, int picWidth, int picHe
void renderToTexture(GLuint tmpTex, int x, int y, int picWidth, int picHeight, int realPicWidth, int realPicHeight) {
GLfloat texCoordW = 1.0;
GLfloat texCoordH = 1.0;
if (! NPOT_textures) {
if (!NPOT_textures) {
picWidth = getNextPOT(picWidth);
picHeight = getNextPOT(picHeight);
texCoordW = ((double)realPicWidth) / picWidth;
@ -1151,7 +1161,7 @@ void renderToTexture(GLuint tmpTex, int x, int y, int picWidth, int picHeight, i
float btx2;
float bty1;
float bty2;
if (! NPOT_textures) {
if (!NPOT_textures) {
btx1 = backdropTexW * x / sceneWidth;
btx2 = backdropTexW * (x + realPicWidth) / sceneWidth;
bty1 = backdropTexH * y / sceneHeight;