Skip to content

Commit

Permalink
Switched to unlicense.org, integrated GCC fixes supplied by Peter Nag…
Browse files Browse the repository at this point in the history
…y <[email protected]>, Anteru at anteru.net, and [email protected], added Codeblocks project (for testing with MinGW and GCC), added VS2008 project files, VS2008 static code analysis pass, added calls to delete at end of test.cpp
  • Loading branch information
richgel99 committed Jun 5, 2012
1 parent 65a1da6 commit 45ffad7
Show file tree
Hide file tree
Showing 5 changed files with 102 additions and 63 deletions.
Binary file modified bin/resampler.exe
Binary file not shown.
65 changes: 35 additions & 30 deletions resampler.cpp
Original file line number Diff line number Diff line change
@@ -1,10 +1,14 @@
// resampler.cpp, Separable filtering image rescaler v2.2, public domain, Rich Geldreich - [email protected]
// resampler.cpp, Separable filtering image rescaler v2.21, Rich Geldreich - [email protected]
// See unlicense at the bottom of resampler.h, or at http://unlicense.org/
//
// Feb. 1996: Creation, losely based on a heavily bugfixed version of Schumacher's resampler in Graphics Gems 3.
// Oct. 2000: Ported to C++, tweaks.
// May 2001: Continous to discrete mapping, box filter tweaks.
// March 9, 2002: Kaiser filter grabbed from Jonathan Blow's GD magazine mipmap sample code.
// Sept. 8, 2002: Comments cleaned up a bit.
// Dec. 31, 2008: Bit more cleanup, released as public domain.
// Dec. 31, 2008: v2.2: Bit more cleanup, released as public domain.
// June 4, 2012: v2.21: Switched to unlicense.org, integrated GCC fixes supplied by Peter Nagy <[email protected]>, Anteru at anteru.net, and [email protected],
// added Codeblocks project (for testing with MinGW and GCC), VS2008 static code analysis pass.
#include <stdlib.h>
#include <math.h>
#include <float.h>
Expand All @@ -14,7 +18,7 @@

#define resampler_assert assert

static inline int resampler_range_check(int v, int h) { resampler_assert((v >= 0) && (v < h)); return v; }
static inline int resampler_range_check(int v, int h) { (void)h; resampler_assert((v >= 0) && (v < h)); return v; }

#ifndef max
#define max(a,b) (((a) > (b)) ? (a) : (b))
Expand Down Expand Up @@ -58,15 +62,15 @@ static inline int posmod(int x, int y)
}
}

// To add your own filter, insert the new function below and update the filter table.
// There is no need to make the filter function particularly fast, because it's
// To add your own filter, insert the new function below and update the filter table.
// There is no need to make the filter function particularly fast, because it's
// only called during initializing to create the X and Y axis contributor tables.

