PageSourceSearch

https://binomial.biz/ktx2_studio/encode_dto_apply.js

js binomial.biz collected 2026-09-25 22:09:06 UTC 5,863 bytes, 129 lines download raw bytes

1// encode_dto_apply.js
2//
3// applyEncodeDTO(encoder, dto): applies an encode DTO (built by buildEncodeDTO in
4// index.html) to a BasisEncoder instance, in the SAME ORDER as the legacy inline
5// encode path; the caller then calls encoder.encode().
6//
7// PURE: depends only on (encoder, dto) -- no Module, no DOM. That's why it can run
8// unchanged on the main thread (non-worker DTO path) AND inside the encode worker
9// (which importScripts() this file). All enum values in dto are already ints.
10//
11// Loaded on the page via <script src> and in the worker via importScripts(), so it
12// is the single source of truth for both modes (no duplication / drift).
13
14function applyEncodeDTO(encoder, dto)
15{
16   // container / threading
17   encoder.controlThreading(dto.multithreaded, dto.numWorkerThreads);
18   encoder.setCreateKTX2File(dto.createKTX2);
19   encoder.setKTX2UASTCSupercompression(dto.ktx2UASTCSupercompression);
20
21   // color / transfer
22   encoder.setPerceptual(dto.sRGB);
23   if (!dto.isHDRSourceFile)
24      encoder.setKTX2AndBasisSRGBTransferFunc(dto.sRGB);
25   encoder.setMipSRGB(dto.sRGB);
26
27   // source image (the DTO normalized the 3 legacy cases into one shape:
28   // pre-decoded RGBA has real dims; raw file bytes use 0,0 + an img_type)
29   const s = dto.source;
30   if (s.isHDRTarget)
31      encoder.setSliceSourceImageHDR(0, s.bytes, s.width, s.height, s.imgType, s.convertLDRToLinear, s.nitMultiplier);
32   else
33      encoder.setSliceSourceImage(0, s.bytes, s.width, s.height, s.imgType);
34
35   encoder.setFormatMode(dto.formatMode);
36   encoder.setRec2020(dto.rec2020);
37   encoder.setDebug(dto.debug);
38   encoder.setComputeStats(dto.computeStats);
39   encoder.setPrintStats(dto.printStats);
40   encoder.setStatusOutput(true);
41
42   // low-level codec opts -- run when the unified checkbox is OFF (legacy).
43   // NOTE this can run even for XUBC7 (when checkbox is off); applyUnified then
44   // overrides quality/effort afterward, exactly as the legacy path does.
45   if (dto.lowLevelOptsEnabled)
46   {
47      encoder.setUASTCHDRQualityLevel(dto.uastcHDRQuality);
48      encoder.setASTC_HDR_6x6_Level(dto.astcHDR6x6Level);
49      encoder.setLambda(dto.astc6x6Lambda);
50
51      // three-way quality branch (mirrors legacy): XUBC7 defers to the unified
52      // call (no setQualityLevel here); XUASTC uses DCT; everything else ETC1S.
53      if (dto.isXUBC7Target)
54      {
55         // intentionally nothing -- applyUnified handles XUBC7 quality/effort
56      }
57      else if (dto.isXUASTCLDRTarget)
58      {
59         if (dto.xuastcDCTQuality < 100)
60         {
61            encoder.setQualityLevel(dto.xuastcDCTQuality);
62            encoder.setXUASTCLDRUseDCT(true);
63         }
64      }
65      else
66      {
67         encoder.setQualityLevel(dto.etc1sQuality);
68      }
69
70      encoder.setXUASTCLDRUseLossySupercompression(dto.xuastcLossySupercompression);
71      encoder.setASTCOrXUASTCLDREffortLevel(dto.astcXuastcEffortLevel);
72      encoder.setRDOUASTC(dto.rdoUASTC);
73      encoder.setRDOUASTCQualityScalar(dto.rdoUASTCQualityScalar);
74      encoder.setPackUASTCFlags(dto.packUASTCFlags);
75      encoder.setETC1SCompressionLevel(dto.etc1sCompLevel);
76   }
77
78   // always-applied opts (available even when the unified path is used)
79   for (let i = 0; i < 6; i++)
80      encoder.setXUASTCLDRBoundedRDOParam(i, dto.xuastcBoundedRDO[i]);
81
82   encoder.setXUASTCLDRForceDisableRGBDualPlane(dto.xuastcDisableRGBDualPlane);
83   encoder.setXUASTCLDRForceDisableSubsets(dto.xuastcDisableSubsets);
84   encoder.setXUASTCLDRUseBlurring(dto.xuastcUseBlur);
85   encoder.setXUASTCLDRSelectCompressor(dto.xuastcSelectCompressor);
86   encoder.setXUASTCLDRHeavySubsetUsage(dto.xuastcHeavySubsetUsage);
87   encoder.setXUASTCLDRSharpenMode(dto.xuastcSharpenMode);
88   encoder.setXUASTCLDRSharpenAmount(dto.xuastcSharpenAmount);
89   encoder.setXUASTCLDRDeblockingMode(dto.xuastcDeblockingMode);
90   encoder.setXUASTCLDRNumDeblockingPasses(dto.xuastcNumDeblockingPasses);
91   encoder.setASTCOrXUASTCLDRWeights(dto.weights[0], dto.weights[1], dto.weights[2], dto.weights[3]);
92   encoder.setSwizzle(dto.swizzle[0], dto.swizzle[1], dto.swizzle[2], dto.swizzle[3]);
93   encoder.setXUASTCLDRSyntax(dto.xuastcSyntax);
94   encoder.setXUBC7RDOLevel(dto.xubc7RDOLevel);
95   encoder.setXUBC7NumStripes(dto.xubc7NumStripes);
96   encoder.setXUBC7Encoder(dto.xubc7Encoder);
97   encoder.setXUBC7BC7EScalarLevel(dto.xubc7BC7EScalarLevel);
98
99   // mipmaps
100   encoder.setMipGen(dto.mipGen);
101   encoder.setMipFilter(dto.mipFilter);
102   encoder.setMipScale(dto.mipScale);
103   encoder.setMipSmallestDimension(dto.mipSmallestDim);
104   encoder.setMipRenormalize(dto.mipRenormalize);
105   encoder.setMipWrapping(dto.mipWrapping);
106   encoder.setYFlip(dto.yFlip);
107
108   // unified quality/effort LAST -- it intentionally overrides some of the
109   // codec-specific low-level options set above (matches the legacy path)
110   if (dto.applyUnified)
111      encoder.setFormatModeAndQualityEffort(dto.formatMode, dto.unifiedQuality, dto.unifiedEffort, true);
112}
113
114// applyDDSEncodeDTO(encoder, dto): applies ONLY the DDS-export-specific options on top of a
115// BasisEncoder that has ALREADY had applyEncodeDTO() run on it. The caller then calls
116// encoder.encodeToDDS(buffer). Used by the DDS-export path (worker 'encodeDDS' message).
117//
118// PURE: depends only on (encoder, dto) -- no Module, no DOM (same contract as applyEncodeDTO).
119//
120// Everything DDS shares with KTX2 (source image, sRGB transfer func, perceptual, channel weights,
121// mips, etc.) was already set by applyEncodeDTO() and is consumed by encodeToDDS()/build_dds()
122// unchanged -- so we only set the genuinely NEW things here: the output format + BC7 packer knobs.
123function applyDDSEncodeDTO(encoder, dto)
124{
125   encoder.setDDSFormatEnum(dto.ddsFormat);
126   encoder.setDDSBC7Encoder(dto.ddsBC7Encoder);
127   encoder.setDDSBC7FLevel(dto.ddsBC7FLevel);
128   encoder.setDDSBC7EScalarLevel(dto.ddsBC7EScalarLevel);
129}

Line numbers count LF bytes from the start of the resource, as the search results do. Vendor segments are library code the classifier recognised; they are stored but not indexed. Bytes are shown as Latin1 characters, one per byte.