1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232
|
/* GIMP - The GNU Image Manipulation Program
* Copyright (C) 1995 Spencer Kimball and Peter Mattis
*
* This program is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation; either version 3 of the License, or
* (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program. If not, see <http://www.gnu.org/licenses/>.
*/
#include "config.h"
#include <string.h>
#include <gegl.h>
#include "libgimpmath/gimpmath.h"
#include "core-types.h"
#include "base/pixel-region.h"
#include "base/temp-buf.h"
#include "base/tile-manager-preview.h"
#include "paint-funcs/subsample-region.h"
#include "config/gimpcoreconfig.h"
#include "gimp.h"
#include "gimpchannel.h"
#include "gimpimage.h"
#include "gimpdrawable-preview.h"
#include "gimpdrawable-private.h"
#include "gimplayer.h"
#include "gimppreviewcache.h"
/* local function prototypes */
static TempBuf * gimp_drawable_preview_private (GimpDrawable *drawable,
gint width,
gint height);
static TempBuf * gimp_drawable_indexed_preview (GimpDrawable *drawable,
const guchar *cmap,
gint src_x,
gint src_y,
gint src_width,
gint src_height,
gint dest_width,
gint dest_height);
/* public functions */
TempBuf *
gimp_drawable_get_preview (GimpViewable *viewable,
GimpContext *context,
gint width,
gint height)
{
GimpDrawable *drawable;
GimpImage *image;
drawable = GIMP_DRAWABLE (viewable);
image = gimp_item_get_image (GIMP_ITEM (drawable));
if (! image->gimp->config->layer_previews)
return NULL;
/* Ok prime the cache with a large preview if the cache is invalid */
if (! drawable->private->preview_valid &&
width <= PREVIEW_CACHE_PRIME_WIDTH &&
height <= PREVIEW_CACHE_PRIME_HEIGHT &&
image &&
gimp_image_get_width (image) > PREVIEW_CACHE_PRIME_WIDTH &&
gimp_image_get_height (image) > PREVIEW_CACHE_PRIME_HEIGHT)
{
TempBuf *tb = gimp_drawable_preview_private (drawable,
PREVIEW_CACHE_PRIME_WIDTH,
PREVIEW_CACHE_PRIME_HEIGHT);
/* Save the 2nd call */
if (width == PREVIEW_CACHE_PRIME_WIDTH &&
height == PREVIEW_CACHE_PRIME_HEIGHT)
return tb;
}
/* Second call - should NOT visit the tile cache...*/
return gimp_drawable_preview_private (drawable, width, height);
}
gint
gimp_drawable_preview_bytes (GimpDrawable *drawable)
{
GimpImageBaseType base_type;
gint bytes = 0;
g_return_val_if_fail (GIMP_IS_DRAWABLE (drawable), 0);
base_type = GIMP_IMAGE_TYPE_BASE_TYPE (gimp_drawable_type (drawable));
switch (base_type)
{
case GIMP_RGB:
case GIMP_GRAY:
bytes = gimp_drawable_bytes (drawable);
break;
case GIMP_INDEXED:
bytes = gimp_drawable_has_alpha (drawable) ? 4 : 3;
break;
}
return bytes;
}
TempBuf *
gimp_drawable_get_sub_preview (GimpDrawable *drawable,
gint src_x,
gint src_y,
gint src_width,
gint src_height,
gint dest_width,
gint dest_height)
{
GimpItem *item;
GimpImage *image;
g_return_val_if_fail (GIMP_IS_DRAWABLE (drawable), NULL);
g_return_val_if_fail (src_x >= 0, NULL);
g_return_val_if_fail (src_y >= 0, NULL);
g_return_val_if_fail (src_width > 0, NULL);
g_return_val_if_fail (src_height > 0, NULL);
g_return_val_if_fail (dest_width > 0, NULL);
g_return_val_if_fail (dest_height > 0, NULL);
item = GIMP_ITEM (drawable);
g_return_val_if_fail ((src_x + src_width) <= gimp_item_get_width (item), NULL);
g_return_val_if_fail ((src_y + src_height) <= gimp_item_get_height (item), NULL);
image = gimp_item_get_image (item);
if (! image->gimp->config->layer_previews)
return NULL;
if (GIMP_IMAGE_TYPE_BASE_TYPE (gimp_drawable_type (drawable)) == GIMP_INDEXED)
return gimp_drawable_indexed_preview (drawable,
gimp_drawable_get_colormap (drawable),
src_x, src_y, src_width, src_height,
dest_width, dest_height);
return tile_manager_get_sub_preview (gimp_drawable_get_tiles (drawable),
src_x, src_y, src_width, src_height,
dest_width, dest_height);
}
/* private functions */
static TempBuf *
gimp_drawable_preview_private (GimpDrawable *drawable,
gint width,
gint height)
{
TempBuf *ret_buf;
if (! drawable->private->preview_valid ||
! (ret_buf = gimp_preview_cache_get (&drawable->private->preview_cache,
width, height)))
{
GimpItem *item = GIMP_ITEM (drawable);
ret_buf = gimp_drawable_get_sub_preview (drawable,
0, 0,
gimp_item_get_width (item),
gimp_item_get_height (item),
width,
height);
if (! drawable->private->preview_valid)
gimp_preview_cache_invalidate (&drawable->private->preview_cache);
drawable->private->preview_valid = TRUE;
gimp_preview_cache_add (&drawable->private->preview_cache, ret_buf);
}
return ret_buf;
}
static TempBuf *
gimp_drawable_indexed_preview (GimpDrawable *drawable,
const guchar *cmap,
gint src_x,
gint src_y,
gint src_width,
gint src_height,
gint dest_width,
gint dest_height)
{
TempBuf *preview_buf;
PixelRegion srcPR;
PixelRegion destPR;
gint bytes = gimp_drawable_preview_bytes (drawable);
gint subsample = 1;
/* calculate 'acceptable' subsample */
while ((dest_width * (subsample + 1) * 2 < src_width) &&
(dest_height * (subsample + 1) * 2 < src_width))
subsample += 1;
pixel_region_init (&srcPR, gimp_drawable_get_tiles (drawable),
src_x, src_y, src_width, src_height,
FALSE);
preview_buf = temp_buf_new (dest_width, dest_height, bytes, 0, 0, NULL);
pixel_region_init_temp_buf (&destPR, preview_buf,
0, 0, dest_width, dest_height);
subsample_indexed_region (&srcPR, &destPR, cmap, subsample);
return preview_buf;
}
|