( texture, index, targetWidth, targetHeight, targetChannel, )
| 423 | let scratchResizeChannel; |
| 424 | |
| 425 | function getTextureChannel( |
| 426 | texture, |
| 427 | index, |
| 428 | targetWidth, |
| 429 | targetHeight, |
| 430 | targetChannel, |
| 431 | ) { |
| 432 | const pixels = texture.pixels; // RGBA |
| 433 | const sourceWidth = texture.width; |
| 434 | const sourceHeight = texture.height; |
| 435 | const sourcePixelsLength = sourceWidth * sourceHeight; |
| 436 | const targetPixelsLength = targetWidth * targetHeight; |
| 437 | |
| 438 | // Allocate the scratchResizeChannel on demand if the texture needs to be resized |
| 439 | let sourceChannel = targetChannel; |
| 440 | if (sourcePixelsLength > targetPixelsLength) { |
| 441 | if ( |
| 442 | !defined(scratchResizeChannel) || |
| 443 | sourcePixelsLength > scratchResizeChannel.length |
| 444 | ) { |
| 445 | scratchResizeChannel = Buffer.alloc(sourcePixelsLength); |
| 446 | } |
| 447 | sourceChannel = scratchResizeChannel; |
| 448 | } |
| 449 | |
| 450 | for (let i = 0; i < sourcePixelsLength; ++i) { |
| 451 | const value = pixels.readUInt8(i * 4 + index); |
| 452 | sourceChannel.writeUInt8(value, i); |
| 453 | } |
| 454 | |
| 455 | if (sourcePixelsLength > targetPixelsLength) { |
| 456 | resizeChannel( |
| 457 | sourceChannel, |
| 458 | sourceWidth, |
| 459 | sourceHeight, |
| 460 | targetChannel, |
| 461 | targetWidth, |
| 462 | targetHeight, |
| 463 | ); |
| 464 | } |
| 465 | |
| 466 | return targetChannel; |
| 467 | } |
| 468 | |
| 469 | function writeChannel(pixels, channel, index) { |
| 470 | const pixelsLength = pixels.length / 4; |
no test coverage detected