Browse Source

Merge pull request #7 from nothings/resample

Resample
Jorge Rodriguez 11 years ago
parent
commit
a0d4f79074
5 changed files with 1750 additions and 1507 deletions
  1. 1682 1476
      stb_image_resize.h
  2. 48 17
      tests/resample_test.cpp
  3. 3 2
      tests/resize.dsp
  4. 13 12
      tools/README.list
  5. 4 0
      tools/make_readme.dsp

File diff suppressed because it is too large
+ 1682 - 1476
stb_image_resize.h


+ 48 - 17
tests/resample_test.cpp

@@ -141,6 +141,34 @@ static void resizer(int argc, char **argv)
 	exit(0);
 }
 
+static void performance(int argc, char **argv)
+{
+	unsigned char* input_pixels;
+	unsigned char* output_pixels;
+	int w, h, count;
+	int n, i;
+	int out_w, out_h, srgb=1;
+	input_pixels = stbi_load(argv[1], &w, &h, &n, 0);
+    #if 1
+    out_w = w/4; out_h h/4; count=100; // 1
+    #elif 0
+	out_w = w*2; out_h = h/4; count=20; // 2   // note this is structured pessimily, would be much faster to downsample vertically first
+    #elif 0
+    out_w = w/4; out_h = h*2; count=50; // 3
+    #elif 0
+    out_w = w*3; out_h = h*3; count=5; srgb=0; // 4
+    #else
+    out_w = w*3; out_h = h*3; count=3; // 5   // this is dominated by linear->sRGB conversion
+    #endif
+
+	output_pixels = (unsigned char*) malloc(out_w*out_h*n);
+    for (i=0; i < count; ++i)
+        if (srgb)
+	        stbir_resize_uint8_srgb(input_pixels, w, h, 0, output_pixels, out_w, out_h, 0, n, -1,0);
+        else
+	        stbir_resize_uint8(input_pixels, w, h, 0, output_pixels, out_w, out_h, 0, n);
+	exit(0);
+}
 
 void test_suite(int argc, char **argv);
 
@@ -153,6 +181,7 @@ int main(int argc, char** argv)
 	int out_w, out_h, out_stride;
 
 	//resizer(argc, argv);
+    //performance(argc, argv);
 
 #if 1
 	test_suite(argc, argv);
@@ -619,10 +648,10 @@ void verify_box(void)
 			      + image88[j*2+0][i*2+1]
 				  + image88[j*2+1][i*2+0]
 				  + image88[j*2+1][i*2+1];
-			STBIR_ASSERT(output44[j][i] == ((n+2)>>2));
+			STBIR_ASSERT(output44[j][i] == ((n+2)>>2) || output44[j][i] == ((n+1)>>2)); // can't guarantee exact rounding due to numerical precision
 			t += n;
 		}
-	STBIR_ASSERT(output11[0][0] == ((t+32)>>6));
+	STBIR_ASSERT(output11[0][0] == ((t+32)>>6) || output11[0][0] == ((t+31)>>6)); // can't guarantee exact rounding due to numerical precision
 }
 
 void verify_filter_normalized(stbir_filter filter, int output_size, unsigned int value)
