tools/pngtotex.c
static int generate_opaque_bitmap(Texture *texture)
{
    const unsigned int width = texture->width;
    const unsigned int height = texture->height;
    const unsigned int rowsize = (width+7) / 8;

    texture->opaque_bitmap = calloc(rowsize * height, 1);
    if (!texture->opaque_bitmap) {
        fprintf(stderr, "Out of memory generating opaque bitmap (%u bytes)\n",
                rowsize * height);
        return 0;
    }

    const uint8_t *src = texture->pixels;
    const uint32_t stride = texture->stride;
    uint8_t *dest = texture->opaque_bitmap;
    switch (texture->format) {
      case TEX_FORMAT_RGBA8888: {
        for (unsigned int y = 0; y < height; y++, src += stride*4, dest += rowsize) {
            for (unsigned int x = 0; x < width; x++) {
                dest[x/8] |= (src[x*4+3]==255 ? 1 : 0) << (x%8);
            }
        }
        break;
      }
      case TEX_FORMAT_PALETTE8_RGBA8888: {
        const uint32_t * const palette = texture->palette;
        for (unsigned int y = 0; y < height; y++, src += stride, dest += rowsize) {
            for (unsigned int x = 0; x < width; x++) {
                dest[x/8] |= ((palette[src[x]]>>24)==255 ? 1 : 0) << (x%8);
            }
        }
        break;
      }
      case TEX_FORMAT_A8: {
        for (unsigned int y = 0; y < height; y++, src += stride, dest += rowsize) {
            for (unsigned int x = 0; x < width; x++) {
                dest[x/8] |= (src[x]==255 ? 1 : 0) << (x%8);
            }
        }
        break;
      }
      default:
        fprintf(stderr, "Can't generate opaque bitmap for format %u\n",
                texture->format);
        free(texture->opaque_bitmap);
        texture->opaque_bitmap = NULL;
        return 0;
    }

    return 1;
}
/**
 * generate_opaque_bitmap:  Generate a bitmap of opaque pixels for the
 * given texture.  For textures with mipmaps, the bitmap reflects the
 * pixels in the base image.  The bitmap is stored in a separately-
 * allocated buffer.
 *
 * [Parameters]
 *     tex: Texture for which to generate an opaque pixel bitmap.
 * [Return value]
 *     True on success, false on error.
 */
Generate a bitmap of opaque pixels for the
given texture.  For textures with mipmaps, the bitmap reflects the
pixels in the base image.  The bitmap is stored in a separately
allocated buffer.

[Parameters]
tex: Texture for which to generate an opaque pixel bitmap.
[Return value]
True on success, false on error.
Generate a bitmap of opaque pixels for the given texture . For textures with mipmaps the bitmap reflects the pixels in the base image . The bitmap is stored in a separately allocated buffer . [ Parameters ] tex : Texture for which to generate an opaque pixel bitmap . [ Return value ] True on success false on error .
static int generate_opaque_bitmap(Texture *texture)
{
    const unsigned int width = texture->width;
    const unsigned int height = texture->height;
    const unsigned int rowsize = (width+7) / 8;
    texture->opaque_bitmap = calloc(rowsize * height, 1);
    if (!texture->opaque_bitmap) {
        fprintf(stderr, "Out of memory generating opaque bitmap (%u bytes)\n",
                rowsize * height);
        return 0;
    }
    const uint8_t *src = texture->pixels;
    const uint32_t stride = texture->stride;
    uint8_t *dest = texture->opaque_bitmap;
    switch (texture->format) {
      case TEX_FORMAT_RGBA8888: {
        for (unsigned int y = 0; y < height; y++, src += stride*4, dest += rowsize) {
            for (unsigned int x = 0; x < width; x++) {
                dest[x/8] |= (src[x*4+3]==255 ? 1 : 0) << (x%8);
            }
        }
        break;
      }
      case TEX_FORMAT_PALETTE8_RGBA8888: {
        const uint32_t * const palette = texture->palette;
        for (unsigned int y = 0; y < height; y++, src += stride, dest += rowsize) {
            for (unsigned int x = 0; x < width; x++) {
                dest[x/8] |= ((palette[src[x]]>>24)==255 ? 1 : 0) << (x%8);
            }
        }
        break;
      }
      case TEX_FORMAT_A8: {
        for (unsigned int y = 0; y < height; y++, src += stride, dest += rowsize) {
            for (unsigned int x = 0; x < width; x++) {
                dest[x/8] |= (src[x]==255 ? 1 : 0) << (x%8);
            }
        }
        break;
      }
      default:
        fprintf(stderr, "Can't generate opaque bitmap for format %u\n",
                texture->format);
        free(texture->opaque_bitmap);
        texture->opaque_bitmap = NULL;
        return 0;
    }
    return 1;
}
static int generate_opaque_bitmap ( Texture * texture ) { const unsigned int width = texture -> width ; const unsigned int height = texture -> height ; const unsigned int rowsize = ( width + 7 ) / 8 ; texture -> opaque_bitmap = calloc ( rowsize * height , 1 ) ; if ( ! texture -> opaque_bitmap ) { fprintf ( stderr , " \n " , rowsize * height ) ; return 0 ; } const uint8_t * src = texture -> pixels ; const uint32_t stride = texture -> stride ; uint8_t * dest = texture -> opaque_bitmap ; switch ( texture -> format ) { case TEX_FORMAT_RGBA8888 : { for ( unsigned int y = 0 ; y < height ; y ++ , src += stride * 4 , dest += rowsize ) { for ( unsigned int x = 0 ; x < width ; x ++ ) { dest [ x / 8 ] |= ( src [ x * 4 + 3 ] == 255 ? 1 : 0 ) << ( x % 8 ) ; } } break ; } case TEX_FORMAT_PALETTE8_RGBA8888 : { const uint32_t * const palette = texture -> palette ; for ( unsigned int y = 0 ; y < height ; y ++ , src += stride , dest += rowsize ) { for ( unsigned int x = 0 ; x < width ; x ++ ) { dest [ x / 8 ] |= ( ( palette [ src [ x ] ] >> 24 ) == 255 ? 1 : 0 ) << ( x % 8 ) ; } } break ; } case TEX_FORMAT_A8 : { for ( unsigned int y = 0 ; y < height ; y ++ , src += stride , dest += rowsize ) { for ( unsigned int x = 0 ; x < width ; x ++ ) { dest [ x / 8 ] |= ( src [ x ] == 255 ? 1 : 0 ) << ( x % 8 ) ; } } break ; } default : fprintf ( stderr , " \n " , texture -> format ) ; free ( texture -> opaque_bitmap ) ; texture -> opaque_bitmap = NULL ; return 0 ; } return 1 ; }
generate_opaque_bitmap:  Generate a bitmap of opaque pixels for the
 given texture.
generate_opaque_bitmap : Generate a bitmap of opaque pixels for the given texture .