#define BOX_FILTER_SUPPORT (0.5f)
static Resample_Real box_filter(Resample_Real t) /* pulse/Fourier window */
{
// make_clist() calls the filter function with t inverted (pos = left, neg = right)
if ((t >= -0.5f) && (t < 0.5f))
if ((t >= -0.5f) && (t < 0.5f))
return 1.0f;
else
return 0.0f;
Expand Down Expand Up @@ -124,18 +128,18 @@ static Resample_Real B_spline_filter(Resample_Real t) /* box (*) box (*) box (*
return (0.0f);
}

// Dodgson, N., "Quadratic Interpolation for Image Resampling"
// Dodgson, N., "Quadratic Interpolation for Image Resampling"
#define QUADRATIC_SUPPORT 1.5f
static Resample_Real quadratic(Resample_Real t, const Resample_Real R)
{
if (t < 0.0f)
t = -t;
t = -t;
if (t < QUADRATIC_SUPPORT)
{
Resample_Real tt = t * t;
if (t <= .5f)
return (-2.0f * R) * tt + .5f * (R + 1.0f);
else
else
return (R * tt) + (-2.0f * R - .5f) * t + (3.0f / 4.0f) * (R + 1.0f);
}
else
Expand All @@ -157,10 +161,10 @@ static Resample_Real quadratic_mix_filter(Resample_Real t)
return quadratic(t, .8f);
}

// Mitchell, D. and A. Netravali, "Reconstruction Filters in Computer Graphics."
// Mitchell, D. and A. Netravali, "Reconstruction Filters in Computer Graphics."
// Computer Graphics, Vol. 22, No. 4, pp. 221-228.
// (B, C)
// (1/3, 1/3) - Defaults recommended by Mitchell and Netravali
// (1/3, 1/3) - Defaults recommended by Mitchell and Netravali
// (1, 0) - Equivalent to the Cubic B-Spline
// (0, 0.5) - Equivalent to the Catmull-Rom Spline
// (0, C) - The family of Cardinal Cubic Splines
Expand Down Expand Up @@ -226,7 +230,7 @@ static Resample_Real clean(double t)
}

//static double blackman_window(double x)
//{
//{
// return .42f + .50f * cos(M_PI*x) + .08f * cos(2.0f*M_PI*x);
//}

Expand Down Expand Up @@ -308,7 +312,7 @@ static Resample_Real lanczos12_filter(Resample_Real t)
return (0.0f);
}

static double bessel0(double x)
static double bessel0(double x)
{
const double EPSILON_RATIO = 1E-16;
double xh, sum, pow, ds;
Expand All @@ -331,7 +335,7 @@ static double bessel0(double x)
}

static const Resample_Real KAISER_ALPHA = 4.0;
static double kaiser(double alpha, double half_width, double x)
static double kaiser(double alpha, double half_width, double x)
{
const double ratio = (x / half_width);
return bessel0(alpha * sqrt(1 - ratio * ratio)) / bessel0(alpha);
Expand All @@ -346,14 +350,14 @@ static Resample_Real kaiser_filter(Resample_Real t)
if (t < KAISER_SUPPORT)
{
// db atten
const Resample_Real att = 40.0f;
const Resample_Real att = 40.0f;
const Resample_Real alpha = (Resample_Real)(exp(log((double)0.58417 * (att - 20.96)) * 0.4) + 0.07886 * (att - 20.96));
//const Resample_Real alpha = KAISER_ALPHA;
return (Resample_Real)clean(sinc(t) * kaiser(alpha, KAISER_SUPPORT, t));
}

return 0.0f;
}
}

// filters[] is a list of all the available filter functions.
static struct
Expand Down Expand Up @@ -424,7 +428,7 @@ int Resampler::reflect(const int j, const int src_x, const Boundary_Op boundary_
return n;
}

// The make_clist() method generates, for all destination samples,
// The make_clist() method generates, for all destination samples,
// the list of all source samples with non-zero weighted contributions.
Resampler::Contrib_List* Resampler::make_clist(
int src_x, int dst_x, Boundary_Op boundary_op,
Expand Down Expand Up @@ -465,7 +469,7 @@ Resampler::Contrib_List* Resampler::make_clist(

if (xscale < 1.0f)
{
int total;
int total; (void)total;

/* Handle case when there are fewer destination
* samples than source samples (downsampling/minification).
Expand Down Expand Up @@ -554,7 +558,7 @@ Resampler::Contrib_List* Resampler::make_clist(

k = Pcontrib[i].n++;

Pcontrib[i].p[k].pixel = unsigned short(n); /* store src sample number */
Pcontrib[i].p[k].pixel = (unsigned short)(n); /* store src sample number */
Pcontrib[i].p[k].weight = weight; /* store src sample weight */

total_weight += weight; /* total weight of all contributors */
Expand All @@ -572,13 +576,13 @@ Resampler::Contrib_List* Resampler::make_clist(

//resampler_assert(Pcontrib[i].n);
//resampler_assert(max_k != -1);
if ((max_k == -1) || (Pcontrib[i].n == 0))
if ((max_k == -1) || (Pcontrib[i].n == 0))
{
free(Pcpool);
free(Pcontrib);
free(Pcontrib_bounds);
return NULL;
}
}

if (total_weight != 1.0f)
Pcontrib[i].p[max_k].weight += 1.0f - total_weight;
Expand Down Expand Up @@ -662,7 +666,7 @@ Resampler::Contrib_List* Resampler::make_clist(

n = reflect(j, src_x, boundary_op);

#if RESAMPLER_DEBUG
#if RESAMPLER_DEBUG
printf("%i(%f), ", n, weight);
#endif

Expand All @@ -673,7 +677,7 @@ Resampler::Contrib_List* Resampler::make_clist(

k = Pcontrib[i].n++;

Pcontrib[i].p[k].pixel = unsigned short(n); /* store src sample number */
Pcontrib[i].p[k].pixel = (unsigned short)(n); /* store src sample number */
Pcontrib[i].p[k].weight = weight; /* store src sample weight */

total_weight += weight; /* total weight of all contributors */
Expand All @@ -692,13 +696,13 @@ Resampler::Contrib_List* Resampler::make_clist(
//resampler_assert(Pcontrib[i].n);
//resampler_assert(max_k != -1);

if ((max_k == -1) || (Pcontrib[i].n == 0))
if ((max_k == -1) || (Pcontrib[i].n == 0))
{
free(Pcpool);
free(Pcontrib);
free(Pcontrib_bounds);
return NULL;
}
}

if (total_weight != 1.0f)
Pcontrib[i].p[max_k].weight += 1.0f - total_weight;
Expand Down Expand Up @@ -764,7 +768,8 @@ void Resampler::clamp(Sample* Pdst, int n)
{
while (n > 0)
{
*Pdst++ = clamp_sample(*Pdst);
*Pdst = clamp_sample(*Pdst);
++Pdst;
n--;
}
}
Expand Down Expand Up @@ -931,12 +936,12 @@ Resampler::~Resampler()
printf("actual ops: %i\n", total_ops);
#endif

free(m_Pdst_buf);
free(m_Pdst_buf);
m_Pdst_buf = NULL;

if (m_Ptmp_buf)
{
free(m_Ptmp_buf);
free(m_Ptmp_buf);
m_Ptmp_buf = NULL;
}

Expand Down Expand Up @@ -979,7 +984,7 @@ void Resampler::restart()
if (STATUS_OKAY != m_status)
return;

m_cur_src_y = m_cur_dst_y = 0;
m_cur_src_y = m_cur_dst_y = 0;

int i, j;
for (i = 0; i < m_resample_src_y; i++)
Expand Down Expand Up @@ -1010,7 +1015,7 @@ Resampler::Resampler(int src_x, int src_y,
const char* Pfilter_name,
Contrib_List* Pclist_x,
Contrib_List* Pclist_y,
Resample_Real filter_x_scale,
Resample_Real filter_x_scale,
Resample_Real filter_y_scale,
Resample_Real src_x_ofs,
Resample_Real src_y_ofs)
Expand Down
30 changes: 28 additions & 2 deletions resampler.h
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
// resampler.h, Separable filtering image rescaler v2.2, public domain, Rich Geldreich - [email protected]
// resampler.h, Separable filtering image rescaler v2.21, Rich Geldreich - [email protected]
// See unlicense.org text at the bottom of this file.
#ifndef __RESAMPLER_H__
#define __RESAMPLER_H__

Expand Down Expand Up @@ -167,4 +168,29 @@ class Resampler
}
};

#endif __RESAMPLER_H__
#endif // __RESAMPLER_H__

// This is free and unencumbered software released into the public domain.
//
// Anyone is free to copy, modify, publish, use, compile, sell, or
// distribute this software, either in source code form or as a compiled
// binary, for any purpose, commercial or non-commercial, and by any
// means.
//
// In jurisdictions that recognize copyright laws, the author or authors
// of this software dedicate any and all copyright interest in the
// software to the public domain. We make this dedication for the benefit
// of the public at large and to the detriment of our heirs and
// successors. We intend this dedication to be an overt act of
// relinquishment in perpetuity of all present and future rights to this
// software under copyright law.
//
// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
// EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
// MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.
// IN NO EVENT SHALL THE AUTHORS BE LIABLE FOR ANY CLAIM, DAMAGES OR
// OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE,
// ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR
// OTHER DEALINGS IN THE SOFTWARE.
//
// For more information, please refer to <http://unlicense.org/>
Loading

0 comments on commit 45ffad7

Please sign in to comment.