@@ -646,6 +675,8 @@ void test_filters(void)
 {
 	int i,j;
 
+	mtsrand(0);
+
 	for (i=0; i < sizeof(image88); ++i)
 		image88[0][i] = mtrand() & 255;
 	verify_box();
@@ -673,25 +704,25 @@ void test_filters(void)
 
 	verify_filter_normalized(STBIR_FILTER_BOX, 8, value);
 	verify_filter_normalized(STBIR_FILTER_TRIANGLE, 8, value);
-	verify_filter_normalized(STBIR_FILTER_CUBIC, 8, value);
+	verify_filter_normalized(STBIR_FILTER_CUBICBSPLINE, 8, value);
 	verify_filter_normalized(STBIR_FILTER_CATMULLROM, 8, value);
 	verify_filter_normalized(STBIR_FILTER_MITCHELL, 8, value);
 
 	verify_filter_normalized(STBIR_FILTER_BOX, 4, value);
 	verify_filter_normalized(STBIR_FILTER_TRIANGLE, 4, value);
-	verify_filter_normalized(STBIR_FILTER_CUBIC, 4, value);
+	verify_filter_normalized(STBIR_FILTER_CUBICBSPLINE, 4, value);
 	verify_filter_normalized(STBIR_FILTER_CATMULLROM, 4, value);
 	verify_filter_normalized(STBIR_FILTER_MITCHELL, 4, value);
 
 	verify_filter_normalized(STBIR_FILTER_BOX, 2, value);
 	verify_filter_normalized(STBIR_FILTER_TRIANGLE, 2, value);
-	verify_filter_normalized(STBIR_FILTER_CUBIC, 2, value);
+	verify_filter_normalized(STBIR_FILTER_CUBICBSPLINE, 2, value);
 	verify_filter_normalized(STBIR_FILTER_CATMULLROM, 2, value);
 	verify_filter_normalized(STBIR_FILTER_MITCHELL, 2, value);
 
 	verify_filter_normalized(STBIR_FILTER_BOX, 1, value);
 	verify_filter_normalized(STBIR_FILTER_TRIANGLE, 1, value);
-	verify_filter_normalized(STBIR_FILTER_CUBIC, 1, value);
+	verify_filter_normalized(STBIR_FILTER_CUBICBSPLINE, 1, value);
 	verify_filter_normalized(STBIR_FILTER_CATMULLROM, 1, value);
 	verify_filter_normalized(STBIR_FILTER_MITCHELL, 1, value);
 
@@ -915,17 +946,17 @@ void test_suite(int argc, char **argv)
 	test_channels(barbara, 2, 2, 4);
 
 	// filter tests
-	resize_image(barbara, 2, 2, STBIR_FILTER_BOX       , STBIR_EDGE_CLAMP, STBIR_COLORSPACE_SRGB, "test-output/barbara-upsample-nearest.png");
-	resize_image(barbara, 2, 2, STBIR_FILTER_TRIANGLE  , STBIR_EDGE_CLAMP, STBIR_COLORSPACE_SRGB, "test-output/barbara-upsample-bilinear.png");
-	resize_image(barbara, 2, 2, STBIR_FILTER_CUBIC     , STBIR_EDGE_CLAMP, STBIR_COLORSPACE_SRGB, "test-output/barbara-upsample-bicubic.png");
-	resize_image(barbara, 2, 2, STBIR_FILTER_CATMULLROM, STBIR_EDGE_CLAMP, STBIR_COLORSPACE_SRGB, "test-output/barbara-upsample-catmullrom.png");
-	resize_image(barbara, 2, 2, STBIR_FILTER_MITCHELL  , STBIR_EDGE_CLAMP, STBIR_COLORSPACE_SRGB, "test-output/barbara-upsample-mitchell.png");
-
-	resize_image(barbara, 0.5f, 0.5f, STBIR_FILTER_BOX       , STBIR_EDGE_CLAMP, STBIR_COLORSPACE_SRGB, "test-output/barbara-downsample-nearest.png");
-	resize_image(barbara, 0.5f, 0.5f, STBIR_FILTER_TRIANGLE  , STBIR_EDGE_CLAMP, STBIR_COLORSPACE_SRGB, "test-output/barbara-downsample-bilinear.png");
-	resize_image(barbara, 0.5f, 0.5f, STBIR_FILTER_CUBIC     , STBIR_EDGE_CLAMP, STBIR_COLORSPACE_SRGB, "test-output/barbara-downsample-bicubic.png");
-	resize_image(barbara, 0.5f, 0.5f, STBIR_FILTER_CATMULLROM, STBIR_EDGE_CLAMP, STBIR_COLORSPACE_SRGB, "test-output/barbara-downsample-catmullrom.png");
-	resize_image(barbara, 0.5f, 0.5f, STBIR_FILTER_MITCHELL  , STBIR_EDGE_CLAMP, STBIR_COLORSPACE_SRGB, "test-output/barbara-downsample-mitchell.png");
+	resize_image(barbara, 2, 2, STBIR_FILTER_BOX         , STBIR_EDGE_CLAMP, STBIR_COLORSPACE_SRGB, "test-output/barbara-upsample-nearest.png");
+	resize_image(barbara, 2, 2, STBIR_FILTER_TRIANGLE    , STBIR_EDGE_CLAMP, STBIR_COLORSPACE_SRGB, "test-output/barbara-upsample-bilinear.png");
+	resize_image(barbara, 2, 2, STBIR_FILTER_CUBICBSPLINE, STBIR_EDGE_CLAMP, STBIR_COLORSPACE_SRGB, "test-output/barbara-upsample-bicubic.png");
+	resize_image(barbara, 2, 2, STBIR_FILTER_CATMULLROM  , STBIR_EDGE_CLAMP, STBIR_COLORSPACE_SRGB, "test-output/barbara-upsample-catmullrom.png");
+	resize_image(barbara, 2, 2, STBIR_FILTER_MITCHELL    , STBIR_EDGE_CLAMP, STBIR_COLORSPACE_SRGB, "test-output/barbara-upsample-mitchell.png");
+
+	resize_image(barbara, 0.5f, 0.5f, STBIR_FILTER_BOX         , STBIR_EDGE_CLAMP, STBIR_COLORSPACE_SRGB, "test-output/barbara-downsample-nearest.png");
+	resize_image(barbara, 0.5f, 0.5f, STBIR_FILTER_TRIANGLE    , STBIR_EDGE_CLAMP, STBIR_COLORSPACE_SRGB, "test-output/barbara-downsample-bilinear.png");
+	resize_image(barbara, 0.5f, 0.5f, STBIR_FILTER_CUBICBSPLINE, STBIR_EDGE_CLAMP, STBIR_COLORSPACE_SRGB, "test-output/barbara-downsample-bicubic.png");
+	resize_image(barbara, 0.5f, 0.5f, STBIR_FILTER_CATMULLROM  , STBIR_EDGE_CLAMP, STBIR_COLORSPACE_SRGB, "test-output/barbara-downsample-catmullrom.png");
+	resize_image(barbara, 0.5f, 0.5f, STBIR_FILTER_MITCHELL    , STBIR_EDGE_CLAMP, STBIR_COLORSPACE_SRGB, "test-output/barbara-downsample-mitchell.png");
 
 	for (i = 10; i < 100; i++)
 	{

+ 3 - 2
tests/resize.dsp

@@ -39,9 +39,10 @@ RSC=rc.exe
 # PROP Use_Debug_Libraries 0
 # PROP Output_Dir "Release"
 # PROP Intermediate_Dir "Release"
+# PROP Ignore_Export_Lib 0
 # PROP Target_Dir ""
 # ADD BASE CPP /nologo /W3 /GX /O2 /D "WIN32" /D "NDEBUG" /D "_CONSOLE" /D "_MBCS" /YX /FD /c
-# ADD CPP /nologo /W3 /GX /O2 /D "WIN32" /D "NDEBUG" /D "_CONSOLE" /D "_MBCS" /YX /FD /c
+# ADD CPP /nologo /G6 /W3 /GX /Z7 /O2 /I ".." /D "WIN32" /D "NDEBUG" /D "_CONSOLE" /D "_MBCS" /YX /FD /c
 # ADD BASE RSC /l 0x409 /d "NDEBUG"
 # ADD RSC /l 0x409 /d "NDEBUG"
 BSC32=bscmake.exe
@@ -49,7 +50,7 @@ BSC32=bscmake.exe
 # ADD BSC32 /nologo
 LINK32=link.exe
 # ADD BASE LINK32 kernel32.lib user32.lib gdi32.lib winspool.lib comdlg32.lib advapi32.lib shell32.lib ole32.lib oleaut32.lib uuid.lib odbc32.lib odbccp32.lib kernel32.lib user32.lib gdi32.lib winspool.lib comdlg32.lib advapi32.lib shell32.lib ole32.lib oleaut32.lib uuid.lib odbc32.lib odbccp32.lib /nologo /subsystem:console /machine:I386
-# ADD LINK32 kernel32.lib user32.lib gdi32.lib winspool.lib comdlg32.lib advapi32.lib shell32.lib ole32.lib oleaut32.lib uuid.lib odbc32.lib odbccp32.lib kernel32.lib user32.lib gdi32.lib winspool.lib comdlg32.lib advapi32.lib shell32.lib ole32.lib oleaut32.lib uuid.lib odbc32.lib odbccp32.lib /nologo /subsystem:console /machine:I386
+# ADD LINK32 kernel32.lib user32.lib gdi32.lib winspool.lib comdlg32.lib advapi32.lib shell32.lib ole32.lib oleaut32.lib uuid.lib odbc32.lib odbccp32.lib kernel32.lib user32.lib gdi32.lib winspool.lib comdlg32.lib advapi32.lib shell32.lib ole32.lib oleaut32.lib uuid.lib odbc32.lib odbccp32.lib /nologo /subsystem:console /debug /machine:I386
 
 !ELSEIF  "$(CFG)" == "resize - Win32 Debug"
 

+ 13 - 12
tools/README.list

@@ -1,12 +1,13 @@
-stb_vorbis.c      | audio    | decode ogg vorbis files from file/memory to float/16-bit signed output
-stb_image.h       | graphics | image loading/decoding from file/memory: JPG, PNG, TGA, BMP, PSD, GIF, HDR, PIC
-stb_truetype.h    | graphics | parse, decode, and rasterize characters from truetype fonts
-stb_image_write.h | graphics | image writing to disk: PNG, TGA, BMP
-stretchy_buffer.h | utility  | typesafe dynamic array for C (i.e. approximation to vector<>), doesn't compile as C++
-stb_textedit.h    | UI       | guts of a text editor for games etc implementing them from scratch
-stb_dxt.h         | 3D graphics | Fabian "ryg" Giesen's real-time DXT compressor
-stb_herringbone_wang_tile.h | games | herringbone Wang tile map generator
-stb_perlin.h      | 3D graphics | revised Perlin noise (3D input, 1D output)
-stb_c_lexer.h     | parsing  | simplify writing parsers for C-like languages
-stb_divide.h      | math     | more useful 32-bit modulus e.g. "euclidean divide"
-stb.h             | misc     | helper functions for C, mostly redundant in C++; basically author's personal stuff
+stb_vorbis.c                | audio       | decode ogg vorbis files from file/memory to float/16-bit signed output
+stb_image.h                 | graphics    | image loading/decoding from file/memory: JPG, PNG, TGA, BMP, PSD, GIF, HDR, PIC
+stb_truetype.h              | graphics    | parse, decode, and rasterize characters from truetype fonts
+stb_image_write.h           | graphics    | image writing to disk: PNG, TGA, BMP
+stb_image_resize.h          | graphics    | resize images larger/smaller with good quality
+stretchy_buffer.h           | utility     | typesafe dynamic array for C (i.e. approximation to vector<>), doesn't compile as C++
+stb_textedit.h              | UI          | guts of a text editor for games etc implementing them from scratch
+stb_dxt.h                   | 3D graphics | Fabian "ryg" Giesen's real-time DXT compressor
+stb_perlin.h                | 3D graphics | revised Perlin noise (3D input, 1D output)
+stb_herringbone_wang_tile.h | games       | herringbone Wang tile map generator
+stb_c_lexer.h               | parsing     | simplify writing parsers for C-like languages
+stb_divide.h                | math        | more useful 32-bit modulus e.g. "euclidean divide"
+stb.h                       | misc        | helper functions for C, mostly redundant in C++; basically author's personal stuff

+ 4 - 0
tools/make_readme.dsp

@@ -84,5 +84,9 @@ LINK32=link.exe
 
 SOURCE=.\make_readme.c
 # End Source File
+# Begin Source File
+
+SOURCE=.\README.list
+# End Source File
 # End Target
 # End Project

Some files were not shown because too many files changed in this diff