mirror of
https://github.com/eddyem/improclib.git
synced 2026-03-20 08:40:57 +03:00
first - simplest - actions
This commit is contained in:
11
examples/CMakeLists.txt
Normal file
11
examples/CMakeLists.txt
Normal file
@@ -0,0 +1,11 @@
|
||||
cmake_minimum_required(VERSION 3.20)
|
||||
project(examples)
|
||||
|
||||
# common includes & library
|
||||
include_directories(../)
|
||||
link_libraries(usefull_macros improc m)
|
||||
|
||||
# exe list
|
||||
add_executable(equalize equalize.c)
|
||||
add_executable(generate generate.c)
|
||||
add_executable(genu16 genu16.c)
|
||||
19
examples/Readme.md
Normal file
19
examples/Readme.md
Normal file
@@ -0,0 +1,19 @@
|
||||
Examples
|
||||
========
|
||||
|
||||
## equalize
|
||||
Open given image file as 1-channel uint8_t, equalize histogram, plot two crosses (red at 30,30 and green at 150,50) ans save as output.jpg
|
||||
|
||||
## generate
|
||||
Generate pseudo-star images with given Moffat parameters at given coordinates xi,yi (with amplitude ampi, ampi < 256)
|
||||
Usage: %s [args] x1,y1[,amp1] x2,y2[,amp2] ... xn,yn[,amp3]
|
||||
args:
|
||||
|
||||
- w - resulting image width (default: 1024)
|
||||
- h - resulting image height (default: 1024)
|
||||
- o - output file name (default: output.jpg)
|
||||
- s - FWHM of 'star' images (default: 3.5)
|
||||
- b - beta Moffat parameter of 'star' images (default: 1)
|
||||
|
||||
## genu16
|
||||
The same as 'generate', but works with 16-bit image and save it as 1-channel png (`ampi` now is weight).
|
||||
50
examples/equalize.c
Normal file
50
examples/equalize.c
Normal file
@@ -0,0 +1,50 @@
|
||||
/*
|
||||
* This file is part of the improclib project.
|
||||
* Copyright 2023 Edward V. Emelianov <edward.emelianoff@gmail.com>.
|
||||
*
|
||||
* 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 3 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, see <http://www.gnu.org/licenses/>.
|
||||
*/
|
||||
|
||||
#include "improclib.h"
|
||||
#include <stdio.h>
|
||||
#include <usefull_macros.h>
|
||||
|
||||
int main(int argc, char **argv){
|
||||
if(argc != 2){
|
||||
fprintf(stderr, "Usage: %s filename - open bw image file, equalize histogram, plot two crosses ans save as output.jpg\n", argv[0]);
|
||||
return 1;
|
||||
}
|
||||
ilImage *I = ilImage_read(argv[1]);
|
||||
if(!I){
|
||||
fprintf(stderr, "Can't read %s\n", argv[1]);
|
||||
return 2;
|
||||
}
|
||||
int w = I->width, h = I->height;
|
||||
uint8_t *eq = ilequalize8(I, 3, 0.1);
|
||||
ilImage_free(&I);
|
||||
if(!eq) return 3;
|
||||
ilImg3 *I3 = MALLOC(ilImg3, 1);
|
||||
I3->data = eq;
|
||||
I3->height = h;
|
||||
I3->width = w;
|
||||
ilPattern *cross = ilPattern_xcross(25, 25);
|
||||
ilPattern_draw3(I3, cross, 30, 30, ilColor_red);
|
||||
ilPattern_draw3(I3, cross, 150, 50, ilColor_green);
|
||||
ilPattern_free(&cross);
|
||||
int ret = ilImg3_jpg("output.jpg", I3, 95);
|
||||
ilImg3_free(&I3);
|
||||
if(!ret) return 4;
|
||||
printf("File 'output.jpg' ready\n");
|
||||
return 0;
|
||||
}
|
||||
103
examples/generate.c
Normal file
103
examples/generate.c
Normal file
@@ -0,0 +1,103 @@
|
||||
/*
|
||||
* This file is part of the improclib project.
|
||||
* Copyright 2023 Edward V. Emelianov <edward.emelianoff@gmail.com>.
|
||||
*
|
||||
* 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 3 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, see <http://www.gnu.org/licenses/>.
|
||||
*/
|
||||
|
||||
#include "improclib.h"
|
||||
#include <usefull_macros.h>
|
||||
#include <stdio.h>
|
||||
#include <string.h>
|
||||
|
||||
static int w = 1024, h = 1024, help = 0;
|
||||
static double fwhm = 3.5, beta = 1.;
|
||||
static char *outp = "output.jpg", *inp = NULL;
|
||||
|
||||
static ilPattern *star = NULL, *cross = NULL;
|
||||
|
||||
static myoption cmdlnopts[] = {
|
||||
{"help", NO_ARGS, NULL, '?', arg_int, APTR(&help), "show this help"},
|
||||
{"width", NEED_ARG, NULL, 'w', arg_int, APTR(&w), "resulting image width (default: 1024)"},
|
||||
{"height", NEED_ARG, NULL, 'h', arg_int, APTR(&h), "resulting image height (default: 1024)"},
|
||||
{"output", NEED_ARG, NULL, 'o', arg_string, APTR(&outp), "output file name (default: output.jpg)"},
|
||||
{"halfwidth",NEED_ARG, NULL, 's', arg_double, APTR(&fwhm), "FWHM of 'star' images (default: 3.5)"},
|
||||
{"beta", NEED_ARG, NULL, 'b', arg_double, APTR(&beta), "beta Moffat parameter of 'star' images (default: 1)"},
|
||||
{"input", NEED_ARG, NULL, 'i', arg_string, APTR(&inp), "input file with coordinates and amplitudes (comma separated)"},
|
||||
end_option
|
||||
};
|
||||
|
||||
static int getpars(const char *argv, int *x, int *y, int *a){
|
||||
char *eptr, *start = (char*)argv;
|
||||
long l;
|
||||
l = strtol(start, &eptr, 0);
|
||||
if(eptr == start || *eptr != ',' || l < 0 || l > INT_MAX) return FALSE;
|
||||
*x = (int)l;
|
||||
start = eptr + 1;
|
||||
l = strtol(start, &eptr, 0);
|
||||
if(eptr == start || l < 0 || l > INT_MAX) return FALSE;
|
||||
*y = (int)l;
|
||||
if(*eptr == ','){
|
||||
start = eptr + 1;
|
||||
l = strtol(start, &eptr, 0);
|
||||
if(eptr == start || l < 0 || l > 255) return FALSE;
|
||||
*a = (int)l;
|
||||
}else *a = 255;
|
||||
return TRUE;
|
||||
}
|
||||
|
||||
static void addstar(ilImg3 *I, const char *str){
|
||||
int x, y, a;
|
||||
if(!getpars(str, &x, &y, &a)) return;
|
||||
printf("Add 'star' at %d,%d (ampl=%d)\n", x,y,a);
|
||||
uint8_t c[3] = {a,a,a};
|
||||
ilPattern_draw3(I, star, x, y, c);
|
||||
ilPattern_draw3(I, cross, x, y, ilColor_red);
|
||||
}
|
||||
|
||||
static void addfromfile(ilImg3 *I){
|
||||
FILE *f = fopen(inp, "r");
|
||||
if(!f){
|
||||
WARN("Can't open %s", inp);
|
||||
return;
|
||||
}
|
||||
char *line = NULL;
|
||||
size_t n = 0;
|
||||
while(getline(&line, &n, f) > 0) addstar(I, line);
|
||||
fclose(f);
|
||||
}
|
||||
|
||||
int main(int argc, char **argv){
|
||||
initial_setup();
|
||||
char *helpstring = "Usage: %s [args] x1,y1[,amp1] x2,y2[,amp2] ... xn,yn[,amp3] - draw 'stars' at coords xi,yi with amplitude ampi (default: 255)\n\n\tWhere args are:\n";
|
||||
change_helpstring(helpstring);
|
||||
parseargs(&argc, &argv, cmdlnopts);
|
||||
if(help) showhelp(-1, cmdlnopts);
|
||||
if(w < 1 || h < 1) ERRX("Wrong image size");
|
||||
if(argc == 0 && inp == NULL) ERRX("Point at least one coordinate pair or file name");
|
||||
ilImg3 *I = ilImg3_new(w, h);
|
||||
if(!I) ERRX("Can't create image %dx%d pixels", w, h);
|
||||
int par = (int)(fwhm*25.);
|
||||
star = ilPattern_star(par, par, fwhm, beta);
|
||||
cross = ilPattern_xcross(25, 25);
|
||||
for(int i = 0; i < argc; ++i) addstar(I, argv[i]);
|
||||
if(inp) addfromfile(I);
|
||||
ilPattern_free(&star);
|
||||
int ret = ilImg3_jpg(outp, I, 95);
|
||||
//int ret = ilImg3_png(outp, I);
|
||||
ilImg3_free(&I);
|
||||
if(!ret) return 4;
|
||||
printf("File %s ready\n", outp);
|
||||
return 0;
|
||||
}
|
||||
112
examples/genu16.c
Normal file
112
examples/genu16.c
Normal file
@@ -0,0 +1,112 @@
|
||||
/*
|
||||
* This file is part of the improclib project.
|
||||
* Copyright 2023 Edward V. Emelianov <edward.emelianoff@gmail.com>.
|
||||
*
|
||||
* 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 3 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, see <http://www.gnu.org/licenses/>.
|
||||
*/
|
||||
|
||||
#include "improclib.h"
|
||||
#include <usefull_macros.h>
|
||||
#include <stdio.h>
|
||||
#include <string.h>
|
||||
|
||||
static int w = 1024, h = 1024, help = 0;
|
||||
static double fwhm = 3.5, beta = 1.;
|
||||
static char *outp = "output.png", *inp = NULL;
|
||||
|
||||
static ilImage *star = NULL;
|
||||
|
||||
static myoption cmdlnopts[] = {
|
||||
{"help", NO_ARGS, NULL, '?', arg_int, APTR(&help), "show this help"},
|
||||
{"width", NEED_ARG, NULL, 'w', arg_int, APTR(&w), "resulting image width (default: 1024)"},
|
||||
{"height", NEED_ARG, NULL, 'h', arg_int, APTR(&h), "resulting image height (default: 1024)"},
|
||||
{"output", NEED_ARG, NULL, 'o', arg_string, APTR(&outp), "output file name (default: output.png)"},
|
||||
{"halfwidth",NEED_ARG, NULL, 's', arg_double, APTR(&fwhm), "FWHM of 'star' images (default: 3.5)"},
|
||||
{"beta", NEED_ARG, NULL, 'b', arg_double, APTR(&beta), "beta Moffat parameter of 'star' images (default: 1)"},
|
||||
{"input", NEED_ARG, NULL, 'i', arg_string, APTR(&inp), "input file with coordinates and amplitudes (comma separated)"},
|
||||
end_option
|
||||
};
|
||||
|
||||
static int getpars(const char *argv, int *x, int *y, double *w){
|
||||
char *eptr, *start = (char*)argv;
|
||||
long l;
|
||||
l = strtol(start, &eptr, 0);
|
||||
if(eptr == start || *eptr != ',' || l < 0 || l > INT_MAX) return FALSE;
|
||||
*x = (int)l;
|
||||
start = eptr + 1;
|
||||
l = strtol(start, &eptr, 0);
|
||||
if(eptr == start || l < 0 || l > INT_MAX) return FALSE;
|
||||
*y = (int)l;
|
||||
if(*eptr == ','){
|
||||
start = eptr + 1;
|
||||
double d = strtod(start, &eptr);
|
||||
if(eptr == start) return FALSE;
|
||||
*w = d;
|
||||
}else *w = 1.;
|
||||
return TRUE;
|
||||
}
|
||||
|
||||
static void addstar(ilImage *I, const char *str){
|
||||
int x, y;
|
||||
double w;
|
||||
if(!getpars(str, &x, &y, &w)) return;
|
||||
printf("Add 'star' at %d,%d (weight=%g)\n", x,y,w);
|
||||
iladd_subimage(I, star, x, y, w);
|
||||
}
|
||||
|
||||
static void addfromfile(ilImage *I){
|
||||
FILE *f = fopen(inp, "r");
|
||||
if(!f){
|
||||
WARN("Can't open %s", inp);
|
||||
return;
|
||||
}
|
||||
char *line = NULL;
|
||||
size_t n = 0;
|
||||
while(getline(&line, &n, f) > 0) addstar(I, line);
|
||||
fclose(f);
|
||||
}
|
||||
|
||||
int main(int argc, char **argv){
|
||||
initial_setup();
|
||||
char *helpstring = "Usage: %s [args] x1,y1[,w1] x2,y2[,w2] ... xn,yn[,w3] - draw 'stars' at coords xi,yi with weight wi (default: 1.)\n\n\tWhere args are:\n";
|
||||
change_helpstring(helpstring);
|
||||
parseargs(&argc, &argv, cmdlnopts);
|
||||
if(help) showhelp(-1, cmdlnopts);
|
||||
if(w < 1 || h < 1) ERRX("Wrong image size");
|
||||
if(argc == 0 && inp == NULL) ERRX("Point at least one coordinate pair or file name");
|
||||
ilImage *I = ilImage_new(w, h, IMTYPE_U16);
|
||||
if(!I) ERRX("Can't create image %dx%d pixels", w, h);
|
||||
int par = (int)(fwhm*25.);
|
||||
star = ilImage_star(IMTYPE_U16, par, par, fwhm, beta);
|
||||
if(!star) ERRX("Can't create 'star' pattern");
|
||||
for(int i = 0; i < argc; ++i) addstar(I, argv[i]);
|
||||
if(inp) addfromfile(I);
|
||||
ilImage_free(&star);
|
||||
ilImage_putstring(I, "Hello, world!!", -10, 10);
|
||||
ilImage_putstring(I, "0", 0, 1016);
|
||||
ilImage_putstring(I, "Hello, world.!?\"'\nMore again", 50, 500);
|
||||
ilImage_putstring(I, "Hello, world!", 950, 1018);
|
||||
for(int _ = 0; _ < 1024; _ += 50){
|
||||
char s[6];
|
||||
snprintf(s, 6, "%d", _);
|
||||
ilImage_putstring(I, s, _, 300);
|
||||
}
|
||||
uint8_t *bytes = ilImage2u8(I, 1);
|
||||
int ret = ilwrite_png(outp, I->width, I->height, 1, bytes);
|
||||
ilImage_free(&I);
|
||||
FREE(bytes);
|
||||
if(!ret) return 4;
|
||||
printf("File %s ready\n", outp);
|
||||
return 0;
|
||||
}
|
||||
Reference in New Issue
Block a user