Don't guess and make our own definitions for uint8_t, etc. Just error out if we can't find a suitable header file.

Fix to use fixed-size type so that this code works when sizeof(unsigned long) != 32. Thanks to Manish Singh.
This commit is contained in:
Carl Worth 2005-05-10 12:42:32 +00:00
parent c66ca528e0
commit 3253534a82
4 changed files with 45 additions and 25 deletions

View file

@ -1,3 +1,14 @@
2005-05-10 Carl Worth <cworth@cworth.org>
* src/cairo-wideint.h: Don't guess and make our own definitions
for uint8_t, etc. Just error out if we can't find a suitable
header file.
* src/cairo-png.c: (unpremultiply_data), (premultiply_data):
* test/read-png.c: (premultiply_data): Fix to use fixed-size type
so that this code works when sizeof(unsigned long) != 32. Thanks
to Manish Singh.
2005-05-10 Carl Worth <cworth@cworth.org>
* src/cairo-gstate.c: (_cairo_gstate_get_font_matrix):

View file

@ -44,11 +44,11 @@ unpremultiply_data (png_structp png, png_row_infop row_info, png_bytep data)
int i;
for (i = 0; i < row_info->rowbytes; i += 4) {
unsigned char *b = &data[i];
unsigned int pixel;
unsigned char alpha;
uint8_t *b = &data[i];
uint32_t pixel;
uint8_t alpha;
memcpy (&pixel, b, sizeof (unsigned int));
memcpy (&pixel, b, sizeof (uint32_t));
alpha = (pixel & 0xff000000) >> 24;
if (alpha == 0) {
b[0] = b[1] = b[2] = b[3] = 0;
@ -268,18 +268,18 @@ premultiply_data (png_structp png,
int i;
for (i = 0; i < row_info->rowbytes; i += 4) {
unsigned char *base = &data[i];
unsigned char blue = base[0];
unsigned char green = base[1];
unsigned char red = base[2];
unsigned char alpha = base[3];
unsigned long p;
uint8_t *base = &data[i];
uint8_t blue = base[0];
uint8_t green = base[1];
uint8_t red = base[2];
uint8_t alpha = base[3];
uint32_t p;
red = ((unsigned) red * (unsigned) alpha + 127) / 255;
green = ((unsigned) green * (unsigned) alpha + 127) / 255;
blue = ((unsigned) blue * (unsigned) alpha + 127) / 255;
p = (alpha << 24) | (red << 16) | (green << 8) | (blue << 0);
memcpy (base, &p, sizeof (unsigned long));
memcpy (base, &p, sizeof (uint32_t));
}
}

View file

@ -1,5 +1,5 @@
/*
* $Id: cairo-wideint.h,v 1.9 2005-04-07 22:00:46 cworth Exp $
* $Id: cairo-wideint.h,v 1.10 2005-05-10 19:42:32 cworth Exp $
*
* Copyright © 2004 Keith Packard
*
@ -45,12 +45,7 @@
#elif HAVE_SYS_INT_TYPES_H
# include <sys/int_types.h>
#else
typedef signed char int8_t;
typedef short int16_t;
typedef long int32_t;
typedef unsigned char uint8_t;
typedef unsigned short uint16_t;
typedef unsigned long uint32_t;
#error Cannot find definitions for fixed-width integral types (uint8_t, uint32_t, etc.)
#endif
/*

View file

@ -25,6 +25,20 @@
* Author: Carl D. Worth <cworth@isi.edu>
*/
#if HAVE_CONFIG_H
#include "config.h"
#endif
#if HAVE_STDINT_H
# include <stdint.h>
#elif HAVE_INTTYPES_H
# include <inttypes.h>
#elif HAVE_SYS_INT_TYPES_H
# include <sys/int_types.h>
#else
#error Cannot find definitions for fixed-width integral types (uint8_t, uint32_t, etc.)
#endif
#include <stdio.h>
#include <stdlib.h>
#include <png.h>
@ -40,18 +54,18 @@ premultiply_data (png_structp png,
int i;
for (i = 0; i < row_info->rowbytes; i += 4) {
unsigned char *base = &data[i];
unsigned char blue = base[0];
unsigned char green = base[1];
unsigned char red = base[2];
unsigned char alpha = base[3];
unsigned long p;
uint8_t *base = &data[i];
uint8_t blue = base[0];
uint8_t green = base[1];
uint8_t red = base[2];
uint8_t alpha = base[3];
uint32_t p;
red = ((unsigned) red * (unsigned) alpha + 127) / 255;
green = ((unsigned) green * (unsigned) alpha + 127) / 255;
blue = ((unsigned) blue * (unsigned) alpha + 127) / 255;
p = (alpha << 24) | (red << 16) | (green << 8) | (blue << 0);
memcpy (base, &p, sizeof (unsigned long));
memcpy (base, &p, sizeof (uint32_t));
}
}