Exemple #1
0
/*
 * WriteBitmap:  Write bytes of given PDIB out to given file.
 *   Return FALSE on error.
 */
BOOL WriteBitmap(file_node *f, PDIB pdib, Options *options)
{
    int width, height, row, col, len, temp;
    BYTE *bits;
    BYTE *buf, *bufptr, byte;

    width = DibWidth(pdib);
    height = DibHeight(pdib);

    buf = (BYTE *) malloc(width * height);
    bufptr = buf;

    if (options->rotate)
    {
        for (col=0; col < width; col++)
        {
            bits = (BYTE *) DibPtr(pdib) + col;

            for (row = 0; row < height; row++)
            {
                *bufptr++ = *bits;
                bits += DibWidthBytes(pdib);
            }
        }
    }
    else
    {
        for (row=0; row < height; row++)
        {
            // Watch out--rows are 4-bytes aligned
            bits = (BYTE *) DibPtr(pdib) + row * DibWidthBytes(pdib);
            memcpy(bufptr, bits, width);
            bufptr += width;
        }
    }

    // Leave space for # of bytes

    if (options->compress)
    {
        len = WrapCompress((char *) buf, f->ptr + 5, width * height);
        if (len > 0)
        {
            byte = 1;
            // Save compressed length
            MappedFileWrite(f, &byte, 1);
            MappedFileWrite(f, &len, 4);
            f->ptr += len;
        }
    }
    else len = -1;

    if (len < 0)
    {
        byte = 0;
        MappedFileWrite(f, &byte, 1);
        temp = 0;
        MappedFileWrite(f, &temp, 4);

        // Buffer is incompressible; just write out bits themselves
        MappedFileWrite(f, buf, width * height);
    }

    free(buf);
    return True;
}
Exemple #2
0
PDIB DibOpenFile(LPSTR szFile)
{
    HFILE               fh;
    DWORD               dwLen;
    DWORD               dwBits;
    PDIB                pdib;
    LPVOID              p;
    long                i, width, height;
    BYTE                *row1, *row2, *temp, *bits;

    fh = open(szFile, O_BINARY | O_RDONLY);

    if (fh == -1)
        return NULL;

    pdib = DibReadBitmapInfo(fh);

    if (!pdib)
       return NULL;
    
    /* How much memory do we need to hold the DIB */

    dwBits = DibWidthBytes(pdib) * pdib->biHeight;
    dwLen  = pdib->biSize + DibPaletteSize(pdib) + dwBits;

    /* Can we get more memory? */

    p = realloc(pdib,dwLen);

    if (!p)
    {
       free(pdib);
       pdib = NULL;
    }
    else
    {
        pdib = (PDIB)p;
    }

    if (pdib)
    {
       /* read in the bits */
       bits = (LPBYTE) pdib + (UINT)pdib->biSize + DibPaletteSize(pdib);
       read(fh, bits, dwBits);
       
       /* Flip the bits to make bitmap top-down */
       width = DibWidthBytes(pdib);
       height = DibHeight(pdib);
       temp = (BYTE *) malloc(width);
       for (i=0; i < height / 2; i++)
       {
          row1 = bits + (height - i - 1) * width; 
          row2 = bits + i * width; 
          memcpy(temp, row1, (size_t) width);
          memcpy(row1, row2, (size_t) width);
          memcpy(row2, temp, (size_t) width);
       }
       free(temp);
    }

    close(fh);

    return pdib;
}