File: png.c

package info (click to toggle)
htp 1.19-8
  • links: PTS, VCS
  • area: main
  • in suites: bookworm, bullseye, sid, trixie
  • size: 1,272 kB
  • sloc: ansic: 6,645; perl: 204; makefile: 60
file content (88 lines) | stat: -rw-r--r-- 2,176 bytes parent folder | download | duplicates (7)
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
/*
//
// png.c
//
// PNG (Portable Network Graphics) support functions
//
// By Rev. Bob, 1999 - derived from gif.c by Jim Nelson
//
// gif.c Copyright (c) 1995-96 Jim Nelson.  Permission to distribute
// granted by the author.  No warranties are made on the fitness of this
// source code.
//
*/

#include "htp.h"

BOOL PngFormatFound(FILE *file)
{
    BYTE header[8];

    /* move to BOF */
    if(fseek(file, 0, SEEK_SET) != 0)
    {
        DEBUG_PRINT(("unable to seek to start of PNG file"));
        return FALSE;
    }

    /* read first eight bytes, looking for PNG header + version info */
    if(fread(header, 1, 8, file) != 8)
    {
        DEBUG_PRINT(("could not read PNG image file header"));
        return FALSE;
    }

    /* is this a PNG file? */
    if(memcmp(header, "\211PNG\r\n\032\n", 8) == 0)
    {
        if(fseek(file, 12, SEEK_SET) != 0)
        {
            DEBUG_PRINT(("unable to seek to IHDR block of PNG file"));
            return FALSE;
        }

        if(fread(header, 1, 4, file) != 4)
        {
            DEBUG_PRINT(("could not read PNG image IHDR signature"));
            return FALSE;
        }

        if(memcmp(header, "IHDR", 4) == 0)
        {
            /* PNG signature and IHDR block found, must be good */
            return TRUE;
        }
    }

    /* not a PNG file */
    return FALSE;
}

/* Modify parms here and in png.h - must use 4-byte integers! */
BOOL PngReadDimensions(FILE *file, DWORD *height, DWORD *width)
{

/* PNG uses MSB LSB (B3 B2 B1 B0) notation - most significant first */

    BYTE buff[8];

    /* move to the image size position (+16) in the file header */
    if(fseek(file, 16, SEEK_SET) != 0)
    {
        DEBUG_PRINT(("unable to seek to start of PNG file"));
        return FALSE;
    }

    /* read the width and height, byte by byte */
    /* this gets around machine endian problems while retaining the */
    /* fact that PNG uses MSB LSB (big-endian) notation */
    if(fread(buff, 1, 8, file) != 8)
    {
        return FALSE;
    }
    *width  = MAKE_DWORD(buff[0], buff[1], buff[2], buff[3]);
    *height = MAKE_DWORD(buff[4], buff[5], buff[6], buff[7]);

    return TRUE;
}