MCPcopy
hub / github.com/processing/p5.js / readFramebufferRegion

Method readFramebufferRegion

src/webgl/p5.RendererGL.js:1778–1835  ·  view source on GitHub ↗
(framebuffer, x, y, w, h)

Source from the content-addressed store, hash-verified

1776 }
1777
1778 readFramebufferRegion(framebuffer, x, y, w, h) {
1779 const gl = this.GL;
1780 const colorFormat = this._getFramebufferColorFormat(framebuffer);
1781
1782 const rawData = readPixelsWebGL(
1783 undefined,
1784 gl,
1785 framebuffer.framebuffer,
1786 x * framebuffer.density,
1787 y * framebuffer.density,
1788 w * framebuffer.density,
1789 h * framebuffer.density,
1790 colorFormat.format,
1791 colorFormat.type
1792 );
1793
1794 // Framebuffer data might be either a Uint8Array or Float32Array
1795 // depending on its format, and it may or may not have an alpha channel.
1796 // To turn it into an image, we have to normalize the data into a
1797 // Uint8ClampedArray with alpha.
1798 const fullData = new Uint8ClampedArray(
1799 w * h * framebuffer.density * framebuffer.density * 4
1800 );
1801 // Default channels that aren't in the framebuffer (e.g. alpha, if the
1802 // framebuffer is in RGB mode instead of RGBA) to 255
1803 fullData.fill(255);
1804
1805 const channels = colorFormat.format === gl.RGB ? 3 : 4;
1806 for (let yPos = 0; yPos < h * framebuffer.density; yPos++) {
1807 for (let xPos = 0; xPos < w * framebuffer.density; xPos++) {
1808 for (let channel = 0; channel < 4; channel++) {
1809 const idx = (yPos * w * framebuffer.density + xPos) * 4 + channel;
1810 if (channel < channels) {
1811 // Find the index of this pixel in `rawData`, which might have a
1812 // different number of channels
1813 const rawDataIdx = channels === 4
1814 ? idx
1815 : (yPos * w * framebuffer.density + xPos) * channels + channel;
1816 fullData[idx] = rawData[rawDataIdx];
1817 }
1818 }
1819 }
1820 }
1821
1822 // Create image from data
1823 const region = new Image(w * framebuffer.density, h * framebuffer.density);
1824 region.imageData = region.canvas.getContext('2d').createImageData(
1825 region.width,
1826 region.height
1827 );
1828 region.imageData.data.set(fullData);
1829 region.pixels = region.imageData.data;
1830 region.updatePixels();
1831 if (framebuffer.density !== 1) {
1832 region.pixelDensity(framebuffer.density);
1833 }
1834 return region;
1835 }

Callers 1

getMethod · 0.45

Calls 6

updatePixelsMethod · 0.95
pixelDensityMethod · 0.95
readPixelsWebGLFunction · 0.90
fillMethod · 0.45
setMethod · 0.45

Tested by

no test coverage detected