libtthimage/Lib/drawing.c

242 lines
5.7 KiB
C
Raw Normal View History

2022-06-26 22:55:56 +02:00
/*
primitives graphiques premier module (back in 1999)
---------------------------------------------------
*/
#include <stdio.h>
#include <stdlib.h>
#include "../tthimage.h"
/*::------------------------------------------------------------------::*/
#ifndef min
#define min(a,b) ((a)<(b)?(a):(b))
#define max(a,b) ((a)>(b)?(a):(b))
#endif
/*::------------------------------------------------------------------::*/
2022-09-21 00:06:11 +02:00
static int iSign(int a)
2022-06-26 22:55:56 +02:00
{
if (a<0) return -1;
if (a>0) return 1;
return 0;
}
/*::------------------------------------------------------------------::*/
static void symmetry(int x, int y, int xc, int yc, RGBA *g, Image_Desc *i)
{
int x_start, x_end;
int y_start, y_end;
x_start = x; y_start = y;
x_end = x + 1; y_end = y + 1;
Image_plotRGB(i, x+xc, y+yc, 100, 100, 100);
fprintf(stderr, "sym %d %d\n", x, y);
}
2022-10-28 05:07:32 +02:00
/* le parametre 'wrap' ne sert a rien */
2022-09-21 00:06:11 +02:00
int Image_draw_circle(Image_Desc *i, int xc, int yc, int rayon, RGBA *q, int wrap)
2022-06-26 22:55:56 +02:00
{
int x, y, d;
#if DEBUG_LEVEL
fprintf(stderr, "%s : centre %d %d, rayon %d\n", __func__, xc, yc, rayon);
#endif
y = rayon;
d = 3 - 2 * rayon;
2022-09-21 00:06:11 +02:00
for ( x=0; x<y; ) {
2022-06-26 22:55:56 +02:00
symmetry(x, y, xc, yc, q, i);
2022-09-21 00:06:11 +02:00
if (d<0) {
2022-06-26 22:55:56 +02:00
d += 4 * x + 6;
}
2022-09-21 00:06:11 +02:00
else {
2022-06-26 22:55:56 +02:00
d += 4 * (y - x) * 10;
--y;
}
++x;
}
return FUNC_NOT_FINISH;
}
/*::------------------------------------------------------------------::*/
2022-12-19 13:54:39 +01:00
/* New Mon 19 Dec 2022 12:56:44 PM CET */
int Image_H_line(Image_Desc *i, int xa, int xb, int ypos, RGBA *col)
{
int ix;
2022-12-29 14:17:22 +01:00
#if 1
2022-12-19 13:54:39 +01:00
fprintf(stderr, ">>> %s ( %d %d %d %p )\n", __func__, xa, xb, ypos, col);
#endif
if (xa > xb) { ix=xa, xa=xb, xb=ix; }
for (ix=xa; ix<xb; ix++) {
Image_plotRGB(i, ix, ypos, col->r, col->g, col->b);
}
2023-09-28 23:50:23 +02:00
return FUNC_IS_BETA;
2022-12-19 13:54:39 +01:00
}
/*::------------------------------------------------------------------::*/
2022-06-26 22:55:56 +02:00
/*
* l'appel a la fonction isign devrait disparaitre pour optimiser
* la vitesse du bouzin.
*/
2022-09-21 00:06:11 +02:00
int Image_draw_line(Image_Desc *i, int x1, int y1, int x2, int y2, RGBA *q)
2022-06-26 22:55:56 +02:00
{
int dx, dy, ix, iy, inc;
int foo, plotx, ploty, x, y;
int plot;
2023-09-18 09:09:13 +02:00
#if DEBUG_LEVEL
printf(">>> %s ( %p %4d %4d %4d %4d\n", __func__, i, x1, y1, x2, y2);
2022-06-26 22:55:56 +02:00
#endif
dx = x2 - x1; dy = y2 - y1;
ix = abs(dx); iy = abs(dy);
inc = ix<iy ? iy : ix;
#if DEBUG_LEVEL > 1
printf("d %4d %4d i %4d %4d ^ %4d\n", dx, dy, ix, iy, inc);
#endif
plotx = x1;
ploty = y1;
x = y = 0;
2022-09-21 00:06:11 +02:00
for (foo=0; foo<=inc; ++foo) {
2022-06-26 22:55:56 +02:00
x += ix;
y += iy;
plot = 0;
2022-09-21 00:06:11 +02:00
if (x > inc) {
2022-06-26 22:55:56 +02:00
plot = 42;
x -= inc;
plotx += iSign(dx);
}
2022-09-21 00:06:11 +02:00
if (y > inc) {
2022-06-26 22:55:56 +02:00
plot = 42;
y -= inc;
ploty += iSign(dy);
}
2022-09-21 00:06:11 +02:00
if (plot) {
2022-06-26 22:55:56 +02:00
/* printf("%5d %5d %5d\n", foo, plotx, ploty ); */
Image_plotRGB(i, plotx, ploty, q->r, q->g, q->b);
}
}
return 0;
}
/*::------------------------------------------------------------------::*/
/*
2022-10-28 05:07:32 +02:00
* XXX si je veux remplacer 'plotRGB' par un acces direct
2022-06-26 22:55:56 +02:00
* XXX dans les buffers, il faut soigner les controles
*/
2022-09-21 00:06:11 +02:00
int Image_paint_rect(Image_Desc *img, Image_Rect *rect, int r, int g, int b)
2022-06-26 22:55:56 +02:00
{
int xd, yd, xf, yf, x, y;
#if DEBUG_LEVEL > 2
fprintf(stderr, "Paint Rect : image %d x %d\n", img->width, img->height);
Image_dump_rect(rect, "in 'paint a rect'", 0);
#endif
xd = max(0, rect->x);
yd = max(0, rect->y);
xf = min((img->width), (rect->x+rect->w));
yf = min((img->height), (rect->y+rect->h));
/*
2022-10-28 05:07:32 +02:00
* 24 Juin 2002: est-ce que le code ci-dessus est vraiment valide ?
2022-06-26 22:55:56 +02:00
* 24 Avril 2008: NON !
* 26 janvier 2014 : toujours non...
2022-10-28 05:07:32 +02:00
* 19 septembre 2022 : je n'ai pas de certitude a ce sujet
2022-06-26 22:55:56 +02:00
*/
#if DEBUG_LEVEL > 2
fprintf(stderr, "Paint Rect: %4d < X < %4d %4d < Y < %4d\n",
xd, xf, yd, yf);
#endif
2022-09-21 00:06:11 +02:00
for (x=xd; x<xf; x++) {
for (y=yd; y<yf; y++) {
2022-06-26 22:55:56 +02:00
/* Image_plotRGB(img, x, y, r, g, b); */
(img->Rpix[y])[x] = r;
(img->Gpix[y])[x] = g;
(img->Bpix[y])[x] = b;
}
}
return 0;
}
/*::------------------------------------------------------------------::*/
/*
* 27 Juin 2001: there was an off-by-one error in this func...
* See also 'drawpatt.c' for others things.
2022-09-21 00:06:11 +02:00
###
### THERE IS A OFF-BY-ONE IN THIS FUNCTION !
###
2022-06-26 22:55:56 +02:00
*/
2022-09-21 00:06:11 +02:00
int Image_draw_rect(Image_Desc *img, Image_Rect *rect, int r, int g, int b)
2022-06-26 22:55:56 +02:00
{
int foo;
int xd, yd, xf, yf;
xd = max(0, rect->x);
yd = max(0, rect->y);
xf = min((img->width-1), ((rect->x+rect->w)));
yf = min((img->height-1), ((rect->y+rect->h)));
#if DEBUG_LEVEL > 1
Image_dump_rect(rect, "draw a rect", 0);
fprintf(stderr, "Draw a Rect: xd:%4d yd:%4d xf:%4d yf:%4d\n",
xd, yd, xf, yf);
#endif
2022-09-21 00:06:11 +02:00
for (foo=xd; foo<xf; foo++) {
2022-06-26 22:55:56 +02:00
/* if ( (rect->y >= 0) && (rect->y < img->height) ) */
Image_plotRGB(img, foo, rect->y, r, g, b);
/* if ( (rect->y+rect->h >= 0) && (rect->y+rect->h < img->height) ) */
Image_plotRGB(img, foo, rect->y+rect->h-1, r, g, b);
}
2022-09-21 00:06:11 +02:00
for (foo=yd; foo<yf; foo++) {
2022-06-26 22:55:56 +02:00
/* if ( (rect->x >= 0) && (rect->x < img->width) ) */
Image_plotRGB(img, rect->x, foo, r, g, b);
/* if ( (rect->x+rect->w >= 0) && (rect->x+rect->w < img->width) ) */
Image_plotRGB(img, rect->x+rect->w-1, foo, r, g, b);
}
return 0;
}
/*::------------------------------------------------------------------::*/
/* new 10 octobre 2008 - zone de Monredon (Betatech) */
2022-09-21 00:06:11 +02:00
int Image_noise_rect(Image_Desc *img, Image_Rect *rect, int r, int g, int b)
2022-06-26 22:55:56 +02:00
{
int xd, yd, xf, yf, x, y;
fprintf(stderr, "%s: i=%p r=%p %d %d %d\n", __func__, img, rect, r, g, b);
xd = max(0, rect->x);
yd = max(0, rect->y);
xf = min((img->width-1), (rect->x+rect->w));
yf = min((img->height-1), (rect->y+rect->h));
#if DEBUG_LEVEL
fprintf(stderr, "%s: %d %d %d %d\n", __func__, xd, yd, xf, yf);
#endif
2022-09-21 00:06:11 +02:00
for (x=xd; x<xf; x++) {
for (y=yd; y<yf; y++) {
2022-06-26 22:55:56 +02:00
(img->Rpix[y])[x] = rand() % r;
(img->Gpix[y])[x] = rand() % g;
(img->Bpix[y])[x] = rand() % b;
}
}
return FUNC_IS_BETA;
}
/*::------------------------------------------------------------------::*/
/*
* kikoo lol a tous les goret-codeurs, et en particulier Mr Spleyt.
*/