@@ -7,7 +7,13 @@ const { describe, it } = require('node:test');
77const assert = require ( 'node:assert' ) ;
88
99const sharp = require ( '../../' ) ;
10- const { inputAvif, inputJpg, inputGifAnimated, inputPng } = require ( '../fixtures' ) ;
10+ const {
11+ inputAvif,
12+ inputAvifWithPitmBox,
13+ inputJpg,
14+ inputGifAnimated,
15+ inputPng,
16+ } = require ( '../fixtures' ) ;
1117
1218describe ( 'AVIF' , ( ) => {
1319 it ( 'called without options does not throw an error' , ( ) => {
@@ -17,16 +23,13 @@ describe('AVIF', () => {
1723 } ) ;
1824
1925 it ( 'can convert AVIF to JPEG' , async ( ) => {
20- const data = await sharp ( inputAvif )
21- . resize ( 32 )
22- . jpeg ( )
23- . toBuffer ( ) ;
26+ const data = await sharp ( inputAvif ) . resize ( 32 ) . jpeg ( ) . toBuffer ( ) ;
2427 const { size, ...metadata } = await sharp ( data ) . metadata ( ) ;
2528 void size ;
2629 assert . deepStrictEqual ( metadata , {
2730 autoOrient : {
2831 height : 13 ,
29- width : 32
32+ width : 32 ,
3033 } ,
3134 channels : 3 ,
3235 chromaSubsampling : '4:2:0' ,
@@ -41,7 +44,7 @@ describe('AVIF', () => {
4144 isProgressive : false ,
4245 isPalette : false ,
4346 space : 'srgb' ,
44- width : 32
47+ width : 32 ,
4548 } ) ;
4649 } ) ;
4750
@@ -55,7 +58,7 @@ describe('AVIF', () => {
5558 assert . deepStrictEqual ( metadata , {
5659 autoOrient : {
5760 height : 26 ,
58- width : 32
61+ width : 32 ,
5962 } ,
6063 channels : 3 ,
6164 compression : 'av1' ,
@@ -70,7 +73,7 @@ describe('AVIF', () => {
7073 pagePrimary : 0 ,
7174 pages : 1 ,
7275 space : 'srgb' ,
73- width : 32
76+ width : 32 ,
7477 } ) ;
7578 } ) ;
7679
@@ -84,7 +87,7 @@ describe('AVIF', () => {
8487 assert . deepStrictEqual ( metadata , {
8588 autoOrient : {
8689 height : 24 ,
87- width : 32
90+ width : 32 ,
8891 } ,
8992 channels : 3 ,
9093 compression : 'av1' ,
@@ -99,20 +102,18 @@ describe('AVIF', () => {
99102 pagePrimary : 0 ,
100103 pages : 1 ,
101104 space : 'srgb' ,
102- width : 32
105+ width : 32 ,
103106 } ) ;
104107 } ) ;
105108
106109 it ( 'can passthrough AVIF' , async ( ) => {
107- const data = await sharp ( inputAvif )
108- . resize ( 32 )
109- . toBuffer ( ) ;
110+ const data = await sharp ( inputAvif ) . resize ( 32 ) . toBuffer ( ) ;
110111 const { size, ...metadata } = await sharp ( data ) . metadata ( ) ;
111112 void size ;
112113 assert . deepStrictEqual ( metadata , {
113114 autoOrient : {
114115 height : 13 ,
115- width : 32
116+ width : 32 ,
116117 } ,
117118 channels : 3 ,
118119 compression : 'av1' ,
@@ -127,7 +128,7 @@ describe('AVIF', () => {
127128 pagePrimary : 0 ,
128129 pages : 1 ,
129130 space : 'srgb' ,
130- width : 32
131+ width : 32 ,
131132 } ) ;
132133 } ) ;
133134
@@ -141,7 +142,7 @@ describe('AVIF', () => {
141142 assert . deepStrictEqual ( metadata , {
142143 autoOrient : {
143144 height : 300 ,
144- width : 10
145+ width : 10 ,
145146 } ,
146147 channels : 4 ,
147148 compression : 'av1' ,
@@ -156,7 +157,7 @@ describe('AVIF', () => {
156157 pagePrimary : 0 ,
157158 pages : 1 ,
158159 space : 'srgb' ,
159- width : 10
160+ width : 10 ,
160161 } ) ;
161162 } ) ;
162163
@@ -171,7 +172,7 @@ describe('AVIF', () => {
171172 assert . deepStrictEqual ( metadata , {
172173 autoOrient : {
173174 height : 26 ,
174- width : 32
175+ width : 32 ,
175176 } ,
176177 channels : 3 ,
177178 compression : 'av1' ,
@@ -186,30 +187,37 @@ describe('AVIF', () => {
186187 pagePrimary : 0 ,
187188 pages : 1 ,
188189 space : 'srgb' ,
189- width : 32
190+ width : 32 ,
190191 } ) ;
191192 } ) ;
192193
193194 it ( 'Invalid width - too large' , async ( ) =>
194195 assert . rejects (
195- ( ) => sharp ( { create : { width : 16385 , height : 16 , channels : 3 , background : 'red' } } ) . avif ( ) . toBuffer ( ) ,
196- / P r o c e s s e d i m a g e i s t o o l a r g e f o r t h e H E I F f o r m a t /
197- )
198- ) ;
196+ ( ) =>
197+ sharp ( {
198+ create : { width : 16385 , height : 16 , channels : 3 , background : 'red' } ,
199+ } )
200+ . avif ( )
201+ . toBuffer ( ) ,
202+ / P r o c e s s e d i m a g e i s t o o l a r g e f o r t h e H E I F f o r m a t / ,
203+ ) ) ;
199204
200205 it ( 'Invalid height - too large' , async ( ) =>
201206 assert . rejects (
202- ( ) => sharp ( { create : { width : 16 , height : 16385 , channels : 3 , background : 'red' } } ) . avif ( ) . toBuffer ( ) ,
203- / P r o c e s s e d i m a g e i s t o o l a r g e f o r t h e H E I F f o r m a t /
204- )
205- ) ;
207+ ( ) =>
208+ sharp ( {
209+ create : { width : 16 , height : 16385 , channels : 3 , background : 'red' } ,
210+ } )
211+ . avif ( )
212+ . toBuffer ( ) ,
213+ / P r o c e s s e d i m a g e i s t o o l a r g e f o r t h e H E I F f o r m a t / ,
214+ ) ) ;
206215
207216 it ( 'Invalid bitdepth value throws error' , ( ) =>
208217 assert . throws (
209218 ( ) => sharp ( ) . avif ( { bitdepth : 11 } ) ,
210- / E x p e c t e d 8 , 1 0 o r 1 2 f o r b i t d e p t h b u t r e c e i v e d 1 1 o f t y p e n u m b e r /
211- )
212- ) ;
219+ / E x p e c t e d 8 , 1 0 o r 1 2 f o r b i t d e p t h b u t r e c e i v e d 1 1 o f t y p e n u m b e r / ,
220+ ) ) ;
213221
214222 it ( 'Different tune options result in different file sizes' , async ( ) => {
215223 const ssim = await sharp ( inputJpg )
@@ -221,5 +229,47 @@ describe('AVIF', () => {
221229 . avif ( { tune : 'iq' , effort : 0 } )
222230 . toBuffer ( ) ;
223231 assert ( ssim . length < iq . length ) ;
224- } )
232+ } ) ;
233+
234+ it ( 'AVIF with non-zero primary item uses it as default page' , async ( ) => {
235+ const { exif, ...metadata } = await sharp ( inputAvifWithPitmBox ) . metadata ( ) ;
236+ void exif ;
237+ assert . deepStrictEqual ( metadata , {
238+ format : 'heif' ,
239+ width : 4096 ,
240+ height : 800 ,
241+ space : 'srgb' ,
242+ channels : 3 ,
243+ depth : 'uchar' ,
244+ isProgressive : false ,
245+ isPalette : false ,
246+ bitsPerSample : 8 ,
247+ pages : 5 ,
248+ pagePrimary : 4 ,
249+ compression : 'av1' ,
250+ resolutionUnit : 'cm' ,
251+ hasProfile : false ,
252+ hasAlpha : false ,
253+ autoOrient : { width : 4096 , height : 800 } ,
254+ } ) ;
255+
256+ const data = await sharp ( inputAvifWithPitmBox )
257+ . png ( { compressionLevel : 0 } )
258+ . toBuffer ( ) ;
259+ const { size, ...pngMetadata } = await sharp ( data ) . metadata ( ) ;
260+ assert . deepStrictEqual ( pngMetadata , {
261+ format : 'png' ,
262+ width : 4096 ,
263+ height : 800 ,
264+ space : 'srgb' ,
265+ channels : 3 ,
266+ depth : 'uchar' ,
267+ isProgressive : false ,
268+ isPalette : false ,
269+ bitsPerSample : 8 ,
270+ hasProfile : false ,
271+ hasAlpha : false ,
272+ autoOrient : { width : 4096 , height : 800 } ,
273+ } ) ;
274+ } ) ;
225275} ) ;
0 commit comments