1+ import { setupOpenCv } from "./cv" ;
2+
3+ beforeAll ( setupOpenCv ) ;
4+
5+ describe ( "applyColorMap" , ( ) => {
6+ it ( "should apply COLORMAP_JET to a grayscale image" , async ( ) => {
7+ // Create a simple grayscale image
8+ const src = new cv . Mat ( 100 , 100 , cv . CV_8UC1 ) ;
9+
10+ // Fill with gradient values
11+ for ( let i = 0 ; i < 100 ; i ++ ) {
12+ for ( let j = 0 ; j < 100 ; j ++ ) {
13+ src . ucharPtr ( i , j ) [ 0 ] = Math . floor ( ( i + j ) * 255 / 200 ) ;
14+ }
15+ }
16+
17+ const dst = new cv . Mat ( ) ;
18+
19+ // Apply JET colormap
20+ cv . applyColorMap ( src , dst , cv . COLORMAP_JET ) ;
21+
22+ // Verify the output is a 3-channel color image
23+ expect ( dst . channels ( ) ) . toBe ( 3 ) ;
24+ expect ( dst . rows ) . toBe ( 100 ) ;
25+ expect ( dst . cols ) . toBe ( 100 ) ;
26+ expect ( dst . type ( ) ) . toBe ( cv . CV_8UC3 ) ;
27+
28+ // Clean up
29+ src . delete ( ) ;
30+ dst . delete ( ) ;
31+ } ) ;
32+
33+ it ( "should have all COLORMAP constants available" , ( ) => {
34+ // Test that all colormap constants are defined
35+ expect ( typeof cv . COLORMAP_JET ) . toBe ( 'number' ) ;
36+ expect ( typeof cv . COLORMAP_AUTUMN ) . toBe ( 'number' ) ;
37+ expect ( typeof cv . COLORMAP_BONE ) . toBe ( 'number' ) ;
38+ expect ( typeof cv . COLORMAP_WINTER ) . toBe ( 'number' ) ;
39+ expect ( typeof cv . COLORMAP_RAINBOW ) . toBe ( 'number' ) ;
40+ expect ( typeof cv . COLORMAP_OCEAN ) . toBe ( 'number' ) ;
41+ expect ( typeof cv . COLORMAP_SUMMER ) . toBe ( 'number' ) ;
42+ expect ( typeof cv . COLORMAP_SPRING ) . toBe ( 'number' ) ;
43+ expect ( typeof cv . COLORMAP_COOL ) . toBe ( 'number' ) ;
44+ expect ( typeof cv . COLORMAP_HSV ) . toBe ( 'number' ) ;
45+ expect ( typeof cv . COLORMAP_PINK ) . toBe ( 'number' ) ;
46+ expect ( typeof cv . COLORMAP_HOT ) . toBe ( 'number' ) ;
47+ expect ( typeof cv . COLORMAP_PARULA ) . toBe ( 'number' ) ;
48+ expect ( typeof cv . COLORMAP_MAGMA ) . toBe ( 'number' ) ;
49+ expect ( typeof cv . COLORMAP_INFERNO ) . toBe ( 'number' ) ;
50+ expect ( typeof cv . COLORMAP_PLASMA ) . toBe ( 'number' ) ;
51+ expect ( typeof cv . COLORMAP_VIRIDIS ) . toBe ( 'number' ) ;
52+ expect ( typeof cv . COLORMAP_CIVIDIS ) . toBe ( 'number' ) ;
53+ expect ( typeof cv . COLORMAP_TWILIGHT ) . toBe ( 'number' ) ;
54+ expect ( typeof cv . COLORMAP_TWILIGHT_SHIFTED ) . toBe ( 'number' ) ;
55+ expect ( typeof cv . COLORMAP_TURBO ) . toBe ( 'number' ) ;
56+ expect ( typeof cv . COLORMAP_DEEPGREEN ) . toBe ( 'number' ) ;
57+ } ) ;
58+
59+ it ( "should apply different colormaps correctly" , async ( ) => {
60+ // Create a simple grayscale image
61+ const src = new cv . Mat ( 50 , 50 , cv . CV_8UC1 , new cv . Scalar ( 128 ) ) ;
62+ const dst1 = new cv . Mat ( ) ;
63+ const dst2 = new cv . Mat ( ) ;
64+
65+ // Apply different colormaps
66+ cv . applyColorMap ( src , dst1 , cv . COLORMAP_JET ) ;
67+ cv . applyColorMap ( src , dst2 , cv . COLORMAP_VIRIDIS ) ;
68+
69+ // Both should be 3-channel color images
70+ expect ( dst1 . channels ( ) ) . toBe ( 3 ) ;
71+ expect ( dst2 . channels ( ) ) . toBe ( 3 ) ;
72+
73+ // Different colormaps should produce different results
74+ const data1 = dst1 . data ;
75+ const data2 = dst2 . data ;
76+ let different = false ;
77+ for ( let i = 0 ; i < Math . min ( data1 . length , data2 . length ) ; i ++ ) {
78+ if ( data1 [ i ] !== data2 [ i ] ) {
79+ different = true ;
80+ break ;
81+ }
82+ }
83+ expect ( different ) . toBe ( true ) ;
84+
85+ // Clean up
86+ src . delete ( ) ;
87+ dst1 . delete ( ) ;
88+ dst2 . delete ( ) ;
89+ } ) ;
90+ } ) ;
0 commit comments