Fredrik Lundh is sharing code with you

Bitbucket is a code hosting site. Unlimited public and private repositories. Free for small teams.

Don't show this again

effbot / pil-2009-raclette http://effbot.org/zone/pil-index.htm

Work repository for PIL 1.1.7 and beyond. The "default" branch should be fairly solid, the other branches less so. For production use, see the pil-117 repository.

Clone this repository (size: 1.6 MB): HTTPS / SSH
hg clone https://bitbucket.org/effbot/pil-2009-raclette
hg clone ssh://hg@bitbucket.org/effbot/pil-2009-raclette

pil-2009-raclette / libImaging / HexDecode.c

/*
 * The Python Imaging Library.
 * $Id: HexDecode.c 2134 2004-10-06 08:55:20Z fredrik $
 *
 * decoder for hex encoded image data
 *
 * history:
 *      96-05-16 fl     Created
 *
 * Copyright (c) Fredrik Lundh 1996.
 * Copyright (c) Secret Labs AB 1997.
 *
 * See the README file for information on usage and redistribution.
 */


#include "Imaging.h"

#define HEX(v) ((v >= '0' && v <= '9') ? v - '0' :\
                (v >= 'a' && v <= 'f') ? v - 'a' + 10 :\
                (v >= 'A' && v <= 'F') ? v - 'A' + 10 : -1)

int
ImagingHexDecode(Imaging im, ImagingCodecState state, UINT8* buf, int bytes)
{
    UINT8* ptr;
    int a, b;

    ptr = buf;

    for (;;) {

        if (bytes < 2)
            return ptr - buf;

        a = HEX(ptr[0]);
        b = HEX(ptr[1]);

        if (a < 0 || b < 0) {

            ptr++;
            bytes--;

        } else {

            ptr += 2;
            bytes -= 2;

            state->buffer[state->x] = (a<<4) + b;

            if (++state->x >= state->bytes) {

                /* Got a full line, unpack it */
                state->shuffle((UINT8*) im->image[state->y], state->buffer,
                               state->xsize);

                state->x = 0;

                if (++state->y >= state->ysize) {
                    /* End of file (errcode = 0) */
                    return -1;
                }
            }

        }
    }
}