2006-12-10 05:33:38 +08:00
|
|
|
/* GIMP - The GNU Image Manipulation Program
|
2001-02-07 08:06:58 +08:00
|
|
|
* Copyright (C) 1995 Spencer Kimball and Peter Mattis
|
|
|
|
*
|
2009-01-18 06:28:01 +08:00
|
|
|
* This program is free software: you can redistribute it and/or modify
|
2001-02-07 08:06:58 +08:00
|
|
|
* it under the terms of the GNU General Public License as published by
|
2009-01-18 06:28:01 +08:00
|
|
|
* the Free Software Foundation; either version 3 of the License, or
|
2001-02-07 08:06:58 +08:00
|
|
|
* (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
|
2009-01-18 06:28:01 +08:00
|
|
|
* along with this program. If not, see <http://www.gnu.org/licenses/>.
|
2001-02-07 08:06:58 +08:00
|
|
|
*/
|
|
|
|
|
|
|
|
#include "config.h"
|
|
|
|
|
|
|
|
#include <string.h>
|
|
|
|
|
2008-10-10 04:24:04 +08:00
|
|
|
#include <gegl.h>
|
2001-02-07 08:06:58 +08:00
|
|
|
|
|
|
|
#include "libgimpmath/gimpmath.h"
|
|
|
|
|
2001-05-10 06:34:59 +08:00
|
|
|
#include "core-types.h"
|
2001-02-07 08:06:58 +08:00
|
|
|
|
2001-05-15 19:25:25 +08:00
|
|
|
#include "base/pixel-region.h"
|
2007-07-18 04:33:10 +08:00
|
|
|
#include "base/tile-manager-preview.h"
|
2001-05-15 19:25:25 +08:00
|
|
|
|
2007-08-08 16:35:45 +08:00
|
|
|
#include "paint-funcs/subsample-region.h"
|
2001-04-07 23:58:26 +08:00
|
|
|
|
2003-03-07 00:47:34 +08:00
|
|
|
#include "config/gimpcoreconfig.h"
|
|
|
|
|
2012-04-09 00:47:49 +08:00
|
|
|
#include "gegl/gimp-gegl-utils.h"
|
|
|
|
|
2003-03-07 00:47:34 +08:00
|
|
|
#include "gimp.h"
|
2001-02-07 08:06:58 +08:00
|
|
|
#include "gimpchannel.h"
|
|
|
|
#include "gimpimage.h"
|
2003-04-01 21:53:47 +08:00
|
|
|
#include "gimpdrawable-preview.h"
|
2009-02-06 04:27:09 +08:00
|
|
|
#include "gimpdrawable-private.h"
|
2001-02-07 08:06:58 +08:00
|
|
|
#include "gimplayer.h"
|
|
|
|
#include "gimppreviewcache.h"
|
2012-04-09 06:59:20 +08:00
|
|
|
#include "gimptempbuf.h"
|
2001-02-07 08:06:58 +08:00
|
|
|
|
|
|
|
|
|
|
|
/* local function prototypes */
|
|
|
|
|
2012-04-09 02:25:49 +08:00
|
|
|
static GimpTempBuf * gimp_drawable_preview_private (GimpDrawable *drawable,
|
|
|
|
gint width,
|
|
|
|
gint height);
|
|
|
|
static GimpTempBuf * 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);
|
2007-07-18 04:33:10 +08:00
|
|
|
|
2001-02-07 08:06:58 +08:00
|
|
|
|
|
|
|
/* public functions */
|
|
|
|
|
2012-04-09 02:25:49 +08:00
|
|
|
GimpTempBuf *
|
2001-02-07 08:06:58 +08:00
|
|
|
gimp_drawable_get_preview (GimpViewable *viewable,
|
2006-08-30 05:44:51 +08:00
|
|
|
GimpContext *context,
|
2004-12-15 22:40:52 +08:00
|
|
|
gint width,
|
|
|
|
gint height)
|
2001-02-07 08:06:58 +08:00
|
|
|
{
|
2012-04-28 21:25:04 +08:00
|
|
|
GimpDrawable *drawable = GIMP_DRAWABLE (viewable);
|
|
|
|
GimpImage *image = gimp_item_get_image (GIMP_ITEM (drawable));
|
2001-02-07 08:06:58 +08:00
|
|
|
|
2012-04-28 21:25:04 +08:00
|
|
|
if (! image->gimp->config->layer_previews ||
|
|
|
|
/* XXX fixme enable drawable previews for > u8 */
|
|
|
|
gimp_drawable_get_precision (drawable) != GIMP_PRECISION_U8)
|
2003-03-07 00:47:34 +08:00
|
|
|
return NULL;
|
|
|
|
|
2001-02-07 08:06:58 +08:00
|
|
|
/* Ok prime the cache with a large preview if the cache is invalid */
|
2009-02-06 04:27:09 +08:00
|
|
|
if (! drawable->private->preview_valid &&
|
2007-12-26 00:21:40 +08:00
|
|
|
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)
|
2001-02-07 08:06:58 +08:00
|
|
|
{
|
2012-04-09 02:25:49 +08:00
|
|
|
GimpTempBuf *tb = gimp_drawable_preview_private (drawable,
|
|
|
|
PREVIEW_CACHE_PRIME_WIDTH,
|
|
|
|
PREVIEW_CACHE_PRIME_HEIGHT);
|
2001-02-07 08:06:58 +08:00
|
|
|
|
|
|
|
/* Save the 2nd call */
|
|
|
|
if (width == PREVIEW_CACHE_PRIME_WIDTH &&
|
2004-12-15 22:40:52 +08:00
|
|
|
height == PREVIEW_CACHE_PRIME_HEIGHT)
|
|
|
|
return tb;
|
2001-02-07 08:06:58 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
/* Second call - should NOT visit the tile cache...*/
|
|
|
|
return gimp_drawable_preview_private (drawable, width, height);
|
|
|
|
}
|
|
|
|
|
2012-04-22 04:09:39 +08:00
|
|
|
const Babl *
|
|
|
|
gimp_drawable_get_preview_format (GimpDrawable *drawable)
|
2004-12-15 22:40:52 +08:00
|
|
|
{
|
2012-04-22 04:09:39 +08:00
|
|
|
g_return_val_if_fail (GIMP_IS_DRAWABLE (drawable), NULL);
|
2004-12-15 22:40:52 +08:00
|
|
|
|
2012-04-07 07:51:08 +08:00
|
|
|
switch (gimp_drawable_get_base_type (drawable))
|
2004-12-15 22:40:52 +08:00
|
|
|
{
|
|
|
|
case GIMP_GRAY:
|
2012-04-22 04:09:39 +08:00
|
|
|
if (gimp_drawable_has_alpha (drawable))
|
|
|
|
return babl_format ("Y'A u8");
|
|
|
|
else
|
|
|
|
return babl_format ("Y' u8");
|
2004-12-15 22:40:52 +08:00
|
|
|
|
2012-04-22 04:09:39 +08:00
|
|
|
case GIMP_RGB:
|
2004-12-15 22:40:52 +08:00
|
|
|
case GIMP_INDEXED:
|
2012-04-22 04:09:39 +08:00
|
|
|
if (gimp_drawable_has_alpha (drawable))
|
|
|
|
return babl_format ("R'G'B'A u8");
|
|
|
|
else
|
|
|
|
return babl_format ("R'G'B' u8");
|
2004-12-15 22:40:52 +08:00
|
|
|
}
|
|
|
|
|
2012-04-22 04:09:39 +08:00
|
|
|
g_return_val_if_reached (NULL);
|
2004-12-15 22:40:52 +08:00
|
|
|
}
|
|
|
|
|
2012-04-09 02:25:49 +08:00
|
|
|
GimpTempBuf *
|
2004-12-11 09:22:58 +08:00
|
|
|
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)
|
|
|
|
{
|
2012-05-06 10:39:23 +08:00
|
|
|
GimpItem *item;
|
|
|
|
GimpImage *image;
|
|
|
|
GeglBuffer *buffer;
|
2004-12-11 09:22:58 +08:00
|
|
|
|
|
|
|
g_return_val_if_fail (GIMP_IS_DRAWABLE (drawable), NULL);
|
2004-12-13 06:52:00 +08:00
|
|
|
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);
|
|
|
|
|
2008-11-03 08:09:01 +08:00
|
|
|
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);
|
2004-12-11 09:22:58 +08:00
|
|
|
|
2006-03-29 01:08:36 +08:00
|
|
|
image = gimp_item_get_image (item);
|
2004-12-11 09:22:58 +08:00
|
|
|
|
2012-04-28 21:25:04 +08:00
|
|
|
if (! image->gimp->config->layer_previews ||
|
|
|
|
/* XXX fixme enable drawable previews for > u8 */
|
|
|
|
gimp_drawable_get_precision (drawable) != GIMP_PRECISION_U8)
|
2004-12-11 09:22:58 +08:00
|
|
|
return NULL;
|
|
|
|
|
2012-04-07 07:51:08 +08:00
|
|
|
if (gimp_drawable_is_indexed (drawable))
|
2007-07-18 04:33:10 +08:00
|
|
|
return gimp_drawable_indexed_preview (drawable,
|
2008-11-10 09:43:34 +08:00
|
|
|
gimp_drawable_get_colormap (drawable),
|
2007-07-18 04:33:10 +08:00
|
|
|
src_x, src_y, src_width, src_height,
|
|
|
|
dest_width, dest_height);
|
2004-12-11 09:22:58 +08:00
|
|
|
|
2012-05-06 10:39:23 +08:00
|
|
|
buffer = gimp_drawable_get_buffer (drawable);
|
|
|
|
|
|
|
|
return tile_manager_get_sub_preview (gimp_gegl_buffer_get_tiles (buffer),
|
2012-04-22 06:42:00 +08:00
|
|
|
gimp_drawable_get_preview_format (drawable),
|
2007-07-18 04:33:10 +08:00
|
|
|
src_x, src_y, src_width, src_height,
|
|
|
|
dest_width, dest_height);
|
2004-12-11 09:22:58 +08:00
|
|
|
}
|
|
|
|
|
2001-02-07 08:06:58 +08:00
|
|
|
|
|
|
|
/* private functions */
|
|
|
|
|
2012-04-09 02:25:49 +08:00
|
|
|
static GimpTempBuf *
|
2001-02-07 08:06:58 +08:00
|
|
|
gimp_drawable_preview_private (GimpDrawable *drawable,
|
2004-12-15 22:40:52 +08:00
|
|
|
gint width,
|
|
|
|
gint height)
|
2001-02-07 08:06:58 +08:00
|
|
|
{
|
2012-04-28 21:25:04 +08:00
|
|
|
GimpTempBuf *ret_buf = NULL;
|
2003-09-06 01:44:39 +08:00
|
|
|
|
2009-02-06 04:27:09 +08:00
|
|
|
if (! drawable->private->preview_valid ||
|
|
|
|
! (ret_buf = gimp_preview_cache_get (&drawable->private->preview_cache,
|
2004-12-12 05:03:52 +08:00
|
|
|
width, height)))
|
2001-02-07 08:06:58 +08:00
|
|
|
{
|
2004-12-12 05:03:52 +08:00
|
|
|
GimpItem *item = GIMP_ITEM (drawable);
|
2003-09-06 01:44:39 +08:00
|
|
|
|
2004-12-12 05:03:52 +08:00
|
|
|
ret_buf = gimp_drawable_get_sub_preview (drawable,
|
|
|
|
0, 0,
|
2008-11-03 08:09:01 +08:00
|
|
|
gimp_item_get_width (item),
|
|
|
|
gimp_item_get_height (item),
|
2004-12-12 05:03:52 +08:00
|
|
|
width,
|
|
|
|
height);
|
2001-02-07 08:06:58 +08:00
|
|
|
|
2009-02-06 04:27:09 +08:00
|
|
|
if (! drawable->private->preview_valid)
|
|
|
|
gimp_preview_cache_invalidate (&drawable->private->preview_cache);
|
2001-02-07 08:06:58 +08:00
|
|
|
|
2009-02-06 04:27:09 +08:00
|
|
|
drawable->private->preview_valid = TRUE;
|
2001-02-07 08:06:58 +08:00
|
|
|
|
2009-02-06 04:27:09 +08:00
|
|
|
gimp_preview_cache_add (&drawable->private->preview_cache, ret_buf);
|
2001-02-07 08:06:58 +08:00
|
|
|
}
|
2004-12-12 05:03:52 +08:00
|
|
|
|
|
|
|
return ret_buf;
|
2001-02-07 08:06:58 +08:00
|
|
|
}
|
|
|
|
|
2012-04-09 02:25:49 +08:00
|
|
|
static GimpTempBuf *
|
2007-07-18 04:33:10 +08:00
|
|
|
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)
|
|
|
|
{
|
2012-05-06 10:39:23 +08:00
|
|
|
GeglBuffer *buffer;
|
2012-04-09 02:25:49 +08:00
|
|
|
GimpTempBuf *preview_buf;
|
2007-07-18 04:33:10 +08:00
|
|
|
PixelRegion srcPR;
|
|
|
|
PixelRegion destPR;
|
|
|
|
gint subsample = 1;
|
|
|
|
|
|
|
|
/* calculate 'acceptable' subsample */
|
|
|
|
while ((dest_width * (subsample + 1) * 2 < src_width) &&
|
|
|
|
(dest_height * (subsample + 1) * 2 < src_width))
|
|
|
|
subsample += 1;
|
|
|
|
|
2012-05-06 10:39:23 +08:00
|
|
|
buffer = gimp_drawable_get_buffer (drawable);
|
|
|
|
|
|
|
|
pixel_region_init (&srcPR, gimp_gegl_buffer_get_tiles (buffer),
|
2007-07-18 04:33:10 +08:00
|
|
|
src_x, src_y, src_width, src_height,
|
|
|
|
FALSE);
|
|
|
|
|
2012-04-09 05:56:52 +08:00
|
|
|
preview_buf = gimp_temp_buf_new (dest_width, dest_height,
|
2012-04-22 04:09:39 +08:00
|
|
|
gimp_drawable_get_preview_format (drawable));
|
2007-07-18 04:33:10 +08:00
|
|
|
|
|
|
|
pixel_region_init_temp_buf (&destPR, preview_buf,
|
|
|
|
0, 0, dest_width, dest_height);
|
|
|
|
|
|
|
|
subsample_indexed_region (&srcPR, &destPR, cmap, subsample);
|
|
|
|
|
|
|
|
return preview_buf;
|
|
|
|
}
|