source: libpipi/trunk/pipi/pixels.c @ 2440

Last change on this file since 2440 was 2260, checked in by Sam Hocevar, 15 years ago
  • getpixel/setpixel functions use doubles instead of ints for now.
File size: 1.8 KB
Line 
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.h"
29#include "pipi_internals.h"
30
31#define C2I(p) (pow(((double)p)/255., 2.2))
32#define I2C(p) ((int)255.999*pow(((double)p), 1./2.2))
33
34int 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
47int 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 || y < 0 || x >= img->width || y >= img->height)
53    {
54        *r = *g = *b = 1.;
55        return -1;
56    }
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
67int 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 TracBrowser for help on using the repository browser.