/* * libpipi Proper image processing implementation library * Copyright (c) 2004-2008 Sam Hocevar * 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 #include #include #include #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) x = 0; else if(x >= img->width) x = img->width - 1; if(y < 0) y = 0; else if(y >= img->height) y = img->height - 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; }