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
/* Eye of Cafe image viewer - utility functions for computing zoom factors
 *
 * Copyright (C) 2000 The Free Software Foundation
 *
 * Author: Federico Mena-Quintero <federico@gnu.org>
 *
 * 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., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301, USA.
 */

#include <config.h>
#include <math.h>
#include "zoom.h"

/**
 * zoom_fit_size:
 * @dest_width: Width of destination area.
 * @dest_height: Height of destination area.
 * @src_width: Width of source image.
 * @src_height: Height of source image.
 * @upscale_smaller: Whether to scale up images smaller than the destination size.
 * @width: Return value for image width.
 * @height: Return value for image height.
 *
 * Computes the final dimensions of an image that is to be scaled to fit to a
 * certain size.  If @upscale_smaller is TRUE, then images smaller than the
 * destination size will be scaled up; otherwise, they will be left at their
 * original size.
 **/
void
zoom_fit_size (guint dest_width, guint dest_height,<--- The function 'zoom_fit_size' should have static linkage since it is not used outside of its translation unit.
	       guint src_width, guint src_height,
	       gboolean upscale_smaller,
	       guint *width, guint *height)
{
	guint w, h;

	g_return_if_fail (width != NULL);
	g_return_if_fail (height != NULL);

	if (src_width == 0 || src_height == 0) {
		*width = 0;
		*height = 0;
		return;
	}

	if (src_width <= dest_width && src_height <= dest_height && !upscale_smaller) {
		*width = src_width;
		*height = src_height;
		return;
	}

	w = dest_width;
	h = floor ((double) (src_height * w) / src_width + 0.5);

	if (h > dest_height) {
		h = dest_height;
		w = floor ((double) (src_width * h) / src_height + 0.5);
	}

	g_assert (w <= dest_width);
	g_assert (h <= dest_height);

	*width = w;
	*height = h;
}

/**
 * zoom_fit_scale:
 * @dest_width: Width of destination area.
 * @dest_height: Height of destination area.
 * @src_width: Width of source image.
 * @src_height: Height of source image.
 * @upscale_smaller: Whether to scale up images smaller than the destination size.
 *
 * Similar to zoom_fit_size(), but returns the zoom factor of the final image
 * with respect to the original image's size.
 *
 * Return value: Zoom factor with respect to the original size.
 **/
double
zoom_fit_scale (guint dest_width, guint dest_height,
		guint src_width, guint src_height,
		gboolean upscale_smaller)
{
	guint w, h;
	double wfactor, hfactor;
	double factor;

	if (src_width == 0 || src_height == 0)
		return 1.0;

	if (dest_width == 0 || dest_height == 0)
		return 0.0;

	zoom_fit_size (dest_width, dest_height, src_width, src_height, upscale_smaller, &w, &h);

	wfactor = (double) w / src_width;
	hfactor = (double) h / src_height;

	factor = MIN (wfactor, hfactor);

	return factor;
}