@@ -5,38 +5,57 @@ window.Module = {
55 }
66}
77
8- const processImageBtn = document . getElementById ( 'process-image-btn' )
9- const inputImage = document . getElementById ( 'input-image' )
10- const outputImage = document . getElementById ( 'output-image' )
11-
128const getImageData = ( ) => {
139 console . log ( '[getImageData]' )
10+ const img = document . getElementById ( 'input-image' )
1411 const canvas = document . createElement ( 'canvas' )
15- canvas . width = inputImage . width
16- canvas . height = inputImage . height
12+ canvas . width = img . width
13+ canvas . height = img . height
1714 ctx = canvas . getContext ( '2d' )
18- ctx . drawImage ( inputImage , 0 , 0 , inputImage . width , inputImage . height )
15+ ctx . drawImage ( img , 0 , 0 , img . width , img . height )
1916 const imageData = ctx . getImageData ( 0 , 0 , canvas . width , canvas . height )
2017 return imageData
2118}
2219
23- const drawOutputImage = ( dataOut , width , height ) => {
24- console . log ( '[drawOutputImage]' )
25- const array = new Uint8ClampedArray ( dataOut )
20+ const imageDataFrom1Channel = ( data , width , height ) => {
21+ console . log ( '[imageDataFrom1Channel]' )
22+ const cb = width * height * 4
23+ const array = new Uint8ClampedArray ( cb )
24+ data . forEach ( ( pixelValue , index ) => {
25+ const base = index * 4
26+ array [ base ] = pixelValue // R
27+ array [ base + 1 ] = pixelValue // G
28+ array [ base + 2 ] = pixelValue // B
29+ array [ base + 3 ] = 255 // A
30+ } )
31+ const imageData = new ImageData ( array , width , height )
32+ return imageData
33+ }
34+
35+ const imageDataFrom4Channels = ( data , width , height ) => {
36+ console . log ( '[imageDataFrom4Channels]' )
37+ const array = new Uint8ClampedArray ( data )
2638 const imageData = new ImageData ( array , width , height )
27- outputImage . width = width
28- outputImage . height = height
29- ctx = outputImage . getContext ( '2d' )
39+ return imageData
40+ }
41+
42+ const drawOutputImage = imageData => {
43+ console . log ( '[drawOutputImage]' )
44+ const canvas = document . getElementById ( 'output-image' )
45+ canvas . width = imageData . width
46+ canvas . height = imageData . height
47+ ctx = canvas . getContext ( '2d' )
3048 ctx . putImageData ( imageData , 0 , 0 )
3149}
3250
3351const onProcessImage = ( module , processImage ) => ( ) => {
3452 console . log ( '[onProcessImage]' )
3553 const { data, width, height } = getImageData ( )
3654 const dataOutAddr = processImage ( data , width , height )
37- const dataOutSize = width * height * 4
55+ const dataOutSize = width * height * 1
3856 const dataOut = module . HEAPU8 . slice ( dataOutAddr , dataOutAddr + dataOutSize )
39- drawOutputImage ( dataOut , width , height )
57+ const imageData = imageDataFrom1Channel ( dataOut , width , height )
58+ drawOutputImage ( imageData )
4059 module . _free ( dataOutAddr )
4160}
4261
@@ -52,5 +71,6 @@ const wrapProcessImage = module => {
5271const init = module => {
5372 console . log ( '[init]' )
5473 const processImage = wrapProcessImage ( module )
74+ const processImageBtn = document . getElementById ( 'process-image-btn' )
5575 processImageBtn . addEventListener ( 'click' , onProcessImage ( module , processImage ) )
5676}
0 commit comments