Skip to content

Commit

Permalink
Merge pull request #4 from MotoLegacy/cypress/stbi-support
Browse files Browse the repository at this point in the history
Move from libpng to STBI
  • Loading branch information
fhomolka authored Aug 26, 2024
2 parents d6f2be1 + a3e7ea3 commit 3322df8
Show file tree
Hide file tree
Showing 10 changed files with 9,836 additions and 381 deletions.
6 changes: 3 additions & 3 deletions .github/workflows/create-artifacts.yml
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@ jobs:
sudo apt install -y cmake
- name: Install 32-bit libraries
run: |
sudo apt install -y g++-multilib zlib1g:i386 libsnappy-dev:i386 liblz4-1:i386 libpng-dev:i386
sudo apt install -y g++-multilib zlib1g:i386 libsnappy-dev:i386 liblz4-1:i386
- name: Checkout
uses: actions/checkout@v4
- name: Build 64 bit
Expand Down Expand Up @@ -66,7 +66,7 @@ jobs:
runs-on: windows-2019
env:
VCPKG_VERSION: 8eb57355a4ffb410a2e94c07b4dca2dffbee8e50
vcpkg_packages: zlib libpng
vcpkg_packages: zlib
strategy:
matrix:
config:
Expand All @@ -88,7 +88,7 @@ jobs:
- name: Run vcpkg
uses: lukka/run-vcpkg@v7
with:
vcpkgArguments: zlib libpng
vcpkgArguments: zlib
vcpkgDirectory: '${{ github.workspace }}\vcpkg'
appendedCacheKey: ${{ matrix.config.vcpkg_triplet }}
vcpkgGitCommitId: ${{ env.VCPKG_VERSION }}
Expand Down
9 changes: 3 additions & 6 deletions CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -2,10 +2,7 @@ cmake_minimum_required(VERSION 2.6)

project(QPAKMAN CXX)

find_package(PNG REQUIRED)

add_definitions(${PNG_DEFINITIONS})
include_directories(${PNG_INCLUDE_DIRS} ${ZLIB_INCLUDE_DIRS})
include_directories(${ZLIB_INCLUDE_DIRS})

if(UNIX)
add_definitions(-DUNIX)
Expand All @@ -17,10 +14,10 @@ endif()

add_executable(qpakman
arc_spec.cc u_assert.cc u_file.cc u_util.cc im_color.cc im_image.cc
im_gen.cc im_mip.cc im_png.cc im_tex.cc archive.cc pakfile.cc main.cc)
im_gen.cc im_mip.cc im_format.cc im_tex.cc archive.cc pakfile.cc main.cc)

set_property(TARGET qpakman PROPERTY COMPILE_FLAGS -fno-rtti)

target_link_libraries(qpakman -lm ${PNG_LIBRARIES})
target_link_libraries(qpakman -lm)

install(TARGETS qpakman RUNTIME DESTINATION bin)
30 changes: 5 additions & 25 deletions arc_spec.cc
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,7 @@
#include "im_color.h"
#include "im_image.h"
#include "im_mip.h"
#include "im_png.h"
#include "im_format.h"
#include "q1_structs.h"


