libtthimage/Lib/zoom.c

186 lines
4.3 KiB
C
Raw Normal View History

2022-06-26 20:06:35 +11:00
/*
Zooms sur les images
--------------------
new 31 janvier 2009 - avenue Saint Exupery
*/
#include <stdio.h>
#include <string.h>
#include <stdlib.h>
#include <math.h>
2022-06-27 11:19:31 +11:00
#include "../tthimage.h"
2022-06-26 20:06:35 +11:00
/*::------------------------------------------------------------------::*/
/* private variables */
/*::------------------------------------------------------------------::*/
/* private functions */
/*::------------------------------------------------------------------::*/
int Image_essai_zoom(Image_Desc *src, Image_Desc *dst,
double kx, double ky, int flags)
{
int xd, yd, xs, ys;
int rd, gd, bd;
double dxs, dys; /* destination float position */
#if DEBUG_LEVEL
long hit, mess;
#endif
#if DEBUG_LEVEL
2022-10-28 14:59:49 +11:00
fprintf(stderr, ">>> %s ( %p %p %f %f 0x%x )\n", __func__,
2022-06-26 20:06:35 +11:00
src, dst, kx, ky, flags);
#endif
2022-10-28 14:59:49 +11:00
if (flags) {
fprintf(stderr, "in %s, flags muste be 0x0000\n", __func__);
return WRONG_FLAG;
}
/* initialisation de variables de debug/comptage */
2022-06-26 20:06:35 +11:00
#if DEBUG_LEVEL
hit = mess = 0L;
#endif
2022-10-28 14:59:49 +11:00
for (xd=0; xd<dst->width; xd++) {
2022-06-26 20:06:35 +11:00
dxs = (double)xd * kx;
2022-10-28 14:59:49 +11:00
for (yd=0; yd<dst->height; yd++) {
2022-06-26 20:06:35 +11:00
rd = gd = bd = 42;
dys = (double)yd * ky; /* kikoo lol */
xs = (int)dxs;
ys = (int)dys;
2022-10-28 14:59:49 +11:00
if ( Image_xy_inside(src, xs, ys) ) {
2022-06-26 20:06:35 +11:00
Image_getRGB(src, xs, ys, &rd, &gd, &bd);
#if DEBUG_LEVEL
hit++;
#endif
}
#if DEBUG_LEVEL
2022-10-28 14:59:49 +11:00
else { mess++; }
2022-06-26 20:06:35 +11:00
#endif
Image_plotRGB(dst, xd, yd, rd, gd, bd);
}
}
#if DEBUG_LEVEL
fprintf(stderr, "%s -> hit=%ld mess=%ld\n", __func__, hit, mess);
#endif
return BAD_ZOOM;
}
/*::------------------------------------------------------------------::*/
2022-10-28 14:59:49 +11:00
/*
* Very old non-working code get an update on Thu 24 Sep 2022
*
* We have two versions:
* zoom_000 without interpolation
* zoom_001 with a pseudo bi-linear interpolation
* Public intercace:
* Image_zoom_extract(src, dst, rect, flags)
*/
/* this is a semi-private function */
int Image_zoom_000(Image_Desc *src, Image_Desc *dst, Image_Rect *rect,
float kx, float ky)
2022-06-26 20:06:35 +11:00
{
2022-10-28 14:59:49 +11:00
int xd, yd, xs, ys, r, g, b;
float ftmp;
2022-06-26 20:06:35 +11:00
#if DEBUG_LEVEL
2022-10-28 14:59:49 +11:00
fprintf(stderr, ">>> %s ( %p %p %p )\n", __func__, src, dst, rect);
2022-06-26 20:06:35 +11:00
#endif
2022-10-28 14:59:49 +11:00
for (yd=0; yd<dst->height; yd++) {
ftmp = ((float)yd) / ky;
ys = (int) (ftmp);
ys += rect->y;
/*
fprintf(stderr, " yd %5d -> %8.3f ys %5d\n", yd, ftmp, ys);
*/
for (xd=0; xd<dst->width; xd++) {
xs = (int) ((float)xd / kx);
xs += rect->x;
if ( Image_xy_inside(src, xs, ys) )
{ Image_getRGB(src, xs, ys, &r, &g, &b); }
else
{ r = g = b = 127; }
Image_plotRGB(dst, xd, yd, r, g, b);
}
}
return FUNC_IS_BETA;
}
/*::------------------------------------------------------------------::*/
/* */
/* nouveau Fri 23 Sep 2022 02:27:00 PM CEST */
/* this is a semi-private function */
/* */
int Image_zoom_001(Image_Desc *src, Image_Desc *dst, Image_Rect *rect,
float kx, float ky)
{
int xd, yd, xs, ys, r, g, b;
double dxs, dys;
#if DEBUG_LEVEL
fprintf(stderr, ">>> %s ( %p %p %p %.3f %.3f)\n", __func__,
src, dst, rect, kx, ky);
#endif
2022-06-26 20:06:35 +11:00
2022-10-28 14:59:49 +11:00
for (yd=0; yd<dst->height; yd++) {
dys = (double)rect->y + (((double)yd) / ky);
ys = (int)dys;
for (xd=0; xd<dst->width; xd++) {
dxs = (double)rect->x + ((double)xd / kx);
xs = (int)dxs;
if ( Image_xy_inside(src, xs, ys) )
{
/* see 'scale.c' for this func */
Image_getpix_bilin(src, dxs, dys, &r, &g, &b);
}
else
{ r = g = b = 127; }
Image_plotRGB(dst, xd, yd, r, g, b);
2022-06-26 20:06:35 +11:00
}
}
2022-10-28 14:59:49 +11:00
return FUNC_IS_BETA;
}
/*::------------------------------------------------------------------::*/
/* */
/* nouveau Fri 23 Sep 2022 02:16:55 PM CEST */
/* */
int Image_zoom_extract(Image_Desc *src, Image_Desc *dst, Image_Rect *rect,
int flags)
{
int foo;
float kx, ky;
#if DEBUG_LEVEL
fprintf(stderr, ">>> %s ( %p %p %p 0x%x)\n", __func__, src, dst, rect, flags);
#endif
kx = (float)dst->width / (float)rect->w;
ky = (float)dst->height / (float)rect->h;
#if DEBUG_LEVEL
fprintf(stderr, " kx %8.3f ky %8.3f\n", kx, ky);
#endif
/* this is ugly code ! */
if (flags)
{ foo = Image_zoom_001(src, dst, rect, kx, ky); }
else
{ foo = Image_zoom_000(src, dst, rect, kx, ky); }
if (foo) {
fprintf(stderr, "got %d in %s\n", foo, __func__);
return foo;
}
return FUNC_IS_ALPHA;
2022-06-26 20:06:35 +11:00
}
/*::------------------------------------------------------------------::*/