source: libpipi/trunk/pipi/tiles.c @ 4690

Last change on this file since 4690 was 3342, checked in by Sam Hocevar, 12 years ago

Change _C pixel format suffixes into _U8 for more clarity.

File size: 2.6 KB
Line 
1/*
2 *  libpipi       Pathetic image processing interface 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 * tiles.c: the tiles system
17 */
18
19#include "config.h"
20
21#include <stdio.h>
22#include <stdlib.h>
23#include <string.h>
24
25#include "pipi.h"
26#include "pipi_internals.h"
27
28#ifdef USE_TILES
29pipi_tile_t *pipi_get_tile(pipi_image_t *img, int x, int y, int zoom,
30                           pipi_format_t fmt, int plane)
31{
32    pipi_tile_t * ret;
33    int i;
34
35    /* If the tile already exists, return it. */
36    for(i = 0; i < img->ntiles; i++)
37    {
38        if(img->tiles[i]->x == x && img->tiles[i]->y == y
39            && img->tiles[i]->fmt == fmt
40            && img->tiles[i]->zoom == zoom
41            && img->tiles[i]->plane == plane)
42        {
43            img->tiles[i]->refcount++;
44            return img->tiles[i];
45        }
46    }
47
48    /* Create a tile. When the image provides a tile creation function,
49     * we should use it. */
50    ret = pipi_create_tile(fmt, plane);
51    ret->x = x;
52    ret->y = y;
53    ret->refcount = 1;
54
55    /* Insert tile and return it. */
56    img->ntiles++;
57    img->tiles = realloc(img->tiles, img->ntiles * sizeof(pipi_tile_t *));
58    img->tiles[img->ntiles - 1] = ret;
59
60    return ret;
61}
62
63void pipi_release_tile(pipi_image_t *img, pipi_tile_t *tile)
64{
65    int i;
66
67    for(i = 0; i < img->ntiles; i++)
68    {
69        if(img->tiles[i] == tile)
70        {
71            img->tiles[i]->refcount--;
72            if(img->tiles[i]->refcount <= 0)
73            {
74                free(img->tiles[i]);
75                img->tiles[i] = img->tiles[img->ntiles - 1];
76                img->ntiles--;
77            }
78            return;
79        }
80    }
81}
82
83pipi_tile_t *pipi_create_tile(pipi_format_t fmt, int plane)
84{
85    pipi_tile_t * ret;
86    size_t bytes;
87
88    switch(fmt)
89    {
90        case PIPI_PIXELS_RGBA_U8:
91        case PIPI_PIXELS_BGR_U8:
92            bytes = sizeof(uint8_t) * TILE_SIZE * TILE_SIZE;
93            break;
94        case PIPI_PIXELS_RGBA_F32:
95        case PIPI_PIXELS_Y_F32:
96            bytes = sizeof(float) * TILE_SIZE * TILE_SIZE;
97            break;
98        default:
99            bytes = 0;
100            break;
101    }
102
103    ret = malloc(sizeof(pipi_tile_t) + bytes);
104    ret->fmt = fmt;
105    ret->plane = plane;
106    ret->data.u8 = ret->align.u8;
107
108    return ret;
109}
110#endif /* USE_TILES */
111
Note: See TracBrowser for help on using the repository browser.