stbirtest.c
This file
Path
stb_image_resize_test/stbirtest.c
992 lines · 29.3 KiB · C
Last changed by
Merge branch 'master' into workingSean Barrettbefore adoption6e9f34d
History of this fileTake it
1#include <stdio.h>2#include <stdlib.h>3#include <string.h>45//#define HEAVYTM6#include "tm.h"78#ifdef RADUSETM39tm_api * g_tm_api;10//#define PROFILE_MODE11#endif1213#include <math.h>1415#ifdef _MSC_VER16#define stop() __debugbreak()17#include <windows.h>18#define int64 __int6419#define uint64 unsigned __int6420#else21#define stop() __builtin_trap()22#define int64 long long23#define uint64 unsigned long long24#endif2526#ifdef _MSC_VER27#pragma warning(disable:4127)28#endif2930//#define NOCOMP313233//#define PROFILE_NEW_ONLY34//#define PROFILE_MODE353637#if defined(_x86_64) || defined( __x86_64__ ) || defined( _M_X64 ) || defined(__x86_64) || defined(__SSE2__) || defined(STBIR_SSE) || defined( _M_IX86_FP ) || defined(__i386) || defined( __i386__ ) || defined( _M_IX86 ) || defined( _X86_ )3839#ifdef _MSC_VER4041 uint64 __rdtsc();42 #define __cycles() __rdtsc()4344#else // non msvc4546 static inline uint64 __cycles() 47 {48 unsigned int lo, hi;49 asm volatile ("rdtsc" : "=a" (lo), "=d" (hi) );50 return ( ( (uint64) hi ) << 32 ) | ( (uint64) lo );51 }5253#endif // msvc5455#elif defined( _M_ARM64 ) || defined( __aarch64__ ) || defined( __arm64__ ) || defined(__ARM_NEON__) 5657#ifdef _MSC_VER5859 #define __cycles() _ReadStatusReg(ARM64_CNTVCT)6061#else6263 static inline uint64 __cycles()64 {65 uint64 tsc;66 asm volatile("mrs %0, cntvct_el0" : "=r" (tsc));67 return tsc;68 }6970#endif7172#else // x64, arm7374#error Unknown platform for timing.7576#endif //x64 and 777879#ifdef PROFILE_MODE8081#define STBIR_ASSERT(cond)8283#endif8485#ifdef _DEBUG86#undef STBIR_ASSERT87#define STBIR_ASSERT(cond) { if (!(cond)) stop(); }88#endif899091#define SHRINKBYW 292#define ZOOMBYW 293#define SHRINKBYH 294#define ZOOMBYH 2959697int mem_count = 0;9899#ifdef TEST_WITH_VALLOC100101#define STBIR__SEPARATE_ALLOCATIONS102103#if TEST_WITH_LIMIT_AT_FRONT104105 void * wmalloc(SIZE_T size)106 {107 static unsigned int pagesize=0;108 void* p;109 SIZE_T s;110111 // get the page size, if we haven't yet112 if (pagesize==0)113 {114 SYSTEM_INFO si;115 GetSystemInfo(&si);116 pagesize=si.dwPageSize;117 }118119 // we need room for the size, 8 bytes to hide the original pointer and a120 // validation dword, and enough data to completely fill one page121 s=(size+(pagesize-1))&~(pagesize-1);122123 // allocate the size plus a page (for the guard)124 p=VirtualAlloc(0,(SIZE_T)s,MEM_RESERVE|MEM_COMMIT,PAGE_READWRITE);125 126 return p;127 }128129 void wfree(void * ptr)130 {131 if (ptr)132 {133 if ( ((ptrdiff_t)ptr) & 4095 ) stop();134 if ( VirtualFree(ptr,0,MEM_RELEASE) == 0 ) stop();135 }136 }137138#else139140 void * wmalloc(SIZE_T size)141 {142 static unsigned int pagesize=0;143 void* p;144 SIZE_T s;145146 // get the page size, if we haven't yet147 if (pagesize==0)148 {149 SYSTEM_INFO si;150 GetSystemInfo(&si);151 pagesize=si.dwPageSize;152 }153154 // we need room for the size, 8 bytes to hide the original pointer and a155 // validation dword, and enough data to completely fill one page156 s=(size+16+(pagesize-1))&~(pagesize-1);157158 // allocate the size plus a page (for the guard)159 p=VirtualAlloc(0,(SIZE_T)(s+pagesize+pagesize),MEM_RESERVE|MEM_COMMIT,PAGE_READWRITE);160161 if (p)162 {163 DWORD oldprot;164 void* orig=p;165166 // protect the first page167 VirtualProtect(((char*)p),pagesize,PAGE_NOACCESS,&oldprot);168169 // protect the final page170 VirtualProtect(((char*)p)+s+pagesize,pagesize,PAGE_NOACCESS,&oldprot);171172 // now move the returned pointer so that it bumps right up against the173 // the next (protected) page (this may result in unaligned return174 // addresses - pre-align the sizes if you always want aligned ptrs)175//#define ERROR_ON_FRONT176#ifdef ERROR_ON_FRONT177 p=((char*)p)+pagesize+16;178#else179 p=((char*)p)+(s-size)+pagesize;180#endif181182 // hide the validation value and the original pointer (which we'll183 // need used for freeing) right behind the returned pointer184 ((unsigned int*)p)[-1]=0x98765432;185 ((void**)p)[-2]=orig;186 ++mem_count;187//printf("aloc: %p bytes: %d\n",p,(int)size);188 return(p);189 }190191 return 0;192 }193194 void wfree(void * ptr)195 {196 if (ptr)197 {198 int err=0;199200 // is this one of our allocations?201 if (((((unsigned int*)ptr)[-1])!=0x98765432) || ((((void**)ptr)[-2])==0))202 {203 err=1;204 }205206 if (err)207 {208 __debugbreak();209 }210 else211 {212213 // back up to find the original pointer214 void* p=((void**)ptr)[-2];215216 // clear the validation value and the original pointer217 ((unsigned int*)ptr)[-1]=0;218 ((void**)ptr)[-2]=0;219220//printf("free: %p\n",ptr);221222 --mem_count;223224 // now free the pages225 if (p)226 VirtualFree(p,0,MEM_RELEASE);227228 }229 }230 }231232#endif233234#define STBIR_MALLOC(size,user_data) ((void)(user_data), wmalloc(size))235#define STBIR_FREE(ptr,user_data) ((void)(user_data), wfree(ptr))236237#endif238239#define STBIR_PROFILE240//#define STBIR_NO_SIMD241//#define STBIR_AVX242//#define STBIR_AVX2243#define STB_IMAGE_RESIZE_IMPLEMENTATION244#include "stb_image_resize2.h" // new one!245246#define STB_IMAGE_WRITE_IMPLEMENTATION247#include "stb_image_write.h"248249int tsizes[5] = { 1, 1, 2, 4, 2 };250int ttypes[5] = { STBIR_TYPE_UINT8, STBIR_TYPE_UINT8_SRGB, STBIR_TYPE_UINT16, STBIR_TYPE_FLOAT, STBIR_TYPE_HALF_FLOAT };251252int cedges[4] = { STBIR_EDGE_CLAMP, STBIR_EDGE_REFLECT, STBIR_EDGE_ZERO, STBIR_EDGE_WRAP };253int flts[5] = { STBIR_FILTER_BOX, STBIR_FILTER_TRIANGLE, STBIR_FILTER_CUBICBSPLINE, STBIR_FILTER_CATMULLROM, STBIR_FILTER_MITCHELL };254int buffers[20] = { STBIR_1CHANNEL, STBIR_2CHANNEL, STBIR_RGB, STBIR_4CHANNEL, 255 STBIR_BGRA, STBIR_ARGB, STBIR_RA, STBIR_AR,256 STBIR_RGBA_PM, STBIR_ARGB_PM, STBIR_RA_PM, STBIR_AR_PM,257 STBIR_RGBA, STBIR_ARGB, STBIR_RA, STBIR_AR,258 STBIR_RGBA_PM, STBIR_ARGB_PM, STBIR_RA_PM, STBIR_AR_PM,259 };260int obuffers[20] = { STBIR_1CHANNEL, STBIR_2CHANNEL, STBIR_RGB, STBIR_4CHANNEL, 261 STBIR_BGRA, STBIR_ARGB, STBIR_RA, STBIR_AR,262 STBIR_RGBA_PM, STBIR_ARGB_PM, STBIR_RA_PM, STBIR_AR_PM,263 STBIR_RGBA_PM, STBIR_ARGB_PM, STBIR_RA_PM, STBIR_AR_PM,264 STBIR_RGBA, STBIR_ARGB, STBIR_RA, STBIR_AR,265 };266267int bchannels[20] = { 1, 2, 3, 4, 4,4, 2,2, 4,4, 2,2, 4,4, 2,2, 4,4, 2,2 }; 268int alphapos[20] = { -1, -1, -1, -1, 3,0, 1,0, 3,0, 1,0, 3,0, 1,0,3,0, 1,0 }; 269270271char const * buffstrs[20] = { "1ch", "2ch", "3ch", "4ch", "RGBA", "ARGB", "RA", "AR", "RGBA_both_pre", "ARGB_both_pre", "RA_both_pre", "AR_both_pre", "RGBA_out_pre", "ARGB_out_pre", "RA_out_pre", "AR_out_pre", "RGBA_in_pre", "ARGB_in_pre", "RA_in_pre", "AR_in_pre" };272char const * typestrs[5] = { "Bytes", "BytesSRGB", "Shorts", "Floats", "Half Floats"};273char const * edgestrs[4] = { "Clamp", "Reflect", "Zero", "Wrap" };274char const * fltstrs[5] = { "Box", "Triangle", "Cubic", "Catmullrom", "Mitchell" };275276#ifdef STBIR_PROFILE277 static void do_acc_zones( STBIR_PROFILE_INFO * profile )278 {279 stbir_uint32 j;280 stbir_uint64 start = tmGetAccumulationStart( tm_mask ); start=start;281282 for( j = 0 ; j < profile->count ; j++ )283 {284 if ( profile->clocks[j] )285 tmEmitAccumulationZone( 0, 0, (tm_uint64*)&start, 0, profile->clocks[j], profile->descriptions[j] );286 }287 }288#else289 #define do_acc_zones(...)290#endif291292int64 vert;293294//#define WINTHREADTEST295#ifdef WINTHREADTEST296297static STBIR_RESIZE * thread_resize;298static LONG which;299static int threads_started = 0;300static HANDLE threads[32];301static HANDLE starts,stops;302 303static DWORD resize_shim( LPVOID p )304{305 for(;;)306 {307 LONG wh;308309 WaitForSingleObject( starts, INFINITE );310311 wh = InterlockedAdd( &which, 1 ) - 1;312313 ENTER( "Split %d", wh );314 stbir_resize_split( thread_resize, wh, 1 );315 #ifdef STBIR_PROFILE316 { STBIR_PROFILE_INFO profile; stbir_resize_split_profile_info( &profile, thread_resize, wh, 1 ); do_acc_zones( &profile ); vert = profile.clocks[1]; }317 #endif318 LEAVE();319320 ReleaseSemaphore( stops, 1, 0 );321 }322}323324#endif325326void nresize( void * o, int ox, int oy, int op, void * i, int ix, int iy, int ip, int buf, int type, int edg, int flt )327{328 STBIR_RESIZE resize;329 330 stbir_resize_init( &resize, i, ix, iy, ip, o, ox, oy, op, buffers[buf], ttypes[type] );331 stbir_set_pixel_layouts( &resize, buffers[buf], obuffers[buf] );332 stbir_set_edgemodes( &resize, cedges[edg], cedges[edg] );333 stbir_set_filters( &resize, flts[flt], /*STBIR_FILTER_POINT_SAMPLE */ flts[flt] );334 //stbir_set_input_subrect( &resize, 0.55f,0.333f,0.75f,0.50f);335 //stbir_set_output_pixel_subrect( &resize, 00, 00, ox/2,oy/2);336 //stbir_set_pixel_subrect(&resize, 1430,1361,30,30);337 338 ENTER( "Resize" );339 340 #ifndef WINTHREADTEST341342 ENTER( "Filters" );343 stbir_build_samplers_with_splits( &resize, 1 );344 #ifdef STBIR_PROFILE345 { STBIR_PROFILE_INFO profile; stbir_resize_build_profile_info( &profile, &resize ); do_acc_zones( &profile ); }346 #endif347 LEAVE();348 349 ENTER( "Resize" );350 if(!stbir_resize_extended( &resize ) )351 stop();352 #ifdef STBIR_PROFILE353 { STBIR_PROFILE_INFO profile; stbir_resize_extended_profile_info( &profile, &resize ); do_acc_zones( &profile ); vert = profile.clocks[1]; }354 #endif355 LEAVE();356357 #else358 {359 int c, cnt;360 361 ENTER( "Filters" );362 cnt = stbir_build_samplers_with_splits( &resize, 4 );363 #ifdef STBIR_PROFILE364 { STBIR_PROFILE_INFO profile; stbir_resize_build_profile_info( &profile, &resize ); do_acc_zones( &profile ); }365 #endif366 LEAVE();367 368 ENTER( "Thread start" );369 if ( threads_started == 0 )370 {371 starts = CreateSemaphore( 0, 0, 32, 0 );372 stops = CreateSemaphore( 0, 0, 32, 0 );373 }374 for( c = threads_started ; c < cnt ; c++ )375 threads[ c ] = CreateThread( 0, 2048*1024, resize_shim, 0, 0, 0 );376 377 threads_started = cnt;378 thread_resize = &resize;379 which = 0;380 LEAVE();381382 // starts the threads383 ReleaseSemaphore( starts, cnt, 0 );384385 ENTER( "Wait" );386 for( c = 0 ; c < cnt; c++ )387 WaitForSingleObject( stops, INFINITE );388 LEAVE();389 }390 #endif391392 ENTER( "Free" );393 stbir_free_samplers( &resize );394 LEAVE();395 LEAVE();396}397398399#define STB_IMAGE_IMPLEMENTATION400#include "stb_image.h"401402extern void oresize( void * o, int ox, int oy, int op, void * i, int ix, int iy, int ip, int buf, int type, int edg, int flt );403404405406#define TYPESTART 0407#define TYPEEND 4408409#define LAYOUTSTART 0410#define LAYOUTEND 19411412#define SIZEWSTART 0413#define SIZEWEND 2414415#define SIZEHSTART 0416#define SIZEHEND 2417418#define EDGESTART 0419#define EDGEEND 3420421#define FILTERSTART 0422#define FILTEREND 4423424#define HEIGHTSTART 0425#define HEIGHTEND 2426427#define WIDTHSTART 0428#define WIDTHEND 2429430431432433static void * convert8to16( unsigned char * i, int w, int h, int c )434{435 unsigned short * ret;436 int p;437438 ret = malloc( w*h*c*sizeof(short) );439 for(p = 0 ; p < (w*h*c) ; p++ )440 {441 ret[p]=(short)((((int)i[p])<<8)+i[p]);442 }443444 return ret;445}446447static void * convert8tof( unsigned char * i, int w, int h, int c )448{449 float * ret;450 int p;451452 ret = malloc( w*h*c*sizeof(float) );453 for(p = 0 ; p < (w*h*c) ; p++ )454 {455 ret[p]=((float)i[p])*(1.0f/255.0f);456 }457458 return ret;459}460461static void * convert8tohf( unsigned char * i, int w, int h, int c )462{463 stbir__FP16 * ret;464 int p;465466 ret = malloc( w*h*c*sizeof(stbir__FP16) );467 for(p = 0 ; p < (w*h*c) ; p++ )468 {469 ret[p]=stbir__float_to_half(((float)i[p])*(1.0f/255.0f));470 }471472 return ret;473}474475static void * convert8tohff( unsigned char * i, int w, int h, int c )476{477 float * ret;478 int p;479480 ret = malloc( w*h*c*sizeof(float) );481 for(p = 0 ; p < (w*h*c) ; p++ )482 {483 ret[p]=stbir__half_to_float(stbir__float_to_half(((float)i[p])*(1.0f/255.0f)));484 }485486 return ret;487}488489static int isprime( int v )490{491 int i;492493 if ( v <= 3 )494 return ( v > 1 );495 if ( ( v & 1 ) == 0 )496 return 0;497 if ( ( v % 3 ) == 0 )498 return 0;499 i = 5;500 while ( (i*i) <= v )501 {502 if ( ( v % i ) == 0 )503 return 0;504 if ( ( v % ( i + 2 ) ) == 0 )505 return 0;506 i += 6;507 }508509 return 1;510}511512static int getprime( int v )513{514 int i;515 i = 0;516 for(;;)517 {518 if ( i >= v )519 return v; // can't find any, just return orig520 if (isprime(v - i))521 return v - i;522 if (isprime(v + i))523 return v + i;524 ++i;525 }526}527528529int main( int argc, char ** argv )530{531 int ix, iy, ic;532 unsigned char * input[6];533 char * ir1;534 char * ir2;535 int szhs[3];536 int szws[3];537 int aw, ah, ac;538 unsigned char * correctalpha;539 int layouts, types, heights, widths, edges, filters;540541 if ( argc != 2 )542 {543 printf("command: stbirtest [imagefile]\n");544 exit(1);545 }546547 SetupTM( "127.0.0.1" );548549 correctalpha = stbi_load( "correctalpha.png", &aw, &ah, &ac, 0 );550551 input[0] = stbi_load( argv[1], &ix, &iy, &ic, 0 );552 input[1] = input[0];553 input[2] = convert8to16( input[0], ix, iy, ic );554 input[3] = convert8tof( input[0], ix, iy, ic );555 input[4] = convert8tohf( input[0], ix, iy, ic );556 input[5] = convert8tohff( input[0], ix, iy, ic );557558 printf("Input %dx%d (%d channels)\n",ix,iy,ic);559560 ir1 = malloc( 4 * 4 * 3000 * 3000ULL );561 ir2 = malloc( 4 * 4 * 3000 * 3000ULL );562563 szhs[0] = getprime( iy/SHRINKBYH );564 szhs[1] = iy;565 szhs[2] = getprime( iy*ZOOMBYH );566567 szws[0] = getprime( ix/SHRINKBYW );568 szws[1] = ix;569 szws[2] = getprime( ix*ZOOMBYW );570571 #if 1572 for( types = TYPESTART ; types <= TYPEEND ; types++ )573 #else574 for( types = 1 ; types <= 1 ; types++ )575 #endif576 {577 ENTER( "Test type: %s",typestrs[types]);578 #if 1579 for( layouts = LAYOUTSTART ; layouts <= LAYOUTEND ; layouts++ )580 #else581 for( layouts = 16; layouts <= 16 ; layouts++ )582 #endif583 {584 ENTER( "Test layout: %s",buffstrs[layouts]);585 586 #if 0587 for( heights = HEIGHTSTART ; heights <= HEIGHTEND ; heights++ )588 {589 int w, h = szhs[heights];590 #else591 for( heights = 0 ; heights <= 11 ; heights++ )592 {593 static int szhsz[12]={32, 200, 350, 400, 450, 509, 532, 624, 700, 824, 1023, 2053 };594 int w, h = szhsz[heights];595 #endif596597 ENTER( "Test height: %d %s %d",iy,(h<iy)?"Down":((h>iy)?"Up":"Same"),h);598 599 #if 0600 for( widths = WIDTHSTART ; widths <= WIDTHEND ; widths++ )601 {602 w = szws[widths];603 #else604 for( widths = 0 ; widths <= 12 ; widths++ )605 {606 static int szwsz[13]={2, 32, 200, 350, 400, 450, 509, 532, 624, 700, 824, 1023, 2053 };607 w = szwsz[widths];608 #endif609610 ENTER( "Test width: %d %s %d",ix, (w<ix)?"Down":((w>ix)?"Up":"Same"), w);611612 #if 0613 for( edges = EDGESTART ; edges <= EDGEEND ; edges++ )614 #else615 for( edges = 0 ; edges <= 0 ; edges++ )616 #endif617 {618 ENTER( "Test edge: %s",edgestrs[edges]);619 #if 0620 for( filters = FILTERSTART ; filters <= FILTEREND ; filters++ )621 #else622 for( filters = 3 ; filters <= 3 ; filters++ )623 #endif624 {625 int op, opw, np,npw, c, a;626 #ifdef COMPARE_SAME 627 int oldtypes = types;628 #else629 int oldtypes = (types==4)?3:types;630 #endif631 632 ENTER( "Test filter: %s",fltstrs[filters]);633 {634 c = bchannels[layouts];635 a = alphapos[layouts];636637 op = w*tsizes[oldtypes]*c + 60;638 opw = w*tsizes[oldtypes]*c;639640 np = w*tsizes[types]*c + 60;641 npw = w*tsizes[types]*c;642643 printf( "%s:layout: %s w: %d h: %d edge: %s filt: %s\n", typestrs[types],buffstrs[layouts], w, h, edgestrs[edges], fltstrs[filters] );644645646 // clear pixel area to different, right edge to zero647 #ifndef NOCLEAR648 ENTER( "Test clear padding" );649 {650 int d;651 for( d = 0 ; d < h ; d++ )652 {653 int oofs = d * op;654 int nofs = d * np;655 memset( ir1 + oofs, 192, opw );656 memset( ir1 + oofs+opw, 79, op-opw );657 memset( ir2 + nofs, 255, npw );658 memset( ir2 + nofs+npw, 79, np-npw );659 }660 }661 LEAVE();662663 #endif664665 #ifdef COMPARE_SAME 666 #define TIMINGS 1667 #else 668 #define TIMINGS 1669 #endif670 ENTER( "Test both" );671 {672 #ifndef PROFILE_NEW_ONLY673 {674 int ttt, max = 0x7fffffff;675 ENTER( "Test old" );676 for( ttt = 0 ; ttt < TIMINGS ; ttt++ )677 {678 int64 m = __cycles();679680 oresize( ir1, w, h, op, 681 #ifdef COMPARE_SAME 682 input[types], 683 #else684 input[(types==4)?5:types], 685 #endif686 ix, iy, ix*ic*tsizes[oldtypes], layouts, oldtypes, edges, filters );687688 m = __cycles() - m;689 if ( ( (int)m ) < max )690 max = (int) m;691 }692 LEAVE();693 printf("old: %d\n", max );694 }695 #endif696 697 {698 int ttt, max = 0x7fffffff, maxv = 0x7fffffff;699 ENTER( "Test new" );700 for( ttt = 0 ; ttt < TIMINGS ; ttt++ )701 {702 int64 m = __cycles();703704 nresize( ir2, w, h, np, input[types], ix, iy, ix*ic*tsizes[types], layouts, types, edges, filters );705 706 m = __cycles() - m;707 if ( ( (int)m ) < max )708 max = (int) m;709 if ( ( (int)vert ) < maxv )710 maxv = (int) vert;711 }712 LEAVE(); // test new713 printf("new: %d (v: %d)\n", max, maxv );714 }715 }716 LEAVE(); // test both717718 if ( mem_count!= 0 )719 stop();720721 #ifndef NOCOMP722 ENTER( "Test compare" );723 {724 int x,y,ch;725 int nums = 0;726 for( y = 0 ; y < h ; y++ )727 {728 for( x = 0 ; x < w ; x++ )729 {730 switch(types)731 {732 case 0:733 case 1: //SRGB 734 {735 unsigned char * p1 = (unsigned char *)&ir1[y*op+x*c];736 unsigned char * p2 = (unsigned char *)&ir2[y*np+x*c];737 for( ch = 0 ; ch < c ; ch++ )738 {739 float pp1,pp2,d;740 float av = (a==-1)?1.0f:((float)p1[a]/255.0f);741742 pp1 = p1[ch];743 pp2 = p2[ch];744745 // compare in premult space746 #ifndef COMPARE_SAME 747 if ( ( ( layouts >=4 ) && ( layouts <= 7 ) ) || ( ( layouts >=16 ) && ( layouts <= 19 ) ) )748 {749 pp1 *= av;750 pp2 *= av;751 }752 #endif753754 d = pp1 - pp2;755 if ( d < 0 ) d = -d;756757 #ifdef COMPARE_SAME 758 if ( d > 0 ) 759 #else 760 if ( d > 1 )761 #endif762 {763 printf("Error at %d x %d (chan %d) (d: %g a: %g) [%d %d %d %d] [%d %d %d %d]\n",x,y,ch, d,av, p1[0],p1[1],p1[2],p1[3], p2[0],p2[1],p2[2],p2[3]);764 ++nums;765 if ( nums > 16 ) goto ex;766 //if (d) exit(1);767 //goto ex;768 }769 } 770 }771 break;772773 case 2:774 {775 unsigned short * p1 = (unsigned short *)&ir1[y*op+x*c*sizeof(short)];776 unsigned short * p2 = (unsigned short *)&ir2[y*np+x*c*sizeof(short)];777 for( ch = 0 ; ch < c ; ch++ )778 {779 float thres,pp1,pp2,d;780 float av = (a==-1)?1.0f:((float)p1[a]/65535.0f);781782 pp1 = p1[ch];783 pp2 = p2[ch];784785 // compare in premult space786 #ifndef COMPARE_SAME 787 if ( ( ( layouts >=4 ) && ( layouts <= 7 ) ) || ( ( layouts >= 16 ) && ( layouts <= 19 ) ) )788 {789 pp1 *= av;790 pp2 *= av;791 }792 #endif793794 d = pp1 - pp2;795 if ( d < 0 ) d = -d;796797 thres=((float)p1[ch]*0.007f)+2.0f;798 if (thres<4) thres = 4;799 800 #ifdef COMPARE_SAME 801 if ( d > 0 ) 802 #else 803 if ( d > thres)804 #endif805 {806 printf("Error at %d x %d (chan %d) %d %d [df: %g th: %g al: %g] (%d %d %d %d) (%d %d %d %d)\n",x,y,ch, p1[ch],p2[ch],d,thres,av,p1[0],p1[1],p1[2],p1[3],p2[0],p2[1],p2[2],p2[3]);807 ++nums;808 if ( nums > 16 ) goto ex;809 //if (d) exit(1);810 //goto ex;811 }812 }813 }814 break;815816 case 3:817 {818 float * p1 = (float *)&ir1[y*op+x*c*sizeof(float)];819 float * p2 = (float *)&ir2[y*np+x*c*sizeof(float)];820 for( ch = 0 ; ch < c ; ch++ )821 {822 float pp1 = p1[ch], pp2 = p2[ch];823 float av = (a==-1)?1.0f:p1[a];824 float thres, d;825826 // clamp827 if (pp1<=0.0f) pp1 = 0;828 if (pp2<=0.0f) pp2 = 0;829 if (av<=0.0f) av = 0;830 if (pp1>1.0f) pp1 = 1.0f;831 if (pp2>1.0f) pp2 = 1.0f;832 if (av>1.0f) av = 1.0f;833834 // compare in premult space835 #ifndef COMPARE_SAME 836 if ( ( ( layouts >=4 ) && ( layouts <= 7 ) ) || ( ( layouts >= 16 ) && ( layouts <= 19 ) ) )837 {838 pp1 *= av;839 pp2 *= av;840 }841 #endif842843 d = pp1 - pp2;844 if ( d < 0 ) d = -d;845846 thres=(p1[ch]*0.002f)+0.0002f;847 if ( thres < 0 ) thres = -thres;848849 #ifdef COMPARE_SAME 850 if ( d != 0.0f ) 851 #else 852 if ( d > thres )853 #endif854 {855 printf("Error at %d x %d (chan %d) %g %g [df: %g th: %g al: %g] (%g %g %g %g) (%g %g %g %g)\n",x,y,ch, p1[ch],p2[ch],d,thres,av,p1[0],p1[1],p1[2],p1[3],p2[0],p2[1],p2[2],p2[3]);856 ++nums;857 if ( nums > 16 ) goto ex;858 //if (d) exit(1);859 //goto ex;860 }861 }862 }863 break;864865 case 4:866 {867 #ifdef COMPARE_SAME 868 stbir__FP16 * p1 = (stbir__FP16 *)&ir1[y*op+x*c*sizeof(stbir__FP16)];869 #else870 float * p1 = (float *)&ir1[y*op+x*c*sizeof(float)];871 #endif872 stbir__FP16 * p2 = (stbir__FP16 *)&ir2[y*np+x*c*sizeof(stbir__FP16)];873 for( ch = 0 ; ch < c ; ch++ )874 {875 #ifdef COMPARE_SAME 876 float pp1 = stbir__half_to_float(p1[ch]);877 float av = (a==-1)?1.0f:stbir__half_to_float(p1[a]);878 #else879 float pp1 = stbir__half_to_float(stbir__float_to_half(p1[ch]));880 float av = (a==-1)?1.0f:stbir__half_to_float(stbir__float_to_half(p1[a]));881 #endif882 float pp2 = stbir__half_to_float(p2[ch]);883 float d, thres;884885 // clamp886 if (pp1<=0.0f) pp1 = 0;887 if (pp2<=0.0f) pp2 = 0;888 if (av<=0.0f) av = 0;889 if (pp1>1.0f) pp1 = 1.0f;890 if (pp2>1.0f) pp2 = 1.0f;891 if (av>1.0f) av = 1.0f;892893 thres=(pp1*0.002f)+0.0002f;894895 // compare in premult space896 #ifndef COMPARE_SAME 897 if ( ( ( layouts >=4 ) && ( layouts <= 7 ) ) || ( ( layouts >= 16 ) && ( layouts <= 19 ) ) )898 {899 pp1 *= av;900 pp2 *= av;901 }902 #endif903904 d = pp1 - pp2;905 if ( d < 0 ) d = -d;906907908 #ifdef COMPARE_SAME 909 if ( d != 0.0f ) 910 #else 911 if ( d > thres )912 #endif913 {914 printf("Error at %d x %d (chan %d) %g %g [df: %g th: %g al: %g] (%g %g %g %g) (%g %g %g %g)\n",x,y,ch, 915 #ifdef COMPARE_SAME 916 stbir__half_to_float(p1[ch]), 917 #else918 p1[ch],919 #endif920 stbir__half_to_float(p2[ch]), 921 d,thres,av,922 #ifdef COMPARE_SAME 923 stbir__half_to_float(p1[0]),stbir__half_to_float(p1[1]),stbir__half_to_float(p1[2]),stbir__half_to_float(p1[3]),924 #else925 p1[0],p1[1],p1[2],p1[3],926 #endif927 stbir__half_to_float(p2[0]),stbir__half_to_float(p2[1]),stbir__half_to_float(p2[2]),stbir__half_to_float(p2[3]) );928 ++nums;929 if ( nums > 16 ) goto ex;930 //if (d) exit(1);931 //goto ex;932 }933 }934 }935 break;936 }937 }938939 for( x = (w*c)*tsizes[oldtypes]; x < op; x++ )940 {941 if ( ir1[y*op+x] != 79 )942 {943 printf("Margin error at %d x %d %d (should be 79) OLD!\n",x,y,(unsigned char)ir1[y*op+x]);944 goto ex;945 }946 }947948 for( x = (w*c)*tsizes[types]; x < np; x++ )949 {950 if ( ir2[y*np+x] != 79 )951 {952 printf("Margin error at %d x %d %d (should be 79) NEW\n",x,y,(unsigned char)ir2[y*np+x]);953 goto ex;954 }955 }956 }957 958 ex:959 ENTER( "OUTPUT IMAGES" );960 printf(" tot pix: %d, errs: %d\n", w*h*c,nums );961962 if (nums)963 {964 stbi_write_png("old.png", w, h, c, ir1, op);965 stbi_write_png("new.png", w, h, c, ir2, np);966 exit(1);967 }968969 LEAVE(); // output images970 }971 LEAVE(); //test compare972 #endif973974975976 }977 LEAVE(); // test filter978 }979 LEAVE(); // test edge980 }981 LEAVE(); // test width982 }983 LEAVE(); // test height984 }985 LEAVE(); // test type986 }987 LEAVE(); // test layout988 }989990 CloseTM();991 return 0;992}