summaryrefslogtreecommitdiffhomepage
path: root/Public/SDK/SystemKit/MePNG.cpp
blob: d43393e0e618769130c5c4185e0ec34be23488cc (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
/*
 *	========================================================
 *
 *	h-core
 * 	Copyright Mahrouss Logic, all rights reserved.
 *
 * 	========================================================
 */

#include <SystemKit/MePNG.hpp>
#include <NewKit/ErrorID.hpp>
#include <NewKit/Utils.hpp>
#include <NewKit/Ref.hpp>

static const char* png_error_to_str(const int err);

enum
{
    kPngErrAdler,
    kPngErrCrc,
    kPngErrCompr,
    kPngUnknown,
};

typedef void* PNGImage;

static const char* png_error_to_str(const int err)
{
    switch (err)
    {
        case kPngErrAdler:
            return "PNG: Bad adler32.";
        case kPngErrCompr:
            return "PngLin: Bad compression.";
        case kPngErrCrc:
            return "PNG: Bad CRC32.";
        case kPngUnknown:
        default:
            return "PNG: Error while loading image.";
    }

    return "PNG: Unknown error.";
}

class PngHeader final
{
public:
    UInt8 Mag1;
    Char* Mag2;
    UInt16 DosDetect;
    UInt16 DosEOF;
    UInt16 UnixLF;

};

#define kIHDRMag "IHDR"
#define kIDATMag "IDAT"
#define kIENDMag "IEND"

MeFilePtr png_open_file(const char* path)
{
    MeFilePtr thePng = new MeFile(path);

    thePng->SetMIME("image/png");
    thePng->Rewind();

    return thePng;
}

static h-core::Ref<PngHeader> png_read_header(MeFilePtr thePng)
{
    if (thePng)
    {
        h-core::Ref<PngHeader> header;

        header.Leak().Mag1 = *(UInt8*)thePng->Read(sizeof(UInt8));

        h-core::rt_copy_memory((h-core::voidPtr)header.Leak().Mag2,
                              (h-core::voidPtr)thePng->Read(h-core::string_length("PNG")),
                                                              h-core::string_length("PNG"));

        header.Leak().DosDetect = *(UInt16*)thePng->Read(sizeof(UInt16));
        header.Leak().DosEOF = *(UInt16*)thePng->Read(sizeof(UInt16));
        header.Leak().UnixLF = *(UInt16*)thePng->Read(sizeof(UInt16));

        return header;
    }

    return h-core::Ref<PngHeader>{ };
}