Changeset 2605
- Timestamp:
- Jul 29, 2008, 1:01:29 AM (14 years ago)
- Location:
- libpipi/trunk/pipi
- Files:
-
- 12 edited
Legend:
- Unmodified
- Added
- Removed
-
libpipi/trunk/pipi/codec.c
r2261 r2605 50 50 } 51 51 52 pipi_image_t *pipi_copy(pipi_image_t const *img)53 {54 pipi_image_t *dst;55 int x, y;56 dst = pipi_new(img->width, img->height);57 for(y = 0; y < img->height; y++)58 {59 for(x = 0; x < img->width; x++)60 {61 double r, g, b;62 pipi_getpixel(img, x, y, &r, &g, &b);63 pipi_setpixel(dst, x, y, r, g, b);64 }65 }66 return dst;67 }68 69 52 void pipi_free(pipi_image_t *img) 70 53 { -
libpipi/trunk/pipi/codec/imlib.c
r2261 r2605 38 38 39 39 img = (pipi_image_t *)malloc(sizeof(pipi_image_t)); 40 memset(img, 0, sizeof(pipi_image_t)); 41 40 42 imlib_context_set_image(priv); 41 img->width = imlib_image_get_width(); 42 img->height = imlib_image_get_height(); 43 img->pitch = 4 * imlib_image_get_width(); 44 img->channels = 4; 45 img->pixels = (char *)imlib_image_get_data(); 46 img->priv = (void *)priv; 43 img->w = imlib_image_get_width(); 44 img->h = imlib_image_get_height(); 45 46 img->p[PIPI_PIXELS_RGBA32].pixels = imlib_image_get_data(); 47 img->p[PIPI_PIXELS_RGBA32].w = imlib_image_get_width(); 48 img->p[PIPI_PIXELS_RGBA32].h = imlib_image_get_height(); 49 img->p[PIPI_PIXELS_RGBA32].pitch = 4 * imlib_image_get_width(); 50 img->last_modified = PIPI_PIXELS_RGBA32; 51 52 img->codec_priv = (void *)priv; 53 img->codec_format = PIPI_PIXELS_RGBA32; 47 54 48 55 return img; … … 58 65 59 66 img = (pipi_image_t *)malloc(sizeof(pipi_image_t)); 67 memset(img, 0, sizeof(pipi_image_t)); 68 60 69 imlib_context_set_image(priv); 61 img->width = imlib_image_get_width(); 62 img->height = imlib_image_get_height(); 63 img->pitch = 4 * imlib_image_get_width(); 64 img->channels = 4; 65 img->pixels = (char *)imlib_image_get_data(); 66 img->priv = (void *)priv; 70 img->w = imlib_image_get_width(); 71 img->h = imlib_image_get_height(); 72 73 img->p[PIPI_PIXELS_RGBA32].pixels = imlib_image_get_data(); 74 img->p[PIPI_PIXELS_RGBA32].w = imlib_image_get_width(); 75 img->p[PIPI_PIXELS_RGBA32].h = imlib_image_get_height(); 76 img->p[PIPI_PIXELS_RGBA32].pitch = 4 * imlib_image_get_width(); 77 img->last_modified = PIPI_PIXELS_RGBA32; 78 79 img->codec_priv = (void *)priv; 80 img->codec_format = PIPI_PIXELS_RGBA32; 67 81 68 82 return img; … … 71 85 void pipi_free_imlib2(pipi_image_t *img) 72 86 { 73 imlib_context_set_image(img-> priv);87 imlib_context_set_image(img->codec_priv); 74 88 imlib_free_image(); 75 89 … … 79 93 void pipi_save_imlib2(pipi_image_t *img, const char *name) 80 94 { 81 imlib_context_set_image(img->priv); 95 pipi_getpixels(img, img->codec_format); 96 imlib_context_set_image(img->codec_priv); 82 97 imlib_save_image(name); 83 98 } -
libpipi/trunk/pipi/codec/opencv.c
r2261 r2605 39 39 40 40 img = (pipi_image_t *)malloc(sizeof(pipi_image_t)); 41 img->width = priv->width; 42 img->height = priv->height; 43 img->pitch = priv->widthStep; 44 img->channels = priv->nChannels; 45 img->pixels = priv->imageData; 46 img->priv = (void *)priv; 41 memset(img, 0, sizeof(pipi_image_t)); 42 43 img->w = priv->width; 44 img->h = priv->height; 45 46 img->p[PIPI_PIXELS_RGBA32].pixels = priv->imageData; 47 img->p[PIPI_PIXELS_RGBA32].w = priv->width; 48 img->p[PIPI_PIXELS_RGBA32].h = priv->height; 49 img->p[PIPI_PIXELS_RGBA32].pitch = priv->widthStep; 50 img->last_modified = PIPI_PIXELS_RGBA32; 51 52 img->codec_priv = (void *)priv; 53 img->codec_format = PIPI_PIXELS_RGBA32; 47 54 48 55 return img; … … 58 65 59 66 img = (pipi_image_t *)malloc(sizeof(pipi_image_t)); 60 img->width = priv->width; 61 img->height = priv->height; 62 img->pitch = priv->widthStep; 63 img->channels = priv->nChannels; 64 img->pixels = priv->imageData; 65 img->priv = (void *)priv; 67 memset(img, 0, sizeof(pipi_image_t)); 68 69 img->w = priv->width; 70 img->h = priv->height; 71 72 img->p[PIPI_PIXELS_RGBA32].pixels = priv->imageData; 73 img->p[PIPI_PIXELS_RGBA32].w = priv->width; 74 img->p[PIPI_PIXELS_RGBA32].h = priv->height; 75 img->p[PIPI_PIXELS_RGBA32].pitch = priv->widthStep; 76 img->last_modified = PIPI_PIXELS_RGBA32; 77 78 img->codec_priv = (void *)priv; 79 img->codec_format = PIPI_PIXELS_RGBA32; 66 80 67 81 return img; … … 71 85 { 72 86 IplImage *iplimg; 73 iplimg = (IplImage *)img-> priv;87 iplimg = (IplImage *)img->codec_priv; 74 88 cvReleaseImage(&iplimg); 75 89 … … 79 93 void pipi_save_opencv(pipi_image_t *img, const char *name) 80 94 { 81 cvSaveImage(name, img->priv); 95 pipi_getpixels(img, img->codec_format); 96 cvSaveImage(name, img->codec_priv); 82 97 } 83 98 -
libpipi/trunk/pipi/codec/sdl.c
r2261 r2605 37 37 return NULL; 38 38 39 if(priv->format->BytesPerPixel == 1)39 if(priv->format->BytesPerPixel != 4) 40 40 { 41 41 img = pipi_new(priv->w, priv->h); 42 SDL_BlitSurface(priv, NULL, img-> priv, NULL);42 SDL_BlitSurface(priv, NULL, img->codec_priv, NULL); 43 43 SDL_FreeSurface(priv); 44 44 return img; … … 46 46 47 47 img = (pipi_image_t *)malloc(sizeof(pipi_image_t)); 48 img->width = priv->w; 49 img->height = priv->h; 50 img->pitch = priv->pitch; 51 img->channels = priv->format->BytesPerPixel; 52 img->pixels = priv->pixels; 53 img->priv = (void *)priv; 48 memset(img, 0, sizeof(pipi_image_t)); 49 50 img->w = priv->w; 51 img->h = priv->h; 52 53 img->p[PIPI_PIXELS_RGBA32].pixels = priv->pixels; 54 img->p[PIPI_PIXELS_RGBA32].w = priv->w; 55 img->p[PIPI_PIXELS_RGBA32].h = priv->h; 56 img->p[PIPI_PIXELS_RGBA32].pitch = priv->pitch; 57 img->last_modified = PIPI_PIXELS_RGBA32; 58 59 img->codec_priv = (void *)priv; 60 img->codec_format = PIPI_PIXELS_RGBA32; 54 61 55 62 return img; … … 79 86 80 87 img = (pipi_image_t *)malloc(sizeof(pipi_image_t)); 81 img->width = priv->w; 82 img->height = priv->h; 83 img->pitch = priv->pitch; 84 img->channels = priv->format->BytesPerPixel; 85 img->pixels = priv->pixels; 86 img->priv = (void *)priv; 88 memset(img, 0, sizeof(pipi_image_t)); 89 90 img->w = priv->w; 91 img->h = priv->h; 92 93 img->p[PIPI_PIXELS_RGBA32].pixels = priv->pixels; 94 img->p[PIPI_PIXELS_RGBA32].w = priv->w; 95 img->p[PIPI_PIXELS_RGBA32].h = priv->h; 96 img->p[PIPI_PIXELS_RGBA32].pitch = priv->pitch; 97 img->last_modified = PIPI_PIXELS_RGBA32; 98 99 img->codec_priv = (void *)priv; 100 img->codec_format = PIPI_PIXELS_RGBA32; 87 101 88 102 return img; … … 91 105 void pipi_free_sdl(pipi_image_t *img) 92 106 { 93 SDL_FreeSurface(img-> priv);107 SDL_FreeSurface(img->codec_priv); 94 108 95 109 free(img); … … 98 112 void pipi_save_sdl(pipi_image_t *img, const char *name) 99 113 { 100 SDL_SaveBMP(img->priv, name); 114 pipi_getpixels(img, img->codec_format); 115 SDL_SaveBMP(img->codec_priv, name); 101 116 } 102 117 -
libpipi/trunk/pipi/dither.c
r2263 r2605 29 29 void pipi_dither_24to16(pipi_image_t *img) 30 30 { 31 /* XXX: disabled because this is not the right place... see pixels.c instead */ 32 #if 0 31 33 int *error, *nexterror; 32 34 uint32_t *p32; … … 85 87 free(error); 86 88 free(nexterror); 89 #endif 87 90 } 88 91 -
libpipi/trunk/pipi/filter/blur.c
r2603 r2605 28 28 #include "pipi_internals.h" 29 29 30 pipi_image_t *pipi_gaussian_blur(pipi_image_t const*src, float radius)30 pipi_image_t *pipi_gaussian_blur(pipi_image_t *src, float radius) 31 31 { 32 32 pipi_image_t *dst; 33 pipi_pixels_t *srcp, *dstp; 34 float *srcdata, *dstdata; 33 35 double *kernel; 34 36 double K, L, t = 0.; 35 37 int x, y, i, j, w, h, kr, kw; 36 38 37 w = src->width; 38 h = src->height; 39 w = src->w; 40 h = src->h; 41 42 srcp = pipi_getpixels(src, PIPI_PIXELS_RGBA_F); 43 srcdata = (float *)srcp->pixels; 44 45 dst = pipi_new(w, h); 46 dstp = pipi_getpixels(dst, PIPI_PIXELS_RGBA_F); 47 dstdata = (float *)dstp->pixels; 39 48 40 49 kr = (int)(3. * radius + 0.99999); … … 53 62 kernel[(j + kr) * kw + (i + kr)] /= t; 54 63 55 dst = pipi_new(w, h);56 57 64 for(y = 0; y < h; y++) 58 65 { … … 64 71 for(i = -kr; i <= kr; i++) 65 72 { 66 double r, g, b,f = kernel[(j + kr) * kw + (i + kr)];73 double f = kernel[(j + kr) * kw + (i + kr)]; 67 74 68 pipi_getpixel(src, x + i, y + j, &r, &g, &b); 69 R += f * r; 70 G += f * g; 71 B += f * b; 75 R += f * srcdata[((y + j) * w + x + i) * 4]; 76 G += f * srcdata[((y + j) * w + x + i) * 4 + 1]; 77 B += f * srcdata[((y + j) * w + x + i) * 4 + 2]; 72 78 } 73 79 74 pipi_setpixel(dst, x, y, R, G, B); 80 dstdata[(y * w + x) * 4] = R; 81 dstdata[(y * w + x) * 4 + 1] = G; 82 dstdata[(y * w + x) * 4 + 2] = B; 75 83 } 76 84 } -
libpipi/trunk/pipi/pipi.c
r2227 r2605 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 * pipi.c: core library routines 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 "pipi.h" 27 #include "pipi_internals.h" 28 29 /* 30 static int init = 0; 31 32 void _pipi_init(void) 33 { 34 if(init) 35 return; 36 37 _pipi_init_pixels(); 38 } 39 */ 40 -
libpipi/trunk/pipi/pipi.h
r2603 r2605 25 25 #endif 26 26 27 /* pipi_format_t: this enum is a list of all possible pixel formats for 28 * our internal images. RGBA32 is the most usual format when an image has 29 * just been loaded, but RGBA_F is a lot better for complex operations. */ 30 typedef enum 31 { 32 PIPI_PIXELS_RGBA32 = 0, 33 PIPI_PIXELS_RGBA_F = 1, 34 35 PIPI_PIXELS_MAX = 2 36 } 37 pipi_format_t; 38 39 /* pipi_pixels_t: this structure stores a pixel view of an image. */ 40 typedef struct 41 { 42 void *pixels; 43 int w, h, pitch; 44 } 45 pipi_pixels_t; 46 47 /* pipi_image_t: the main image type */ 27 48 typedef struct pipi_image pipi_image_t; 28 49 29 extern pipi_image_t *pipi_load(const char *name);30 extern pipi_image_t *pipi_new(int width, int height);31 extern pipi_image_t *pipi_copy(const pipi_image_t *img);32 extern void pipi_free(pipi_image_t *img);33 extern void pipi_save(pipi_image_t *img, const char *name);34 50 35 extern int pipi_getgray(pipi_image_t const *img, int x, int y, int *g); 36 extern int pipi_getpixel(pipi_image_t const *img, 37 int x, int y, double *r, double *g, double *b); 38 extern int pipi_setpixel(pipi_image_t *img, int x, int y, 39 double r, double g, double b); 51 extern pipi_image_t *pipi_load(const char *); 52 extern pipi_image_t *pipi_new(int, int); 53 extern void pipi_free(pipi_image_t *); 54 extern void pipi_save(pipi_image_t *, const char *); 40 55 41 extern pipi_ image_t *pipi_resize(pipi_image_t const *, int, int);56 extern pipi_pixels_t *pipi_getpixels(pipi_image_t *, pipi_format_t); 42 57 43 extern pipi_image_t *pipi_ gaussian_blur(pipi_image_t const *, float);58 extern pipi_image_t *pipi_resize(pipi_image_t *, int, int); 44 59 45 extern void pipi_dither_24to16(pipi_image_t *img); 60 extern pipi_image_t *pipi_gaussian_blur(pipi_image_t *, float); 61 62 extern void pipi_dither_24to16(pipi_image_t *); 46 63 47 64 extern void pipi_test(pipi_image_t *); -
libpipi/trunk/pipi/pipi_internals.h
r2261 r2605 17 17 */ 18 18 19 #ifndef __ CACA_INTERNALS_H__20 #define __ CACA_INTERNALS_H__19 #ifndef __PIPI_INTERNALS_H__ 20 #define __PIPI_INTERNALS_H__ 21 21 22 /* pipi_image_t: the image structure. This is probably going to be the most 23 * complex structure in the library, but right now it only has fairly normal 24 * stuff, like width and height and pointers to pixel areas. */ 22 25 struct pipi_image 23 26 { 24 int width, height, pitch, channels; 25 unsigned char *pixels; 26 void *priv; 27 int w, h, pitch; 28 pipi_format_t codec_format, last_modified; 29 30 /* List of all possible pixel formats */ 31 pipi_pixels_t p[PIPI_PIXELS_MAX]; 32 33 /* Private data used by the codec */ 34 void *codec_priv; 27 35 }; 28 36 -
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 -
libpipi/trunk/pipi/resize.c
r2260 r2605 26 26 #include "pipi_internals.h" 27 27 28 pipi_image_t *pipi_resize(pipi_image_t const*src, int w, int h)28 pipi_image_t *pipi_resize(pipi_image_t *src, int w, int h) 29 29 { 30 double*aline, *line;30 float *srcdata, *dstdata, *aline, *line; 31 31 pipi_image_t *dst; 32 pipi_pixels_t *srcp, *dstp; 32 33 int x, y, x0, y0, sw, dw, sh, dh, remy; 33 34 35 srcp = pipi_getpixels(src, PIPI_PIXELS_RGBA_F); 36 srcdata = (float *)srcp->pixels; 37 34 38 dst = pipi_new(w, h); 39 dstp = pipi_getpixels(dst, PIPI_PIXELS_RGBA_F); 40 dstdata = (float *)dstp->pixels; 35 41 36 sw = src->w idth; sh = src->height;37 dw = dst->w idth; dh = dst->height;42 sw = src->w; sh = src->h; 43 dw = dst->w; dh = dst->h; 38 44 39 aline = malloc(3 * dw * sizeof( double));40 line = malloc(3 * dw * sizeof( double));45 aline = malloc(3 * dw * sizeof(float)); 46 line = malloc(3 * dw * sizeof(float)); 41 47 42 memset(line, 0, 3 * dw * sizeof( double));48 memset(line, 0, 3 * dw * sizeof(float)); 43 49 remy = 0; 44 50 45 for(y = 0, y0 = 0; y < d st->height; y++)51 for(y = 0, y0 = 0; y < dh; y++) 46 52 { 47 53 int toty = 0, ny; 48 54 49 memset(aline, 0, 3 * dw * sizeof( double));55 memset(aline, 0, 3 * dw * sizeof(float)); 50 56 51 57 while(toty < sh) … … 53 59 if(remy == 0) 54 60 { 55 doubler = 0, g = 0, b = 0;61 float r = 0, g = 0, b = 0; 56 62 int remx = 0; 57 63 58 for(x = 0, x0 = 0; x < d st->width; x++)64 for(x = 0, x0 = 0; x < dw; x++) 59 65 { 60 doublear = 0, ag = 0, ab = 0;66 float ar = 0, ag = 0, ab = 0; 61 67 int totx = 0, nx; 62 68 … … 65 71 if(remx == 0) 66 72 { 67 pipi_getpixel(src, x0, y0, &r, &g, &b); 73 r = srcdata[(y0 * sw + x0) * 4]; 74 g = srcdata[(y0 * sw + x0) * 4 + 1]; 75 b = srcdata[(y0 * sw + x0) * 4 + 2]; 68 76 x0++; 69 77 remx = dw; … … 86 94 87 95 ny = (toty + remy <= sh) ? remy : sh - toty; 88 for(x = 0; x < d st->width; x++)96 for(x = 0; x < dw; x++) 89 97 { 90 98 aline[3 * x] += ny * line[3 * x]; … … 96 104 } 97 105 98 for(x = 0; x < dst->width; x++) 99 pipi_setpixel(dst, x, y, 100 aline[3 * x] / (sw * sh), 101 aline[3 * x + 1] / (sw * sh), 102 aline[3 * x + 2] / (sw * sh)); 106 for(x = 0; x < dw; x++) 107 { 108 dstdata[(y * dw + x) * 4] = aline[3 * x] / (sw * sh); 109 dstdata[(y * dw + x) * 4 + 1] = aline[3 * x + 1] / (sw * sh); 110 dstdata[(y * dw + x) * 4 + 2] = aline[3 * x + 2] / (sw * sh); 111 } 103 112 } 104 113 -
libpipi/trunk/pipi/test.c
r2600 r2605 28 28 void pipi_test(pipi_image_t *img) 29 29 { 30 pipi_pixels_t *pixels; 31 float *data; 30 32 int x, y; 31 33 32 for(y = 0; y < img->height; y++) 34 pixels = pipi_getpixels(img, PIPI_PIXELS_RGBA_F); 35 data = (float *)pixels->pixels; 36 37 for(y = 0; y < img->h; y++) 33 38 { 34 for(x = 0; x < img->w idth; x++)39 for(x = 0; x < img->w; x++) 35 40 { 36 41 double r = 0, g = 0, b = 0; 37 42 38 pipi_getpixel(img, x, y, &r, &g, &b); 43 r = data[(y * img->w + x) * 4]; 44 g = data[(y * img->w + x) * 4 + 1]; 45 b = data[(y * img->w + x) * 4 + 2]; 46 39 47 if(r + g + b == 0) 40 48 r = g = b = 1. / 3; … … 54 62 r += d; g -= d; b -= d; 55 63 } 56 pipi_setpixel(img, x, y, r, g, b); 64 65 data[(y * img->w + x) * 4] = r; 66 data[(y * img->w + x) * 4 + 1] = g; 67 data[(y * img->w + x) * 4 + 2] = b; 57 68 } 58 69 }
Note: See TracChangeset
for help on using the changeset viewer.