libtthimage/Lib/tools.c

359 lines
8.9 KiB
C
Raw Normal View History

2022-06-26 20:06:35 +11:00
/*
TOOLS.C outils de mise au point
(debugging tools)
*/
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <time.h>
2022-06-27 07:55:56 +11:00
#include "../tthimage.h"
2022-06-26 20:06:35 +11:00
/*::------------------------------------------------------------------::*/
/*
* WARNING! this function is not re-entrant!
*/
2022-09-21 09:06:11 +11:00
void Image_pacifier(char *texte, int div)
2022-06-26 20:06:35 +11:00
{
static int compteur;
int foo;
char batons[] = "|/-\\";
if ( texte != NULL ) printf("%s : ", texte);
else printf("wait ");
if (div<1) div=1;
foo = (compteur++) / div;
printf("%c\r", batons[foo&0x03]); fflush(stdout);
}
/*::------------------------------------------------------------------::*/
2023-10-12 07:36:16 +11:00
/*
* I think that the 'chrono' system is full broken
*/
2022-06-26 20:06:35 +11:00
#define NB_CHRONOS 42
2022-09-21 09:06:11 +11:00
static struct {
2022-06-26 20:06:35 +11:00
int in_use;
time_t debut;
time_t fin;
} chronos[NB_CHRONOS];
2022-09-21 09:06:11 +11:00
int Image_start_chrono(char *texte, int num)
2022-06-26 20:06:35 +11:00
{
2022-09-21 09:06:11 +11:00
if (num< 0 || num>= NB_CHRONOS) {
2022-06-26 20:06:35 +11:00
return BAD_CHRONO;
}
2022-09-21 09:06:11 +11:00
if (NULL != texte) {
2022-06-26 20:06:35 +11:00
fprintf(stderr, ">>> start chrono(%d) %s\n", num, texte);
}
time(&chronos[num].debut);
chronos[num].in_use = 1;
return OLL_KORRECT;
}
2022-09-21 09:06:11 +11:00
long Image_stop_chrono(char *texte, int num)
2022-06-26 20:06:35 +11:00
{
long delta_t;
2022-09-21 09:06:11 +11:00
if (num < 0 || num >= NB_CHRONOS) {
2022-06-26 20:06:35 +11:00
return -1L;
}
time(&chronos[num].fin);
chronos[num].in_use = 0;
delta_t = chronos[num].fin - chronos[num].debut;
if (texte != NULL)
fprintf(stderr, ">>> chrono(%d): %s: %ld seconds\n", num, texte, delta_t);
return delta_t;
}
/*::------------------------------------------------------------------::*/
2022-09-21 09:06:11 +11:00
int Image_dump_colormap(RGB_map *map, int verbose)
2022-06-26 20:06:35 +11:00
{
int foo;
2022-09-21 09:06:11 +11:00
if (map == NULL) {
fprintf(stderr, "%s: 'map' ptr is NULL\n", __func__);
2022-06-26 20:06:35 +11:00
return NULL_DESCRIPTOR;
}
printf("+---------------------------\n");
printf("| nom de la palette : %s\n", map->name);
printf("| nombre de couleurs : %d\n", map->nbre);
printf("+---------------------------\n");
2022-09-21 09:06:11 +11:00
if (verbose) {
for (foo=0; foo<map->nbre; foo++) {
2022-06-26 20:06:35 +11:00
printf("%3d: %3d %3d %3d\n", foo,
map->red[foo], map->green[foo], map->blue[foo]);
}
}
return 0;
}
/*::------------------------------------------------------------------::*/
2022-09-21 09:06:11 +11:00
int Image_dump_descriptor(Image_Desc *im, char *text)
2022-06-26 20:06:35 +11:00
{
fflush(stdout);
printf("/-------- Descriptor Dump (lib v=%s) -----------\n",
IMAGE_VERSION_STRING);
printf("| why text: %s\n", text);
printf("| main pointer: %p\n", im);
2023-10-12 07:36:16 +11:00
if ( NULL == im ) {
2022-06-26 20:06:35 +11:00
printf("!\t\tthis is a null descriptor, good luck, Sir...\n");
return NULL_DESCRIPTOR;
}
2022-09-21 09:06:11 +11:00
else {
2022-06-26 20:06:35 +11:00
printf("| name: %s\n", im->name);
printf("| comment: %s\n", im->comment);
printf("| magic: 0x%08lx\n", im->magic);
printf("| dimensions: %d x %d\n", im->width, im->height);
printf("| type/nb_plan: %d (%s), %d\n", im->type,
Image_type2str(im->type),
im->nb_planes);
printf("| planes pointers: %p %p %p %p\n",
im->Rpix, im->Gpix, im->Bpix, im->Apix);
printf("| errmsg/modified : %d / %d\n", im->errmsg, im->modified);
printf("| xy fenetre : %15f %15f\n", im->fx, im->fy);
printf("| wh fenetre : %15f %15f\n", im->fw, im->fh);
}
printf("\\-----------------------------------------------------------\n");
fflush(stdout);
return 0;
}
/*::------------------------------------------------------------------::*/
2022-09-21 09:06:11 +11:00
int Image_dump_rect(Image_Rect *rect, char *texte, int flag)
2022-06-26 20:06:35 +11:00
{
2023-10-12 07:36:16 +11:00
printf("Rect: '%s'\n\t%5d %5d %5d %5d $%04lx\n", texte,
2022-06-26 20:06:35 +11:00
rect->x, rect->y, rect->w, rect->h, rect->reserved);
if (flag) {
2023-10-12 07:36:16 +11:00
printf(" lower right %5d %5d\n", rect->x+rect->w, rect->y+rect->h);
printf(" surface %8ld\n", (long)rect->w * (long)rect->h);
2022-06-26 20:06:35 +11:00
}
fflush(stdout);
return OLL_KORRECT;
}
/*::------------------------------------------------------------------::*/
/* new 8 octobre 2009 */
static void ligne_char(char c, int t)
{
int foo;
for (foo=0; foo<t; foo++)
putc(c, stderr);
putc('\n', stderr);
}
static void ligne_space(char c, int t)
{
int foo;
putc(c, stderr);
for (foo=0; foo<t-2; foo++)
putc(' ', stderr);
putc(c, stderr);
putc('\n', stderr);
}
void Image_warning(char caractere, char *lig1, char *lig2, int k)
{
2023-09-18 18:09:13 +11:00
int len1, len2, len;
2022-06-26 20:06:35 +11:00
len1 = len2 = 0;
if (NULL != lig1) len1 = strlen(lig1);
2022-08-23 02:53:30 +11:00
if (NULL != lig2) len2 = strlen(lig2);
2022-06-26 20:06:35 +11:00
if (len2 > len1) len = len2;
else len = len1;
#if DEBUG_LEVEL
fprintf(stderr, "%s : largeur = %d\n", __func__, len);
fprintf(stderr, "%s : ligne 1 = %s\n", __func__, lig1);
fprintf(stderr, "%s : ligne 2 = %s\n", __func__, lig2);
#endif
2023-09-29 08:50:23 +11:00
ligne_char(caractere, len+10+k);
ligne_space(caractere, len+10+k);
ligne_char(caractere, len+10+k);
2022-06-26 20:06:35 +11:00
}
/*::------------------------------------------------------------------::*/
2022-09-21 09:06:11 +11:00
int Image_print_rgba(char *txt, RGBA *rgba, int hexa)
2022-06-26 20:06:35 +11:00
{
char *fmtd = "%-20s: RGBA %4d %4d %4d %4d\n";
char *fmth = "%-20s: 0xRGBA %02x %02x %02x %02x\n";
FILE *fp;
if (*txt == '-') {
fp = stdout;
txt++; /* skip the '-' */
}
2023-10-12 07:36:16 +11:00
else {
2022-06-26 20:06:35 +11:00
fp = stderr;
}
fprintf(fp, hexa ? fmth : fmtd, txt, rgba->r, rgba->g, rgba->b, rgba->a);
fflush(fp);
return OLL_KORRECT;
}
/*::------------------------------------------------------------------::*/
/* affichage d'un descripteur de DF3 - new 28feb2008 - ave St Exupery */
2022-09-21 09:06:11 +11:00
int Image_print_DF3head(DensityF3Head *h, char *txt, int flag)
2022-06-26 20:06:35 +11:00
{
int retval = 0;
int foo;
2022-09-21 09:06:11 +11:00
if (NULL == h) {
2022-06-26 20:06:35 +11:00
fprintf(stderr, "%s: null pointer\n", __func__);
return 666;
}
2022-09-21 09:06:11 +11:00
if (h->control != MAGIC_OF_DF3) {
2022-06-26 20:06:35 +11:00
fprintf(stderr, "%s: bad magic value\n", __func__);
return 666;
}
2023-09-29 08:50:23 +11:00
printf("+--- DF3head @ %p -- %s -- %c\n", h, txt, flag ? 'X' : 'O');
2022-06-26 20:06:35 +11:00
printf("| magic: %08lx\n", h->control);
printf("| name: %s\n", h->name);
printf("| dims: %d %d %d\n", h->xdim, h->ydim, h->zdim);
foo = h->nbrbytes;
printf("| nbr bytes: %d ", foo);
2022-09-21 09:06:11 +11:00
if ( (foo!=1) && (foo!=2) && (foo!=4) ) {
2022-06-26 20:06:35 +11:00
printf("**ERROR**\n");
retval++;
}
2022-09-21 09:06:11 +11:00
else {
2022-06-26 20:06:35 +11:00
printf("\n");
}
printf("| flags: %08x\n", h->flags);
printf("| scale: %.g\n", h->scale);
printf("| datas @ : %p\n", h->datas);
printf("+------------------------------------\n");
return FUNC_IS_ALPHA;
}
/*::------------------------------------------------------------------::*/
2022-09-21 09:06:11 +11:00
int Image_Overlay_Palette(Image_Desc *im, int xo, int yo)
2022-06-26 20:06:35 +11:00
{
int x, y, xx, yy, code;
int r, g, b;
/* No boundchecking here ? */
2022-09-21 09:06:11 +11:00
for (y=0; y<128; y++) {
2022-06-26 20:06:35 +11:00
yy = y + yo;
2022-09-21 09:06:11 +11:00
if ( (yy >= 0) && (yy<im->height) ) {
for (x=0; x<256; x++) {
2022-06-26 20:06:35 +11:00
xx = x + xo;
2022-09-21 09:06:11 +11:00
if ( (xx >= 0) && (xx<im->width) ) {
2022-06-26 20:06:35 +11:00
code = (y<<8) | x;
r = (code & 0x1f) * 4;
b = ((code>>5) & 0x1f) * 4;
g = ((code>>10) & 0x1f) * 4;
Image_plotRGB(im, xx, yy, r, g, b);
}
}
}
}
im->modified = 1;
return 0;
}
/*::------------------------------------------------------------------::*/
/*
* this function need more security controls
* see also: mircol.c
*/
2022-09-21 09:06:11 +11:00
int Image_fabrique_une_mire(Image_Desc *im, RGB_map *map)
2022-06-26 20:06:35 +11:00
{
int foo, bar;
#if DEBUG_LEVEL
fprintf(stderr, "%s : %p %p\n", __func__, im, map);
#endif
if ( (im->width < 320) || (im->width<200) )
return IMAGE_TOO_SMALL;
2022-09-21 09:06:11 +11:00
if (NULL != map) {
2022-06-26 20:06:35 +11:00
fprintf(stderr, "%s:%s map must be null\n", __FILE__, __func__);
exit(5);
}
Image_Overlay_Palette(im, 50, 10);
2022-09-21 09:06:11 +11:00
for (foo=0; foo<256; foo++) {
for (bar=0; bar<10; bar++) {
2022-06-26 20:06:35 +11:00
Image_plotRGB(im, foo, bar, foo, foo, foo);
Image_plotRGB(im, foo, 190+bar, 256-foo, 256-foo, 256-foo);
}
}
im->modified = 1;
return OLL_KORRECT;
}
/*::------------------------------------------------------------------::*/
/*
* This func make a grid over an image, and need a lot of work.
*/
2022-09-21 09:06:11 +11:00
int Image_Grille(Image_Desc *im, int stepx, int stepy, int rgb)
2022-06-26 20:06:35 +11:00
{
int foo, bar;
int r, g, b;
if (rgb < 0) {
r = rand() % 256;
g = rand() % 256;
b = rand() % 256;
}
else {
r = g = b = rgb;
}
2022-09-21 09:06:11 +11:00
for (foo=0; foo<im->width; foo+=stepx) {
2022-06-26 20:06:35 +11:00
for (bar=0; bar<im->height; bar++)
Image_plotRGB(im, foo, bar, r, g, b);
}
2022-09-21 09:06:11 +11:00
for (foo=0; foo<im->height; foo+=stepy) {
2022-06-26 20:06:35 +11:00
for (bar=0; bar<im->width; bar++)
Image_plotRGB(im, bar, foo, r, g, b);
}
im->modified = 1;
return OLL_KORRECT;
}
/*::------------------------------------------------------------------::*/
2022-09-21 09:06:11 +11:00
int Image_print_minmax(Image_Desc *img)
2022-06-26 20:06:35 +11:00
{
int res[8], foo;
printf(" R G B A\n");
foo = Image_minmax_RGB(img, res);
2022-08-23 02:53:30 +11:00
if (foo) {
fprintf(stderr, "in %s, minmax rgb -> %d\n", __func__, foo);
}
2022-06-26 20:06:35 +11:00
printf("min %4d %4d %4d %4d\n", res[0], res[2], res[4], res[6]);
printf("max %4d %4d %4d %4d\n", res[1], res[3], res[5], res[7]);
return OLL_KORRECT;
}
/*::------------------------------------------------------------------::*/
int Image_xy_inside(Image_Desc *img, int x, int y)
{
/*
* 1er mars 2010 : this func need a g77 binding
2022-09-21 09:06:11 +11:00
* ???
2022-06-26 20:06:35 +11:00
*/
2022-09-21 09:06:11 +11:00
if ( (x<0) || (y<0) || (x>=img->width) || (y>=img->height) ) {
2022-06-26 20:06:35 +11:00
#if DEBUG_LEVEL > 1
fprintf(stderr, "%s : %p %6d %6d --> FAIL\n", __func__, img, x, y);
#endif
return 0;
}
return 1;
}
/*::------------------------------------------------------------------::*/