| 1 | /* |
|---|
| 2 | * libpipi Proper image processing implementation library |
|---|
| 3 | * Copyright (c) 2004-2008 Sam Hocevar <sam@zoy.org> |
|---|
| 4 | * All Rights Reserved |
|---|
| 5 | * |
|---|
| 6 | * $Id$ |
|---|
| 7 | * |
|---|
| 8 | * This library is free software. It comes without any warranty, to |
|---|
| 9 | * the extent permitted by applicable law. You can redistribute it |
|---|
| 10 | * and/or modify it under the terms of the Do What The Fuck You Want |
|---|
| 11 | * To Public License, Version 2, as published by Sam Hocevar. See |
|---|
| 12 | * http://sam.zoy.org/wtfpl/COPYING for more details. |
|---|
| 13 | */ |
|---|
| 14 | |
|---|
| 15 | /* |
|---|
| 16 | * pixels.c: pixel-level image manipulation |
|---|
| 17 | */ |
|---|
| 18 | |
|---|
| 19 | #include "config.h" |
|---|
| 20 | #include "common.h" |
|---|
| 21 | |
|---|
| 22 | #include <stdio.h> |
|---|
| 23 | #include <stdlib.h> |
|---|
| 24 | #include <string.h> |
|---|
| 25 | |
|---|
| 26 | #include <math.h> |
|---|
| 27 | |
|---|
| 28 | #include "pipi_internals.h" |
|---|
| 29 | #include "pipi.h" |
|---|
| 30 | |
|---|
| 31 | #define C2I(p) ((int)255.999*pow(((double)p)/255., 2.2)) |
|---|
| 32 | #define I2C(p) ((int)255.999*pow(((double)p)/255., 1./2.2)) |
|---|
| 33 | |
|---|
| 34 | int pipi_getgray(pipi_image_t const *img, int x, int y, int *g) |
|---|
| 35 | { |
|---|
| 36 | if(x < 0 || y < 0 || x >= img->width || y >= img->height) |
|---|
| 37 | { |
|---|
| 38 | *g = 255; |
|---|
| 39 | return -1; |
|---|
| 40 | } |
|---|
| 41 | |
|---|
| 42 | *g = (unsigned char)img->pixels[y * img->pitch + x * img->channels + 1]; |
|---|
| 43 | |
|---|
| 44 | return 0; |
|---|
| 45 | } |
|---|
| 46 | |
|---|
| 47 | int pipi_getpixel(pipi_image_t const *img, |
|---|
| 48 | int x, int y, int *r, int *g, int *b) |
|---|
| 49 | { |
|---|
| 50 | uint8_t *pixel; |
|---|
| 51 | |
|---|
| 52 | if(x < 0 || y < 0 || x >= img->width || y >= img->height) |
|---|
| 53 | { |
|---|
| 54 | *r = 255; |
|---|
| 55 | *g = 255; |
|---|
| 56 | *b = 255; |
|---|
| 57 | return -1; |
|---|
| 58 | } |
|---|
| 59 | |
|---|
| 60 | pixel = img->pixels + y * img->pitch + x * img->channels; |
|---|
| 61 | |
|---|
| 62 | *b = C2I((unsigned char)pixel[0]); |
|---|
| 63 | *g = C2I((unsigned char)pixel[1]); |
|---|
| 64 | *r = C2I((unsigned char)pixel[2]); |
|---|
| 65 | |
|---|
| 66 | return 0; |
|---|
| 67 | } |
|---|
| 68 | |
|---|
| 69 | int pipi_setpixel(pipi_image_t *img, int x, int y, int r, int g, int b) |
|---|
| 70 | { |
|---|
| 71 | uint8_t *pixel; |
|---|
| 72 | |
|---|
| 73 | if(x < 0 || y < 0 || x >= img->width || y >= img->height) |
|---|
| 74 | return -1; |
|---|
| 75 | |
|---|
| 76 | pixel = img->pixels + y * img->pitch + x * img->channels; |
|---|
| 77 | |
|---|
| 78 | pixel[0] = I2C(b); |
|---|
| 79 | pixel[1] = I2C(g); |
|---|
| 80 | pixel[2] = I2C(r); |
|---|
| 81 | |
|---|
| 82 | return 0; |
|---|
| 83 | } |
|---|
| 84 | |
|---|