Skip to content

Commit

Permalink
Merge pull request #39 from haavee/itf
Browse files Browse the repository at this point in the history
  • Loading branch information
danieljprice authored Sep 5, 2022
2 parents a6cefc4 + 6500617 commit 2aacdb9
Show file tree
Hide file tree
Showing 13 changed files with 343 additions and 55 deletions.
4 changes: 2 additions & 2 deletions src/Makefile.am
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@ libgiza_la_SOURCES = giza-annotate.c giza-arrow-style.c giza-arrow.c giza-axis.c
giza-render.c giza-save.c giza-set-font.c giza-stroke.c \
giza-subpanel.c giza-text-background.c giza-text.c giza-tick.c \
giza-transforms.c giza-vector.c giza-viewport.c giza-version.c \
giza-warnings.c giza-window.c giza.c lex.yy.c \
giza-warnings.c giza-window.c giza.c lex.yy.c giza-itf.c\
giza-arrow-style-private.h giza-driver-svg-private.h giza-stroke-private.h \
giza-band-private.h giza-driver-xw-private.h giza-subpanel-private.h \
giza-character-size-private.h giza-drivers-private.h giza-text-background-private.h \
Expand All @@ -33,7 +33,7 @@ libgiza_la_SOURCES = giza-annotate.c giza-arrow-style.c giza-arrow.c giza-axis.c
giza-driver-null-private.h giza-private.h giza-viewport-private.h \
giza-driver-pdf-private.h giza-render-private.h giza-warnings-private.h \
giza-driver-png-private.h giza-set-font-private.h giza-window-private.h \
giza-driver-ps-private.h giza-shared.h giza.h
giza-driver-ps-private.h giza-shared.h giza.h giza-itf.h

libgiza_la_CPPFLAGS = $(X11_CFLAGS) $(CAIRO_CFLAGS) $(FT_CFLAGS) $(FC_CFLAGS)

Expand Down
31 changes: 6 additions & 25 deletions src/giza-colour-index.c
Original file line number Diff line number Diff line change
Expand Up @@ -427,31 +427,12 @@ _giza_init_colour_index (void)
void
giza_set_colour_index_range (int cimin, int cimax)
{
if (cimin < GIZA_COLOUR_INDEX_MIN)
{
_giza_colour_index_min = GIZA_COLOUR_INDEX_MIN;
}
else if (cimin > GIZA_COLOUR_INDEX_MAX)
{
_giza_colour_index_min = GIZA_COLOUR_INDEX_MAX;
}
else
{
_giza_colour_index_min = cimin;
}

if (cimax < GIZA_COLOUR_INDEX_MIN)
{
_giza_colour_index_max = GIZA_COLOUR_INDEX_MIN;
}
else if (cimax > GIZA_COLOUR_INDEX_MAX)
{
_giza_colour_index_max = GIZA_COLOUR_INDEX_MAX;
}
else
{
_giza_colour_index_max = cimax;
}
/* constrain both colour indices to be within GIZA_COLOUR_INDEX_MIN, GIZA_COLOUR_INDEX_MAX */
_giza_colour_index_min = MAX(GIZA_COLOUR_INDEX_MIN, MIN(cimin, GIZA_COLOUR_INDEX_MAX));
_giza_colour_index_max = MIN(GIZA_COLOUR_INDEX_MAX, MAX(cimax, GIZA_COLOUR_INDEX_MIN));
/* Now make sure that colour_index_min <= colour_index_max */
_giza_colour_index_min = MIN(_giza_colour_index_min, _giza_colour_index_max);
_giza_colour_index_max = MAX(_giza_colour_index_min, _giza_colour_index_max);
}

