gimpviewrendererdrawable.c 8.13 KB
Newer Older
1 2 3
/* The GIMP -- an image manipulation program
 * Copyright (C) 1995 Spencer Kimball and Peter Mattis
 *
4
 * gimpviewrendererdrawable.c
5
 * Copyright (C) 2003 Michael Natterer <mitch@gimp.org>
6
 *
7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25
 * 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 2 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, write to the Free Software
 * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
 */

#include "config.h"

#include <gtk/gtk.h>

26 27
#include "libgimpmath/gimpmath.h"

28
#include "widgets-types.h"
29

Michael Natterer's avatar
Michael Natterer committed
30 31
#include "base/temp-buf.h"

32
#include "core/gimp-utils.h"
33
#include "core/gimpdrawable.h"
34
#include "core/gimpdrawable-preview.h"
35 36
#include "core/gimpimage.h"

37
#include "gimpviewrendererdrawable.h"
38

39

40
static void   gimp_view_renderer_drawable_class_init (GimpViewRendererDrawableClass *klass);
41

42 43
static void   gimp_view_renderer_drawable_render     (GimpViewRenderer *renderer,
                                                      GtkWidget        *widget);
44

45

46
static GimpViewRendererClass *parent_class = NULL;
47 48


49
GType
50
gimp_view_renderer_drawable_get_type (void)
51
{
52
  static GType renderer_type = 0;
53

54
  if (! renderer_type)
55
    {
56
      static const GTypeInfo renderer_info =
57
      {
58
        sizeof (GimpViewRendererDrawableClass),
59 60
        NULL,           /* base_init */
        NULL,           /* base_finalize */
61
        (GClassInitFunc) gimp_view_renderer_drawable_class_init,
62 63
        NULL,           /* class_finalize */
        NULL,           /* class_data */
64
        sizeof (GimpViewRendererDrawable),
65
        0,              /* n_preallocs */
66
        NULL            /* instance_init */
67 68
      };

69
      renderer_type = g_type_register_static (GIMP_TYPE_VIEW_RENDERER,
70
                                              "GimpViewRendererDrawable",
71
                                              &renderer_info, 0);
72
    }
73 74

  return renderer_type;
75 76
}

77
static void
78
gimp_view_renderer_drawable_class_init (GimpViewRendererDrawableClass *klass)
79
{
80
  GimpViewRendererClass *renderer_class = GIMP_VIEW_RENDERER_CLASS (klass);
81

82
  parent_class = g_type_class_peek_parent (klass);
83

84
  renderer_class->render = gimp_view_renderer_drawable_render;
85 86
}

87
static void
88 89
gimp_view_renderer_drawable_render (GimpViewRenderer *renderer,
                                    GtkWidget        *widget)
