예제 #1
0
 inline char* readstring2(){
     //Read a Pascal string with 2 length bytes
     unsigned length = readint16();
     char *string = (char*) malloc(length+1);
     readbytes(string, length);
     string[length] = '\0';
     return string;
 }
예제 #2
0
unsigned char *
simage_pic_load(const char *filename,
int *width_ret,
int *height_ret,
int *numComponents_ret)
{
    int w, h, width, height, i, j, format;
    unsigned char palette[256][3];
    unsigned char * tmpbuf, * buffer, * ptr;

    FILE *fp = fopen(filename, "rb");
    if (!fp) return NULL;

    picerror = ERROR_NO_ERROR;

    fseek(fp, 2, SEEK_SET);
    if (!readint16(fp, &w))
    {
        picerror = ERROR_READING_HEADER;
        fclose(fp);
        return NULL;
    }

    fseek(fp, 4, SEEK_SET);
    if (!readint16(fp, &h))
    {
        picerror = ERROR_READING_HEADER;
        fclose(fp);
        return NULL;
    }

    width = w;
    height = h;

    if (width <= 0 || height <= 0)
    {
        fclose(fp);
        return NULL;
    }
    fseek(fp, 32, SEEK_SET);

    if (fread(&palette, 3, 256, fp) != 256)
    {
        picerror = ERROR_READING_PALETTE;
    }

    tmpbuf = new unsigned char [width];
    buffer = new unsigned char [3*width*height];
    if (tmpbuf == NULL || buffer == NULL)
    {
        picerror = ERROR_MEMORY;
        if (tmpbuf) free(tmpbuf);
        if (buffer) free(buffer);
        fclose(fp);
        return NULL;
    }
    ptr = buffer;
    for (i = 0; i < height; i++)
    {
        if (fread(tmpbuf, 1, width, fp) != (size_t) width)
        {
            picerror = ERROR_READ_ERROR;
            fclose(fp);
            if (tmpbuf) delete [] tmpbuf;
            if (buffer) delete [] buffer;
            buffer = NULL;
            width = height = 0;
            return NULL;
        }
        for (j = 0; j < width; j++)
        {
            int idx = tmpbuf[j];
            *ptr++ = palette[idx][0];
            *ptr++ = palette[idx][1];
            *ptr++ = palette[idx][2];
        }
    }
    format = 3;
    fclose(fp);

    *width_ret = width;
    *height_ret = height;
    *numComponents_ret = format;

    if (tmpbuf) delete [] tmpbuf;

    return buffer;
}