@@ -180,14 +180,17 @@ function normalizeBlendMode(value, parsingArray = false) {
180180 return "source-over" ;
181181}
182182
183- function addLocallyCachedImageOps ( opList , data ) {
184- if ( data . objId ) {
185- opList . addDependency ( data . objId ) ;
183+ function addCachedImageOps (
184+ opList ,
185+ { objId, fn, args, optionalContent, hasMask }
186+ ) {
187+ if ( objId ) {
188+ opList . addDependency ( objId ) ;
186189 }
187- opList . addImageOps ( data . fn , data . args , data . optionalContent , data . hasMask ) ;
190+ opList . addImageOps ( fn , args , optionalContent , hasMask ) ;
188191
189- if ( data . fn === OPS . paintImageMaskXObject && data . args [ 0 ] ?. count > 0 ) {
190- data . args [ 0 ] . count ++ ;
192+ if ( fn === OPS . paintImageMaskXObject && args [ 0 ] ?. count > 0 ) {
193+ args [ 0 ] . count ++ ;
191194 }
192195}
193196
@@ -742,7 +745,8 @@ class PartialEvaluator {
742745 // If there is no imageMask, create the PDFImage and a lot
743746 // of image processing can be done here.
744747 let objId = `img_${ this . idFactory . createObjId ( ) } ` ,
745- cacheGlobally = false ;
748+ cacheGlobally = false ,
749+ globalCacheData = null ;
746750
747751 if ( this . parsingType3Font ) {
748752 objId = `${ this . idFactory . getDocId ( ) } _type3_${ objId } ` ;
@@ -767,15 +771,17 @@ class PartialEvaluator {
767771 operatorList . addImageOps ( fn , args , optionalContent , hasMask ) ;
768772
769773 if ( cacheGlobally ) {
774+ globalCacheData = {
775+ objId,
776+ fn,
777+ args,
778+ optionalContent,
779+ hasMask,
780+ byteSize : 0 , // Temporary entry, to avoid `setData` returning early.
781+ } ;
782+
770783 if ( this . globalImageCache . hasDecodeFailed ( imageRef ) ) {
771- this . globalImageCache . setData ( imageRef , {
772- objId,
773- fn,
774- args,
775- optionalContent,
776- hasMask,
777- byteSize : 0 , // Data is `null`, since decoding failed previously.
778- } ) ;
784+ this . globalImageCache . setData ( imageRef , globalCacheData ) ;
779785
780786 this . _sendImgData ( objId , /* imgData = */ null , cacheGlobally ) ;
781787 return ;
@@ -792,14 +798,7 @@ class PartialEvaluator {
792798 ] ) ;
793799
794800 if ( localLength ) {
795- this . globalImageCache . setData ( imageRef , {
796- objId,
797- fn,
798- args,
799- optionalContent,
800- hasMask,
801- byteSize : 0 , // Temporary entry, to avoid `setData` returning early.
802- } ) ;
801+ this . globalImageCache . setData ( imageRef , globalCacheData ) ;
803802 this . globalImageCache . addByteSize ( imageRef , localLength ) ;
804803 return ;
805804 }
@@ -848,14 +847,8 @@ class PartialEvaluator {
848847 this . _regionalImageCache . set ( /* name = */ null , imageRef , cacheData ) ;
849848
850849 if ( cacheGlobally ) {
851- this . globalImageCache . setData ( imageRef , {
852- objId,
853- fn,
854- args,
855- optionalContent,
856- hasMask,
857- byteSize : 0 , // Temporary entry, note `addByteSize` above.
858- } ) ;
850+ assert ( globalCacheData , "The global cache-data must be available." ) ;
851+ this . globalImageCache . setData ( imageRef , globalCacheData ) ;
859852 }
860853 }
861854 }
@@ -1779,7 +1772,7 @@ class PartialEvaluator {
17791772 if ( isValidName ) {
17801773 const localImage = localImageCache . getByName ( name ) ;
17811774 if ( localImage ) {
1782- addLocallyCachedImageOps ( operatorList , localImage ) ;
1775+ addCachedImageOps ( operatorList , localImage ) ;
17831776 args = null ;
17841777 continue ;
17851778 }
@@ -1793,28 +1786,12 @@ class PartialEvaluator {
17931786
17941787 let xobj = xobjs . getRaw ( name ) ;
17951788 if ( xobj instanceof Ref ) {
1796- const localImage =
1789+ const cachedImage =
17971790 localImageCache . getByRef ( xobj ) ||
1798- self . _regionalImageCache . getByRef ( xobj ) ;
1799- if ( localImage ) {
1800- addLocallyCachedImageOps ( operatorList , localImage ) ;
1801- resolveXObject ( ) ;
1802- return ;
1803- }
1804-
1805- const globalImage = self . globalImageCache . getData (
1806- xobj ,
1807- self . pageIndex
1808- ) ;
1809- if ( globalImage ) {
1810- operatorList . addDependency ( globalImage . objId ) ;
1811- operatorList . addImageOps (
1812- globalImage . fn ,
1813- globalImage . args ,
1814- globalImage . optionalContent ,
1815- globalImage . hasMask
1816- ) ;
1817-
1791+ self . _regionalImageCache . getByRef ( xobj ) ||
1792+ self . globalImageCache . getData ( xobj , self . pageIndex ) ;
1793+ if ( cachedImage ) {
1794+ addCachedImageOps ( operatorList , cachedImage ) ;
18181795 resolveXObject ( ) ;
18191796 return ;
18201797 }
@@ -1907,7 +1884,7 @@ class PartialEvaluator {
19071884 if ( cacheKey ) {
19081885 const localImage = localImageCache . getByName ( cacheKey ) ;
19091886 if ( localImage ) {
1910- addLocallyCachedImageOps ( operatorList , localImage ) ;
1887+ addCachedImageOps ( operatorList , localImage ) ;
19111888 args = null ;
19121889 continue ;
19131890 }
0 commit comments