90
{
91
  GimpDrawable *drawable;
92
  GimpItem     *item;
93 94 95
  GimpImage    *gimage;
  gint          width;
  gint          height;
96 97
  gint          view_width;
  gint          view_height;
98
  gboolean      scaling_up;
99
  TempBuf      *render_buf = NULL;
100

101
  drawable = GIMP_DRAWABLE (renderer->viewable);
102 103
  item     = GIMP_ITEM (drawable);
  gimage   = gimp_item_get_image (item);
104

105 106
  width  = renderer->width;
  height = renderer->height;
107

108
  if (gimage && ! renderer->is_popup)
109 110
    {
      width  = MAX (1, ROUND ((((gdouble) width / (gdouble) gimage->width) *
111
                               (gdouble) item->width)));
112
      height = MAX (1, ROUND ((((gdouble) height / (gdouble) gimage->height) *
113
                              (gdouble) item->height)));
114

115
      gimp_viewable_calc_preview_size (item->width,
116
                                       item->height,
117 118
                                       width,
                                       height,
119
                                       renderer->dot_for_dot,
120 121
                                       gimage->xresolution,
                                       gimage->yresolution,
122 123
                                       &view_width,
                                       &view_height,
124
                                       &scaling_up);
125 126 127
    }
  else
    {
128
      gimp_viewable_calc_preview_size (item->width,
129
                                       item->height,
130 131
                                       width,
                                       height,
132
                                       renderer->dot_for_dot,
133 134
                                       gimage ? gimage->xresolution : 1.0,
                                       gimage ? gimage->yresolution : 1.0,
135 136
                                       &view_width,
                                       &view_height,
137
                                       &scaling_up);
138 139
    }

140 141 142
  if ((view_width * view_height) < (item->width * item->height * 4))
    scaling_up = FALSE;

143 144
  if (scaling_up)
    {
145 146 147 148
      if (gimage && ! renderer->is_popup)
        {
          gint src_x, src_y;
          gint src_width, src_height;
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

          if (gimp_rectangle_intersect (0, 0,
                                        item->width, item->height,
                                        -item->offset_x, -item->offset_y,
                                        gimage->width, gimage->height,
                                        &src_x, &src_y,
                                        &src_width, &src_height))
            {
              gint dest_width;
              gint dest_height;

              dest_width  = ROUND (((gdouble) renderer->width /
                                    (gdouble) gimage->width) *
                                   (gdouble) src_width);
              dest_height = ROUND (((gdouble) renderer->height /
                                    (gdouble) gimage->height) *
                                   (gdouble) src_height);

              if (dest_width  < 1) dest_width  = 1;
              if (dest_height < 1) dest_height = 1;

              render_buf = gimp_drawable_get_sub_preview (drawable,
                                                          src_x, src_y,
                                                          src_width, src_height,
                                                          dest_width, dest_height);
            }
          else
            {
177
              gint   bytes    = gimp_drawable_preview_bytes (drawable);
178 179 180 181
              guchar empty[4] = { 0, 0, 0, 0 };

              render_buf = temp_buf_new (1, 1, bytes, 0, 0, empty);
            }
182 183 184 185
        }
      else
        {
          TempBuf *temp_buf;
186

187 188 189
          temp_buf = gimp_viewable_get_new_preview (renderer->viewable,
                                                    item->width,
                                                    item->height);
190

191 192 193
          if (temp_buf)
            {
              render_buf = temp_buf_scale (temp_buf, view_width, view_height);
194

195 196
              temp_buf_free (temp_buf);
            }
197
        }
198 199 200
    }
  else
    {
201
      render_buf = gimp_viewable_get_new_preview (renderer->viewable,
202 203
                                                  view_width,
                                                  view_height);
204 205
    }

206
  if (render_buf)
207
    {
208 209
      if (gimage && ! renderer->is_popup)
        {
210
          if (item->offset_x != 0)
211 212
            render_buf->x =
              ROUND ((((gdouble) renderer->width / (gdouble) gimage->width) *
213
                      (gdouble) item->offset_x));
214

215
          if (item->offset_y != 0)
216 217
            render_buf->y =
              ROUND ((((gdouble) renderer->height / (gdouble) gimage->height) *
218
                      (gdouble) item->offset_y));
219 220 221 222 223 224

          if (scaling_up)
            {
              if (render_buf->x < 0) render_buf->x = 0;
              if (render_buf->y < 0) render_buf->y = 0;
            }
225 226 227
        }
      else
        {
228 229
          if (view_width < width)
            render_buf->x = (width - view_width) / 2;
230

231 232
          if (view_height < height)
            render_buf->y = (height - view_height) / 2;
233 234
        }

235 236 237
      gimp_view_renderer_render_buffer (renderer, render_buf, -1,
                                        GIMP_VIEW_BG_CHECKS,
                                        GIMP_VIEW_BG_CHECKS);
238 239

      temp_buf_free (render_buf);
240 241 242
    }
  else
    {
243
      const gchar *stock_id;
244

245
      stock_id = gimp_viewable_get_stock_id (renderer->viewable);
246

247
      gimp_view_renderer_default_render_stock (renderer, widget, stock_id);
248
    }
249
}