Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

engine: common: imagelib: add simple decoder/encoder for 8-bit RGB/RGBA PNG images #48

Merged
merged 1 commit into from
Jun 29, 2019

Conversation

nekonomicon
Copy link
Member

No description provided.

buf_p += sizeof( png_sign );

// get IHDR chunk length
chunk_len = *buf_p++ << 24;
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This may cause crash on ARM due to unaligned access.

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

A note on my prev comment: reading and writing on ARM is possible only if pointer aligned by 32 bits. So moving pointer by one or two bytes will cause a crash.

I think you can read 32-bits and swap it for big endian, if needed. Engine already have these handy macros: https://github.com/FWGS/xash3d-fwgs/blob/master/common/xash3d_types.h#L88

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I need swap bytes on little endian, so this macros useless for me.

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Seems I must use ntohl for little endian,
https://www.w3.org/TR/PNG/#7Integers-and-byte-order

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

It's better to use network conversions here, but keep in mind, that we're still not linking to ws32 on Windows yet directly. So it will fail Windows builds.

@@ -198,15 +198,15 @@ extern "C" {
typedef unsigned long mz_ulong;

/* mz_free() internally uses the MZ_FREE() macro (which by default calls free() unless you've modified the MZ_MALLOC macro) to release a block allocated from the heap. */
void mz_free(void *p);
static void mz_free(void *p);
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Instead of re-defining them as static, you could define MINIZ_HEADER_FILE_ONLY before including miniz.
miniz implementation already defined in filesystem.c, but if you want, you can move it to dummy file.

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I want to refactor this library anyway.

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I have generated it as a single-header in hope that it will not change, except obvious updates. :)

buffer = (byte *)Mem_Calloc( host.imagepool, outsize );

// write PNG header
buffer[0] = 0x89;
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think, you can memcpy png_sign here, as well as other signatures(IDAT, IHDR) or fill structure and then copy it at once.

@noodlecollie
Copy link
Contributor

Given the STB libraries are already utilised in the MainUI project, would it not make sense to use stb_png for this implementation? I used it for PNG reading in my fork and I'm pretty sure it was less complex than this.

@a1batross
Copy link
Member

STB library in MainUI C++ is optional, used as a last hope when we can't use nor Win32 APIs nor FreeType, as it only roughly renders TrueType font(doesn't have hinting). And it doesn't changed at all.

I'm quite unhappy with stb_image and stb_image_write library code, as it bloated(~7.5+1.5k loc) and implements different image formats instead of splitting it to small libraries one per image format.

It's not the same as PNG loader. Making stb_image smaller(in lines of code) means forking it and harder updates and testing. That's why I'm also unhappy with @nekonomicon's decision to make changes in miniz library.

@nekonomicon
Copy link
Member Author

I have a bad experience with stb_image. This library has a bad performance=\

@noodlecollie
Copy link
Contributor

OK, I guess it might be better not to use STB then. Once this goes in I'll replace the PNG implementation in my fork with this instead.

@a1batross
Copy link
Member

I'm OK to merge it.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

3 participants