@@ -21,20 +21,25 @@ export const calcStore = (set, get) => ({
21
21
set ( ( ) => ( { totalTokens : null , totalCost : null } ) ) ;
22
22
} ,
23
23
runCalculation : ( ) => {
24
- function getResizedImageSize ( minSize , height , width ) {
24
+ function getResizedImageSize ( maxDimension , minSide , height , width ) {
25
25
let resizedHeight = height ;
26
26
let resizedWidth = width ;
27
27
28
- if ( height > width ) {
29
- resizedHeight = minSize ;
30
- } else {
31
- resizedHeight = ( height / width ) * minSize ;
28
+ // Only scale down if larger than maxDimension on any side
29
+ if ( width > maxDimension || height > maxDimension ) {
30
+ const scaleFactor = Math . min (
31
+ maxDimension / width ,
32
+ maxDimension / height
33
+ ) ;
34
+ resizedWidth = width * scaleFactor ;
35
+ resizedHeight = height * scaleFactor ;
32
36
}
33
37
34
- if ( height > width ) {
35
- resizedWidth = ( width / height ) * minSize ;
36
- } else {
37
- resizedWidth = minSize ;
38
+ // If the shortest side is greater than minSide, scale to minSide
39
+ if ( Math . min ( resizedWidth , resizedHeight ) > minSide ) {
40
+ const scaleFactor = minSide / Math . min ( resizedWidth , resizedHeight ) ;
41
+ resizedWidth = resizedWidth * scaleFactor ;
42
+ resizedHeight = resizedHeight * scaleFactor ;
38
43
}
39
44
40
45
return { height : resizedHeight , width : resizedWidth } ;
@@ -48,13 +53,15 @@ export const calcStore = (set, get) => ({
48
53
49
54
const { model, images } = get ( ) ;
50
55
const tokensPerTile = model . tokensPerTile ;
56
+ const maxImageDimension = model . maxImageDimension ;
51
57
const imageMinSizeLength = model . imageMinSizeLength ;
52
58
const tileSizeLength = model . tileSizeLength ;
53
59
const additionalBuffer = model . additionalBuffer ;
54
60
const costPerThousandTokens = model . costPerThousandTokens ;
55
61
56
62
const imageTileCount = images . flatMap ( ( image ) => {
57
63
const imgSize = getResizedImageSize (
64
+ maxImageDimension ,
58
65
imageMinSizeLength ,
59
66
image . height ,
60
67
image . width
0 commit comments