Expand All @@ -42,7 +42,7 @@ static int StorePOP(FILE *fp, const char *lump)
{
printf(" Converting POP back to LMP...\n");

rgb_image_c *img = PNG_Load(fp);
rgb_image_c *img = Image_Load(fp);

if (! img)
{
Expand Down Expand Up @@ -110,21 +110,11 @@ static int AnalysePOP(int entry, const char *path)
img->PixelAt(x, y) = COL_ReadPalette(pop[y*16+x]);
}


FILE *fp = fopen(png_name, "wb");
if (! fp)
{
printf("FAILURE: cannot create output file: %s\n\n", png_name);
delete img;
return ARCSP_Failed;
}

bool result = PNG_Save(fp, img);
bool result = Image_Save(png_name, img);

if (! result)
printf("FAILURE: error while writing PNG file\n\n");

fclose(fp);
delete img;

// POP image is merely additional to the POP lmp
Expand Down Expand Up @@ -320,7 +310,7 @@ static int StoreLMP(FILE *fp, const char *lump)
{
printf(" Converting PNG graphic to LMP...\n");

rgb_image_c *img = PNG_Load(fp);
rgb_image_c *img = Image_Load(fp);

if (! img)
{
Expand Down Expand Up @@ -423,21 +413,11 @@ static int ExtractLMP(int entry, const char *path)

delete[] pixels;


FILE *fp = fopen(png_name, "wb");
if (! fp)
{
printf("FAILURE: cannot create output file: %s\n\n", png_name);
delete img;
return ARCSP_Failed;
}

bool result = PNG_Save(fp, img);
bool result = Image_Save(png_name, img);

if (! result)
printf("FAILURE: error while writing PNG file\n\n");

fclose(fp);
delete img;


Expand Down
8 changes: 4 additions & 4 deletions im_color.cc
Original file line number Diff line number Diff line change
Expand Up @@ -318,9 +318,9 @@ void COL_SetFullBright(bool enable)

u32_t COL_ReadPalette(byte pix)
{
byte R = palette[pix*3 + 0];
byte R = palette[pix*3 + 2];
byte G = palette[pix*3 + 1];
byte B = palette[pix*3 + 2];
byte B = palette[pix*3 + 0];

return MAKE_RGB(R, G, B);
}
Expand All @@ -331,9 +331,9 @@ u32_t COL_ReadPalWithTrans(byte pix)
if (pix == transparent_color)
return MAKE_RGBA(0, 0, 0, ALPHA_TRANS);

byte R = palette[pix*3 + 0];
byte R = palette[pix*3 + 2];
byte G = palette[pix*3 + 1];
byte B = palette[pix*3 + 2];
byte B = palette[pix*3 + 0];

return MAKE_RGB(R, G, B);
}
Expand Down
92 changes: 92 additions & 0 deletions im_format.cc
Original file line number Diff line number Diff line change
@@ -0,0 +1,92 @@
//------------------------------------------------------------------------
// Image Handling
//------------------------------------------------------------------------
//
// Copyright (c) 2008 Andrew J Apted
// Copyright (c) 2015 Anton Leontiev
// Copyright (c) 2024 Ivy Bowling
//
// This program is free software; you can redistribute it and/or
// modify it under the terms of the GNU General Public License
// as published by the Free Software Foundation; either version 2
// of the License, or (at your option) any later version.
//
// This program is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
// GNU General Public License for more details.
//
//------------------------------------------------------------------------

#include "headers.h"
#include "main.h"

#include "im_format.h"

#define STB_IMAGE_IMPLEMENTATION
#define STBI_ONLY_JPEG
#define STBI_ONLY_PNG
#define STBI_ONLY_BMP
#define STBI_ONLY_TGA
#include "stb_image.h"

#define STB_IMAGE_WRITE_IMPLEMENTATION
#include "stb_image_write.h"

rgb_image_c *Image_Load(FILE *fp)
{
rgb_image_c * img = 0;

int image_width;
int image_height;
int bits_per_pixel;

byte * image_pixels = stbi_load_from_file(fp, &image_width, &image_height, &bits_per_pixel, 4);

if (image_pixels == NULL) {
printf("Image_Load : Could not load image, STBI returns: \"%s\" !\n", stbi_failure_reason());
goto failed;
}

img = new rgb_image_c(image_width, image_height);

img->pixels = (u32_t*)image_pixels;

// cypress (25 aug 2024) -- "is_solid" is completely unused, though seems out of scope
// to purge. leaving it here.
img->is_solid = true;

return img;

/* -AJA- Normally I don't like gotos. In this situation where there
* are lots of points of possible failure and a growing set of
* things to be undone, it makes for nicer code.
*/
failed:

if (img)
{
delete img;
img = 0;
}

return 0;
}


bool Image_Save(const char *filename, rgb_image_c *img, int compress)
{
int result = stbi_write_png(filename, img->width, img->height, 4, img->pixels, 4 * img->width);

// 1 = success
if (!result)
{
printf("Image_Save : Error saving PNG image, STBIW returns: \"%s\" !\n", stbi_failure_reason());
return false;
}

return true;
}

//--- editor settings ---
// vi:ts=2:sw=2:expandtab
16 changes: 8 additions & 8 deletions im_png.h → im_format.h
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
//------------------------------------------------------------------------
// PNG Image Handling
// Image Handling
//------------------------------------------------------------------------
//
// Copyright (c) 2008 Andrew J Apted
Expand All @@ -16,24 +16,24 @@
//
//------------------------------------------------------------------------

#ifndef __IMAGE_PNG_H__
#define __IMAGE_PNG_H__
#ifndef __IMAGE_FORMAT_H__
#define __IMAGE_FORMAT_H__

#include "headers.h"

#include "im_image.h"

rgb_image_c * PNG_Load(FILE *fp);
// loads the given PNG image. Returns 0 if something went wrong.
// The image will be RGB or RGBA (never paletted).
rgb_image_c * Image_Load(FILE *fp);
// loads the given PNG/JPG/TGA/BMP image. Returns 0 if something went wrong.
// The image will be BGR or BGRA (never paletted).

bool PNG_Save(FILE *fp, rgb_image_c *img, int compress = 3);
bool Image_Save(const char *filename, rgb_image_c *img, int compress = 3);
// saves the image (in PNG format) to the given file. The compression
// level should be between 1 (Z_BEST_SPEED) and 9 (Z_BEST_COMPRESSION).
// Returns false if failed to save.
// NOTE: Alpha channel is IGNORED.

#endif /* __IMAGE_PNG_H__ */
#endif /* __IMAGE_FORMAT_H__ */

//--- editor settings ---
// vi:ts=2:sw=2:expandtab
59 changes: 9 additions & 50 deletions im_mip.cc
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@
#include "im_color.h"
#include "im_image.h"
#include "im_mip.h"
#include "im_png.h"
#include "im_format.h"
#include "pakfile.h"
#include "q1_structs.h"

Expand All @@ -41,31 +41,16 @@ std::map<std::string, int> all_lump_names;
rgb_image_c *MIP_LoadImage(const char *filename)
{
// Note: extension checks are case-insensitive

if (CheckExtension(filename, "bmp") || CheckExtension(filename, "tga") ||
CheckExtension(filename, "gif") || CheckExtension(filename, "pcx") ||
CheckExtension(filename, "pgm") || CheckExtension(filename, "ppm") ||
CheckExtension(filename, "tif") || CheckExtension(filename, "tiff"))
// cypress (25 aug 2024) -- add more image support.
if (!CheckExtension(filename, "bmp") && !CheckExtension(filename, "png") &&
!CheckExtension(filename, "tga") && !CheckExtension(filename, "jpg") &&
!CheckExtension(filename, "jpeg"))
{
printf("FAILURE: Unsupported image format\n");
return NULL;
}

if (CheckExtension(filename, "jpg") ||
CheckExtension(filename, "jpeg"))
{
// TODO: JPEG
printf("FAILURE: JPEG image format not supported yet\n");
return NULL;
}

if (! CheckExtension(filename, "png"))
{
printf("FAILURE: Not an image file\n");
return NULL;
}


FILE *fp = fopen(filename, "rb");

if (! fp)
Expand All @@ -74,7 +59,7 @@ rgb_image_c *MIP_LoadImage(const char *filename)
return NULL;
}

rgb_image_c * img = PNG_Load(fp);
rgb_image_c * img = Image_Load(fp);

fclose(fp);

Expand Down Expand Up @@ -531,23 +516,11 @@ static bool Do_SaveImage(rgb_image_c *img, const char *lump_name, bool fullbrigh
return false;
}

FILE *fp = fopen(filename, "wb");

if (! fp)
{
printf("FAILURE: cannot create output file: %s\n\n", filename);

StringFree(filename);
return false;
}

bool result = PNG_Save(fp, img);
bool result = Image_Save(filename, img);

if (! result)
printf("FAILURE: error while writing PNG file\n\n");

fclose(fp);

StringFree(filename);

return result;
Expand Down Expand Up @@ -861,21 +834,7 @@ bool MIP_DecodeWAL(int entry, const char *filename)


// TODO: transparent bits and/or SKY


FILE *fp = fopen(filename, "wb");

if (! fp)
{
printf("FAILURE: cannot create output file: %s\n\n", filename);

delete img;
return false;
}

bool result = PNG_Save(fp, img);

fclose(fp);
bool result = Image_Save(filename, img);

delete img;

Expand Down
Loading

0 comments on commit 3322df8

Please sign in to comment.