Changeset 2605 for libpipi/trunk/pipi/pixels.c
- Timestamp:
- Jul 29, 2008, 1:01:29 AM (14 years ago)
- File:
-
- 1 edited
Legend:
- Unmodified
- Added
- Removed
-
libpipi/trunk/pipi/pixels.c
r2602 r2605 32 32 #define I2C(p) ((int)255.999*pow(((double)p), 1./2.2)) 33 33 34 int pipi_getgray(pipi_image_t const *img, int x, int y, int *g) 34 /* Return a direct pointer to an image's pixels. */ 35 pipi_pixels_t *pipi_getpixels(pipi_image_t *img, pipi_format_t type) 35 36 { 36 if(x < 0 || y < 0 || x >= img->width || y >= img->height) 37 size_t bytes = 0; 38 int x, y, i; 39 40 if(type < 0 || type >= PIPI_PIXELS_MAX) 41 return NULL; 42 43 if(img->last_modified == type) 44 return &img->p[type]; 45 46 /* Allocate pixels if necessary */ 47 if(!img->p[type].pixels) 37 48 { 38 *g = 255; 39 return -1; 49 switch(type) 50 { 51 case PIPI_PIXELS_RGBA32: 52 bytes = img->w * img->h * 4 * sizeof(uint8_t); 53 break; 54 case PIPI_PIXELS_RGBA_F: 55 bytes = img->w * img->h * 4 * sizeof(float); 56 break; 57 default: 58 return NULL; 59 } 60 61 img->p[type].pixels = malloc(bytes); 40 62 } 41 63 42 *g = (unsigned char)img->pixels[y * img->pitch + x * img->channels + 1]; 64 /* Convert pixels */ 65 if(img->last_modified == PIPI_PIXELS_RGBA32 66 && type == PIPI_PIXELS_RGBA_F) 67 { 68 uint8_t *src = (uint8_t *)img->p[PIPI_PIXELS_RGBA32].pixels; 69 float *dest = (float *)img->p[type].pixels; 43 70 44 return 0; 71 for(y = 0; y < img->h; y++) 72 for(x = 0; x < img->w; x++) 73 for(i = 0; i < 4; i++) 74 dest[4 * (y * img->w + x) + i] 75 = C2I(src[4 * (y * img->w + x) + i]); 76 } 77 else if(img->last_modified == PIPI_PIXELS_RGBA_F 78 && type == PIPI_PIXELS_RGBA32) 79 { 80 float *src = (float *)img->p[PIPI_PIXELS_RGBA_F].pixels; 81 uint8_t *dest = (uint8_t *)img->p[type].pixels; 82 83 for(y = 0; y < img->h; y++) 84 for(x = 0; x < img->w; x++) 85 for(i = 0; i < 4; i++) 86 dest[4 * (y * img->w + x) + i] 87 = I2C(src[4 * (y * img->w + x) + i]); 88 } 89 else 90 { 91 memset(img->p[type].pixels, 0, bytes); 92 } 93 94 img->last_modified = type; 95 96 return &img->p[type]; 45 97 } 46 98 47 int pipi_getpixel(pipi_image_t const *img,48 int x, int y, double *r, double *g, double *b)49 {50 uint8_t *pixel;51 52 if(x < 0) x = 0;53 else if(x >= img->width) x = img->width - 1;54 55 if(y < 0) y = 0;56 else if(y >= img->height) y = img->height - 1;57 58 pixel = img->pixels + y * img->pitch + x * img->channels;59 60 *b = C2I((unsigned char)pixel[0]);61 *g = C2I((unsigned char)pixel[1]);62 *r = C2I((unsigned char)pixel[2]);63 64 return 0;65 }66 67 int pipi_setpixel(pipi_image_t *img, int x, int y, double r, double g, double b)68 {69 uint8_t *pixel;70 71 if(x < 0 || y < 0 || x >= img->width || y >= img->height)72 return -1;73 74 pixel = img->pixels + y * img->pitch + x * img->channels;75 76 pixel[0] = I2C(b);77 pixel[1] = I2C(g);78 pixel[2] = I2C(r);79 80 return 0;81 }82
Note: See TracChangeset
for help on using the changeset viewer.