|
- /*
- * libpipi Proper image processing implementation library
- * Copyright (c) 2004-2008 Sam Hocevar <sam@zoy.org>
- * All Rights Reserved
- *
- * $Id$
- *
- * This library is free software. It comes without any warranty, to
- * the extent permitted by applicable law. You can redistribute it
- * and/or modify it under the terms of the Do What The Fuck You Want
- * To Public License, Version 2, as published by Sam Hocevar. See
- * http://sam.zoy.org/wtfpl/COPYING for more details.
- */
-
- /*
- * pixels.c: pixel-level image manipulation
- */
-
- #include "config.h"
- #include "common.h"
-
- #include <stdio.h>
- #include <stdlib.h>
- #include <string.h>
-
- #include <math.h>
-
- #include "pipi.h"
- #include "pipi_internals.h"
-
- #define C2I(p) (pow(((double)p)/255., 2.2))
- #define I2C(p) ((int)255.999*pow(((double)p), 1./2.2))
-
- int pipi_getgray(pipi_image_t const *img, int x, int y, int *g)
- {
- if(x < 0 || y < 0 || x >= img->width || y >= img->height)
- {
- *g = 255;
- return -1;
- }
-
- *g = (unsigned char)img->pixels[y * img->pitch + x * img->channels + 1];
-
- return 0;
- }
-
- int pipi_getpixel(pipi_image_t const *img,
- int x, int y, double *r, double *g, double *b)
- {
- uint8_t *pixel;
-
- if(x < 0 || y < 0 || x >= img->width || y >= img->height)
- {
- *r = *g = *b = 1.;
- return -1;
- }
-
- pixel = img->pixels + y * img->pitch + x * img->channels;
-
- *b = C2I((unsigned char)pixel[0]);
- *g = C2I((unsigned char)pixel[1]);
- *r = C2I((unsigned char)pixel[2]);
-
- return 0;
- }
-
- int pipi_setpixel(pipi_image_t *img, int x, int y, double r, double g, double b)
- {
- uint8_t *pixel;
-
- if(x < 0 || y < 0 || x >= img->width || y >= img->height)
- return -1;
-
- pixel = img->pixels + y * img->pitch + x * img->channels;
-
- pixel[0] = I2C(b);
- pixel[1] = I2C(g);
- pixel[2] = I2C(r);
-
- return 0;
- }
|