From 0494eff2efcadb18fc26f79782d41f1fb6436d88 Mon Sep 17 00:00:00 2001 From: sam Date: Mon, 12 Jan 2009 23:51:17 +0000 Subject: [PATCH] Add pipi_release_pixels and pipi_set_colorspace in prevision of pipi_get_pixels using the tiles system. These functions should disappear one day, because they're going to leak like crazy. git-svn-id: file:///srv/caca.zoy.org/var/lib/svn/libpipi/trunk@3341 92316355-f0b4-4df1-b90c-862c8a59935f --- examples/edd.c | 2 +- examples/img2rubik.c | 1 + pipi-sharp/Picture.cs | 5 +++++ pipi/analysis/histogram.c | 6 +++++- pipi/analysis/measure.c | 1 + pipi/codec/gdi.c | 4 ++++ pipi/codec/imlib.c | 2 +- pipi/codec/modular/jpeg.c | 2 ++ pipi/codec/opencv.c | 2 +- pipi/codec/oric.c | 11 ++++++----- pipi/codec/sdl.c | 2 +- pipi/pipi.h | 2 ++ pipi/pixels.c | 11 +++++++++++ pipi/tiles.c | 1 - 14 files changed, 41 insertions(+), 11 deletions(-) diff --git a/examples/edd.c b/examples/edd.c index b783a03..7e54958 100644 --- a/examples/edd.c +++ b/examples/edd.c @@ -49,7 +49,7 @@ int main(int argc, char *argv[]) /* Load image, convert it to grayscale, dither it with Floyd-Steinberg */ img = pipi_load(argv[1]); - pipi_get_pixels(img, PIPI_PIXELS_Y_F); + pipi_set_colorspace(img, PIPI_PIXELS_Y_F); gauss = pipi_gaussian_blur(img, sigma); kernel = pipi_load("ediff:fs"); dither = pipi_dither_ediff(img, kernel, PIPI_SCAN_RASTER); diff --git a/examples/img2rubik.c b/examples/img2rubik.c index afd8249..db45f67 100644 --- a/examples/img2rubik.c +++ b/examples/img2rubik.c @@ -111,6 +111,7 @@ int main(int argc, char *argv[]) } pipi_save(dst, argv[2]); + pipi_release_pixels(dst, p); return 0; } diff --git a/pipi-sharp/Picture.cs b/pipi-sharp/Picture.cs index fa2afaa..9b108b9 100644 --- a/pipi-sharp/Picture.cs +++ b/pipi-sharp/Picture.cs @@ -90,6 +90,9 @@ namespace Pipi [DllImport("libpipi-0.dll", CallingConvention=CallingConvention.Cdecl), SuppressUnmanagedCodeSecurity] private static extern IntPtr pipi_get_pixels(IntPtr img, int type); + [DllImport("libpipi-0.dll", CallingConvention=CallingConvention.Cdecl), + SuppressUnmanagedCodeSecurity] + private static extern void pipi_release_pixels(IntPtr img, IntPtr p); public byte[] GetPixels(int w, int h, int x, int y) { byte[] array = new byte[w * h * 4]; @@ -116,6 +119,8 @@ namespace Pipi } } + pipi_release_pixels(_picture, pixels); + return array; } } diff --git a/pipi/analysis/histogram.c b/pipi/analysis/histogram.c index 30dc356..ce61735 100644 --- a/pipi/analysis/histogram.c +++ b/pipi/analysis/histogram.c @@ -35,6 +35,7 @@ pipi_histogram_t* pipi_new_histogram(void) int pipi_get_image_histogram(pipi_image_t *img, pipi_histogram_t*h, int flags) { + pipi_pixels_t *p; uint8_t *data; float n; unsigned int max; @@ -42,7 +43,8 @@ int pipi_get_image_histogram(pipi_image_t *img, pipi_histogram_t*h, int flags) if(!h) return -1; - data = (uint8_t *)pipi_get_pixels(img, PIPI_PIXELS_RGBA_C)->pixels; + p = pipi_get_pixels(img, PIPI_PIXELS_RGBA_C); + data = (uint8_t *)p->pixels; memset(h->a, 0, 256*(sizeof(unsigned int))); memset(h->r, 0, 256*(sizeof(unsigned int))); memset(h->g, 0, 256*(sizeof(unsigned int))); @@ -118,6 +120,8 @@ int pipi_get_image_histogram(pipi_image_t *img, pipi_histogram_t*h, int flags) h->y[i]*=n; } + pipi_release_pixels(img, p); + return 0; } diff --git a/pipi/analysis/measure.c b/pipi/analysis/measure.c index a226d3e..7658dc7 100644 --- a/pipi/analysis/measure.c +++ b/pipi/analysis/measure.c @@ -41,6 +41,7 @@ double pipi_measure_msd(pipi_image_t *i1, pipi_image_t *i2) f1 = i1->last_modified; f2 = i2->last_modified; + /* FIXME: this is not right */ pipi_get_pixels(i1, PIPI_PIXELS_Y_F); pipi_get_pixels(i2, PIPI_PIXELS_Y_F); diff --git a/pipi/codec/gdi.c b/pipi/codec/gdi.c index 262bffe..de42e63 100644 --- a/pipi/codec/gdi.c +++ b/pipi/codec/gdi.c @@ -70,6 +70,8 @@ pipi_image_t *pipi_load_gdi(const char *name) /* FIXME: do we need to swap bytes? Apparently Vista doesn't need it, * but we'd need a more thorough test. */ + pipi_release_pixels(img, p); + img->codec_priv = NULL; img->wrap = 0; @@ -135,6 +137,8 @@ int pipi_save_gdi(pipi_image_t *img, const char *name) } CloseHandle(hfile); + pipi_release_pixels(img, p); + return 0; } diff --git a/pipi/codec/imlib.c b/pipi/codec/imlib.c index 0e33a05..bac691c 100644 --- a/pipi/codec/imlib.c +++ b/pipi/codec/imlib.c @@ -98,7 +98,7 @@ int pipi_save_imlib2(pipi_image_t *img, const char *name) img->u8 = 1; } - pipi_get_pixels(img, img->codec_format); + pipi_set_colorspace(img, img->codec_format); imlib_context_set_image(img->codec_priv); imlib_save_image(name); diff --git a/pipi/codec/modular/jpeg.c b/pipi/codec/modular/jpeg.c index 33136d4..b319766 100644 --- a/pipi/codec/modular/jpeg.c +++ b/pipi/codec/modular/jpeg.c @@ -205,6 +205,8 @@ end: free(line); fclose(fp); + pipi_release_pixels(img, pixels); + return 1; } diff --git a/pipi/codec/opencv.c b/pipi/codec/opencv.c index a9ea884..01da834 100644 --- a/pipi/codec/opencv.c +++ b/pipi/codec/opencv.c @@ -91,7 +91,7 @@ int pipi_save_opencv(pipi_image_t *img, const char *name) img->u8 = 1; } - pipi_get_pixels(img, img->codec_format); + pipi_set_colorspace(img, img->codec_format); cvSaveImage(name, img->codec_priv); return 0; diff --git a/pipi/codec/oric.c b/pipi/codec/oric.c index b63da16..79e9857 100644 --- a/pipi/codec/oric.c +++ b/pipi/codec/oric.c @@ -110,6 +110,8 @@ pipi_image_t *pipi_load_oric(char const *name) img->wrap = 0; img->u8 = 1; + pipi_release_pixels(img, p); + return img; } @@ -139,16 +141,15 @@ int pipi_save_oric(pipi_image_t *img, char const *name) fwrite("\x00", 1, 1, fp); if(img->w != WIDTH || img->h != HEIGHT) - { tmp = pipi_resize(img, WIDTH, HEIGHT); - p = pipi_get_pixels(tmp, PIPI_PIXELS_RGBA_F); - } else - p = pipi_get_pixels(img, PIPI_PIXELS_RGBA_F); + tmp = img; + p = pipi_get_pixels(tmp, PIPI_PIXELS_RGBA_F); data = p->pixels; screen = malloc(WIDTH * HEIGHT / 6); write_screen(data, screen); - if(tmp) + pipi_release_pixels(tmp, p); + if(tmp != img) pipi_free(tmp); fwrite(screen, 1, WIDTH * HEIGHT / 6, fp); diff --git a/pipi/codec/sdl.c b/pipi/codec/sdl.c index 1b265d1..7879189 100644 --- a/pipi/codec/sdl.c +++ b/pipi/codec/sdl.c @@ -95,7 +95,7 @@ int pipi_save_sdl(pipi_image_t *img, const char *name) img->u8 = 1; } - pipi_get_pixels(img, img->codec_format); + pipi_set_colorspace(img, img->codec_format); SDL_SaveBMP(img->codec_priv, name); return 0; diff --git a/pipi/pipi.h b/pipi/pipi.h index cbd6f3a..0ffe521 100644 --- a/pipi/pipi.h +++ b/pipi/pipi.h @@ -144,6 +144,8 @@ __extern int pipi_save(pipi_image_t *, const char *); __extern void pipi_set_gamma(double); __extern pipi_pixels_t *pipi_get_pixels(pipi_image_t *, pipi_format_t); +__extern void pipi_release_pixels(pipi_image_t *, pipi_pixels_t *); +__extern void pipi_set_colorspace(pipi_image_t *, pipi_format_t); __extern int pipi_get_image_width(pipi_image_t *img); __extern int pipi_get_image_height(pipi_image_t *img); __extern int pipi_get_image_pitch(pipi_image_t *img); diff --git a/pipi/pixels.c b/pipi/pixels.c index 69f5444..3a978dc 100644 --- a/pipi/pixels.c +++ b/pipi/pixels.c @@ -250,6 +250,17 @@ pipi_pixels_t *pipi_get_pixels(pipi_image_t *img, pipi_format_t type) return &img->p[type]; } +void pipi_release_pixels(pipi_image_t *img, pipi_pixels_t *p) +{ + return; +} + +void pipi_set_colorspace(pipi_image_t *img, pipi_format_t fmt) +{ + pipi_pixels_t *p = pipi_get_pixels(img, fmt); + pipi_release_pixels(img, p); +} + void pipi_set_gamma(double g) { if(g > 0.) diff --git a/pipi/tiles.c b/pipi/tiles.c index 315add7..443428d 100644 --- a/pipi/tiles.c +++ b/pipi/tiles.c @@ -107,6 +107,5 @@ pipi_tile_t *pipi_create_tile(pipi_format_t fmt, int plane) return ret; } - #endif /* USE_TILES */