/**
Expand Down
9 changes: 7 additions & 2 deletions src/giza-colour-table.c
Original file line number Diff line number Diff line change
Expand Up @@ -110,8 +110,13 @@ giza_set_colour_table (const double *controlPoints, const double *red, const dou
_giza_colour_table.n = tmpn;
if (tmpn < n)
{
_giza_warning ("giza_set_colour_table", "Invalid values for control points in colour table settings");
return 2;
/* require at least two valid control points in our table to remain! */
if( tmpn<2 ) {
_giza_warning ("giza_set_colour_table", "not enough valid control points in colour table remain after vetting");
return 2;
}
/* otherwise just issue a warning */
_giza_warning ("giza_set_colour_table", "%d invalid values for control points in colour table settings out of %d", tmpn, n);
}

/* use the installed colour table to set the colours of colour indices in the specified range */
Expand Down
22 changes: 17 additions & 5 deletions src/giza-contour.c
Original file line number Diff line number Diff line change
Expand Up @@ -31,11 +31,12 @@
#include "giza-io-private.h"
#include "giza-transforms-private.h"
#include <giza.h>
#include <stdlib.h> /* for abs() */


void
giza_contour (int sizex, int sizey, const double* data, int i1,
int i2, int j1, int j2, int ncont, const double* cont,
int i2, int j1, int j2, int ncont_in, const double* cont,
const double *affine)
{
#define xsect(p1,p2) (h[p2]*xh[p1]-h[p1]*xh[p2])/(h[p2]-h[p1])
Expand Down Expand Up @@ -75,9 +76,12 @@ giza_contour (int sizex, int sizey, const double* data, int i1,

/* set up the line style */
int ls;
int curls, newls;
const int ncont = abs(ncont_in);
const int auto_style = (ncont_in > 0);
giza_get_line_style (&ls);
giza_set_line_style (ls);

curls = ls;
giza_set_line_style (curls);

for (j = (j2 - 1); j >= j1; j--)
{
Expand All @@ -101,7 +105,6 @@ giza_contour (int sizex, int sizey, const double* data, int i1,
{
if (cont[k] < dmin || cont[k] > dmax)
continue;

for (m = 4; m >= 0; m--)
{
/* calculate the relative height of the four corners and centre of the square */
Expand Down Expand Up @@ -201,6 +204,12 @@ giza_contour (int sizex, int sizey, const double* data, int i1,
default:
break;
}
/* compute what the line style should be */
newls = (auto_style ? (cont[k]<0 ? GIZA_LS_DOT : GIZA_LS_SOLID) : ls);
if( newls!=curls ) {
curls = newls;
giza_set_line_style (curls);
}
/* draw the line */
cairo_matrix_transform_point (&mat, &x1, &y1);
cairo_matrix_transform_point (&mat, &x2, &y2);
Expand All @@ -217,6 +226,9 @@ giza_contour (int sizex, int sizey, const double* data, int i1,
/* restore the transformation */
_giza_set_trans (oldTrans);

/* make sure line style returned to previous value */
giza_set_line_style (ls);

/* restore buffering and stroke */
if (!oldBuf)
giza_end_buffer ();
Expand All @@ -239,7 +251,7 @@ giza_contour_float (int sizex, int sizey, const float* data, int i1,
}
}

for (i=0; i<ncont; i++) {
for (i=0; i<abs(ncont); i++) {
dcont[i] = (double) cont[i];
}

Expand Down
8 changes: 4 additions & 4 deletions src/giza-cpgplot.c
Original file line number Diff line number Diff line change
Expand Up @@ -237,7 +237,7 @@ void cpgcons(const float *a, int idim, int jdim, int i1, int i2, \
{
float affine[6];
convert_tr_to_affine(tr,affine);
giza_contour_float(idim,jdim,a,i1-1,i2-1,j1-1,j2-1,abs(nc),c,affine);
giza_contour_float(idim,jdim,a,i1-1,i2-1,j1-1,j2-1,nc,c,affine);
}

/***************************************************************
Expand All @@ -249,7 +249,7 @@ void cpgcont(const float *a, int idim, int jdim, int i1, int i2, \
{
float affine[6];
convert_tr_to_affine(tr,affine);
giza_contour_float(idim,jdim,a,i1-1,i2-1,j1-1,j2-1,abs(nc),c,affine);
giza_contour_float(idim,jdim,a,i1-1,i2-1,j1-1,j2-1,nc,c,affine);
}

/***************************************************************
Expand Down Expand Up @@ -798,7 +798,7 @@ void cpgqinf(const char *item, char *value, int *value_length)
***************************************************************/
void cpgqitf(int *itf)
{

giza_get_image_transfer_function(itf);
}

/***************************************************************
Expand Down Expand Up @@ -1069,7 +1069,7 @@ void cpgshs(float angle, float sepn, float phase)
***************************************************************/
void cpgsitf(int itf)
{

giza_set_image_transfer_function(itf);
}

/***************************************************************
Expand Down
18 changes: 18 additions & 0 deletions src/giza-fortran.F90
Original file line number Diff line number Diff line change
Expand Up @@ -148,6 +148,8 @@ module giza
giza_set_window_equal_scale, &
giza_get_window, &
giza_format_number, &
giza_set_image_transfer_function, &
giza_get_image_transfer_function, &
giza_query_device

#include "giza-shared.h"
Expand Down Expand Up @@ -1718,6 +1720,21 @@ integer(kind=c_int) function giza_query_device_c(qtype,string,rval) bind(C,name=
integer(kind=c_int),intent(out) :: rval
end function giza_query_device_c
end interface

interface giza_set_image_transfer_function
subroutine giza_set_image_transfer_function(itf) bind(C)
import
integer(kind=c_int), value, intent(in) :: itf
end subroutine giza_set_image_transfer_function
end interface

interface giza_get_image_transfer_function
subroutine giza_get_image_transfer_function(itf) bind(C)
import
integer(kind=c_int), intent(out) :: itf
end subroutine giza_get_image_transfer_function
end interface

!------------------ end of interfaces -----------------------

contains
Expand Down Expand Up @@ -2022,6 +2039,7 @@ subroutine giza_query_device_f2c(qtype,string)

end subroutine giza_query_device_f2c


!---------------------------------------------------------------------------
!
! function to safely convert a string to c format (ie. with a terminating
Expand Down
168 changes: 168 additions & 0 deletions src/giza-itf.c
Original file line number Diff line number Diff line change
@@ -0,0 +1,168 @@
/* giza - a scientific plotting library built on cairo
*
* Copyright (c) 2010 James Wetter and Daniel Price
* Copyright (c) 2010-2022 Daniel Price
*
* This library is free software; and you are welcome to redistribute
* it under the terms of the GNU General Public License
* (GPL, see LICENSE file for details) and the provision that
* this notice remains intact. If you modify this file, please
* note section 2a) of the GPLv2 states that:
*
* a) You must cause the modified files to carry prominent notices
* stating that you changed the files and the date of any change.
*
* This software is distributed "AS IS", with ABSOLUTELY NO WARRANTY.
* See the GPL for specific language governing rights and limitations.
*
* The Original code is the giza plotting library.
*
* Contributor(s):
* James Wetter <wetter.j@gmail.com>
* Daniel Price <daniel.price@monash.edu> (main contact)
* Marjolein Verkouter <verkouter@jive.eu>
*/
#include "giza-itf.h"
#include "giza-io-private.h"
#include "giza-private.h"
#include <giza.h>
#include <math.h>


double _giza_itf_linear(const double pixelvalue, const double vmin, const double vmax);
double _giza_itf_log(const double pixelvalue, const double vmin, const double vmax);
double _giza_itf_sqrt(const double pixelvalue, const double vmin, const double vmax);

float _giza_itf_linear_f(const float pixelvalue, const float vmin, const float vmax);
float _giza_itf_log_f(const float pixelvalue, const float vmin, const float vmax);
float _giza_itf_sqrt_f(const float pixelvalue, const float vmin, const float vmax);

int _giza_itf_idx_linear(const double pixelvalue, const double vmin, const double vmax, const int cimin, const int cimax);
int _giza_itf_idx_log(const double pixelvalue, const double vmin, const double vmax, const int cimin, const int cimax);
int _giza_itf_idx_sqrt(const double pixelvalue, const double vmin, const double vmax, const int cimin, const int cimax);

int _giza_itf_idx_linear_f(const float pixelvalue, const float vmin, const float vmax, const int cimin, const int cimax);
int _giza_itf_idx_log_f(const float pixelvalue, const float vmin, const float vmax, const int cimin, const int cimax);
int _giza_itf_idx_sqrt_f(const float pixelvalue, const float vmin, const float vmax, const int cimin, const int cimax);

/* We keep an array of three image transfer functions, use set_image_transfer_function to set
0: linear
1: log
2: sqrt
*/
giza_itf_type giza_itf[3] = {_giza_itf_linear, _giza_itf_log, _giza_itf_sqrt};
giza_itf_type_f giza_itf_f[3] = {_giza_itf_linear_f, _giza_itf_log_f, _giza_itf_sqrt_f};
giza_itf_idx_type giza_itf_idx[3] = {_giza_itf_idx_linear, _giza_itf_idx_log, _giza_itf_idx_sqrt};
giza_itf_idx_type_f giza_itf_idx_f[3] = {_giza_itf_idx_linear_f, _giza_itf_idx_log_f, _giza_itf_idx_sqrt_f};


void giza_set_image_transfer_function(int itf) {
if (!_giza_check_device_ready ("giza_set_image_transfer_function"))
return;
if (itf < 0 || itf > 2)
{
_giza_warning ("giza_set_image_transfer_function",
"Invalid image transfer function, not set");
return;
}
Dev[id].itf = itf;
}

void giza_get_image_transfer_function(int* itfp) {
if (!_giza_check_device_ready ("giza_get_image_transfer_function"))
return;
*itfp = Dev[id].itf;
}


/*
* Lifted from PGPLOT grimg2.f on how to handle the image transfer function:
SFAC = 65000.0
SFACL = LOG(1.0+SFAC)
...
IF (MODE.EQ.0) THEN
IV = NINT((MININD*(A2-AV) + MAXIND*(AV-A1))/(A2-A1))
ELSE IF (MODE.EQ.1) THEN
IV = MININD + NINT((MAXIND-MININD)*
LOG(1.0+SFAC*ABS((AV-A1)/(A2-A1)))/SFACL)
ELSE IF (MODE.EQ.2) THEN
IV = MININD + NINT((MAXIND-MININD)*
SQRT(ABS((AV-A1)/(A2-A1))))
ELSE
IV = MININD
*/

/* The linear transfer functions */

/* returns position of pixelvalue as fraction of the distance between vmin, vmax */
double _giza_itf_linear(const double pixelvalue, const double vmin, const double vmax) {
return (vmax > vmin) ? (MIN(vmax, MAX(vmin, pixelvalue))-vmin)/(vmax-vmin) : (MIN(vmin, MAX(vmax, pixelvalue))-vmax)/(vmin-vmax);
}

/* maps pixelvalue's fractional position between vmin, vmax to corresponding index between cimin, cimax */
int _giza_itf_idx_linear(const double pixelvalue, const double vmin, const double vmax, const int cimin, const int cimax) {
const double fractional_pos = _giza_itf_linear(pixelvalue, vmin, vmax);
return MIN(cimin, cimax) + (int) round(((cimin < cimax) ? (cimax-cimin) : (cimin - cimax)) * fractional_pos);
}

/* id. for floats */
float _giza_itf_linear_f(const float pixelvalue, const float vmin, const float vmax) {
return (vmax > vmin) ? (MIN(vmax, MAX(vmin, pixelvalue))-vmin)/(vmax-vmin) : (MIN(vmin, MAX(vmax, pixelvalue))-vmax)/(vmin-vmax);
}

int _giza_itf_idx_linear_f(const float pixelvalue, const float vmin, const float vmax, const int cimin, const int cimax) {
const float fractional_pos = _giza_itf_linear_f(pixelvalue, vmin, vmax);
return MIN(cimin, cimax) + (int)roundf(((cimin < cimax) ? (cimax-cimin) : (cimin - cimax)) * fractional_pos);
}

/* The log transfer functions */
const double sfac = 65000.0;

/* returns position of pixelvalue as fraction of the distance between vmin, vmax */
double _giza_itf_log(const double pixelvalue, const double vmin, const double vmax) {
double sfacl = log(1.0+sfac);
return log(1.0+sfac*fabs(((vmax > vmin) ? (MIN(vmax, MAX(vmin, pixelvalue))-vmin) : (MIN(vmin, MAX(vmax, pixelvalue))-vmax))/(vmax-vmin)))/sfacl;
}

/* maps pixelvalue's fractional position between vmin, vmax to corresponding index between cimin, cimax */
int _giza_itf_idx_log(const double pixelvalue, const double vmin, const double vmax, const int cimin, const int cimax) {
const double fractional_pos = _giza_itf_log(pixelvalue, vmin, vmax);
return MIN(cimin, cimax) + (int)round(((cimax > cimin) ? (cimax-cimin) : (cimin-cimax)) * fractional_pos);
}

/* id. for floats */
const double sfac_f = 65000.0f;

float _giza_itf_log_f(const float pixelvalue, const float vmin, const float vmax) {
float sfacl_f = logf(1.0f+sfac);
return log(1.0f+sfac_f*fabsf(((vmax > vmin) ? (MIN(vmax, MAX(vmin, pixelvalue))-vmin) : (MIN(vmin, MAX(vmax, pixelvalue))-vmax))/(vmax-vmin)))/sfacl_f;
}

int _giza_itf_idx_log_f(const float pixelvalue, const float vmin, const float vmax, const int cimin, const int cimax) {
const float fractional_pos = _giza_itf_log_f(pixelvalue, vmin, vmax);
return MIN(cimin, cimax) + (int)roundf(((cimax > cimin) ? (cimax-cimin) : (cimin-cimax)) * fractional_pos);
}

/* and the sqrt versions */

/* returns position of pixelvalue as fraction of the distance between vmin, vmax */
double _giza_itf_sqrt(const double pixelvalue, const double vmin, const double vmax) {
return sqrt( _giza_itf_linear(pixelvalue, vmin, vmax) );
}

/* maps pixelvalue's fractional position between vmin, vmax to corresponding index between cimin, cimax */
int _giza_itf_idx_sqrt(const double pixelvalue, const double vmin, const double vmax, const int cimin, const int cimax) {
const double fractional_pos = _giza_itf_sqrt(pixelvalue, vmin, vmax);
return MIN(cimin, cimax) + (int)round(((cimax > cimin) ? (cimax-cimin) : (cimin-cimax)) * fractional_pos);
}

/* id. for floats */
float _giza_itf_sqrt_f(const float pixelvalue, const float vmin, const float vmax) {
return sqrtf( _giza_itf_linear_f(pixelvalue, vmin, vmax) );
}

int _giza_itf_idx_sqrt_f(const float pixelvalue, const float vmin, const float vmax, const int cimin, const int cimax) {
const float fractional_pos = _giza_itf_sqrt_f(pixelvalue, vmin, vmax);
return MIN(cimin, cimax) + (int)roundf(((cimax > cimin) ? (cimax-cimin) : (cimin-cimax)) * fractional_pos);
}
Loading

0 comments on commit 2aacdb9

Please sign in to comment.