The scripts of WebP Studio
Version 1.0. These are the scripts exactly as they are in the stack that plays here. Each line has its number, and a link to a line keeps it.
the stack script
WebP Studio.oxtstack, stack "WebP Studio", 1259 lines
-- WebP Studio: pictures and animations written, read and played by -- webpxt, in motion. -- -- The stack opens straight into its first scene and plays the scenes -- one after another until "Play all" is turned off: -- 1. Squeeze it the picture written as WebP at falling quality, -- the file read back and shown beside the original -- 2. Every pixel the picture written without loss and read back the -- same, under a lens beside a lossy one -- 3. See-through a window cut in the picture, written with its -- transparency, over checks that move behind it -- 4. Animate thirty frames drawn and written into one file, -- then played back from that file -- 5. Your turn your own picture, written the way you choose, -- saved as a WebP file; an animation of yours plays -- -- The picture is an evening over a lake that this stack carries in a -- hidden image: drawn from noise for this stack, nothing borrowed. -- -- Every scene is played by a timer one frame at a time, with the screen -- locked while a frame is drawn: nothing here waits, so the stack -- answers a click at any moment. Every control is named "of me" (this -- stack): a message sent in time resolves a bare name against whichever -- stack is the defaultStack when it arrives. -- -- The stack keeps working without the library: it says so on its stage, -- shows its picture, and every button still answers. constant kTick = 40 constant kPicture = "78,132,638,512" constant kGood = "#34D399" constant kBad = "#F87171" constant kOrange = "#FF8A3D" constant kButton = "#2A3146" constant kText = "#E6E9F0" constant kDim = "#9199AC" constant kFaint = "#6B7385" constant kQualities = "90,70,50,35,20,10,5,2,15,40,80" constant kFrames = 30 constant kFrameMs = 80 constant kStrip = "90,424,626,531" constant kSettings = "Lossless,80,40,10" local sHasLibrary local sScene local sLast local sStep local sTimer local sNext local sTour local sData local sPicture local sPNGSize local sBuiltIn local sBuiltInPNG local sMine local sMinePNG local sOwn local sSetting local sFile local sAnim local sWriter local sDragging on preOpenStack wsStart end preOpenStack -- Straight into motion: the first picture is written as the stack opens. on openStack wsPlay 1 end openStack on closeStack wsStopAll pass closeStack end closeStack -- Asks the library who it is. A stack must never fail because a library -- is missing, so the first call is inside a try. command wsStart local tVersion, tError put false into sHasLibrary put true into sTour put empty into sScene put 1 into sLast put empty into sData put false into sOwn put 3 into sSetting put 0 into sAnim put 0 into sWriter put false into sDragging try put wpLibraryVersion() into tVersion put true into sHasLibrary catch tError put empty into tVersion end try lock screen wsClearStage wsUseBuiltIn if sHasLibrary then put tVersion into field "Version" of me set the textColor of field "Version" of me to kFaint else put "webpxt is not installed here" into field "Version" of me set the textColor of field "Version" of me to kBad end if wsShowSetting wsShowTour unlock screen end wsStart function wsHasLibrary return sHasLibrary is true end wsHasLibrary -- The number of the scene playing, empty when none is (the tests and -- Play all read it). function wsPlaying return sScene end wsPlaying -- The picture this stack carries, and how large it is as a PNG file. -- Scenes 1 to 4 always play with it; Your turn plays with yours once you -- have opened one. command wsUseBuiltIn local tPNG put the width of image "Source" of me into sBuiltIn["width"] put the height of image "Source" of me into sBuiltIn["height"] put the imageData of image "Source" of me into sBuiltIn["imageData"] put empty into sBuiltIn["alphaData"] export image "Source" of me to tPNG as PNG put the number of bytes in tPNG into sBuiltInPNG put sBuiltIn into sPicture put sBuiltInPNG into sPNGSize put empty into sMine put false into sOwn end wsUseBuiltIn -- 12345678 as "12,345,678". function wsCount pNumber local tOut put empty into tOut repeat while the number of chars in pNumber > 3 put comma & char -3 to -1 of pNumber before tOut delete char -3 to -1 of pNumber end repeat return pNumber & tOut end wsCount -- A time in milliseconds, said plainly. function wsMs pTime if pTime < 1 then return "under 1 ms" end if return pTime && "ms" end wsMs -- How many times smaller pSmall is than pLarge, said plainly. function wsTimes pSmall, pLarge if pSmall < 1 then return empty end if if pLarge / pSmall < 1.5 then return round(100 - 100 * pSmall / pLarge) & "% smaller than the PNG" end if return "1/" & round(pLarge / pSmall) && "of the PNG" end wsTimes ------------------------------------------------------------------------ -- Scenes: one timer, one frame at a time ------------------------------------------------------------------------ command wsStopAll if sTimer is not empty then cancel sTimer end if if sNext is not empty then cancel sNext end if put empty into sTimer put empty into sNext put false into sDragging -- an open animation holds its file and a decoder until it is closed, -- and a writer its frames until it is finished or dropped if wsHasLibrary() then if sAnim is not 0 and wpAnimIsValid(sAnim) then wpAnimClose sAnim end if if sWriter is not 0 and wpAnimEncoderIsValid(sWriter) then wpAnimEncoderDiscard sWriter end if end if put 0 into sAnim put 0 into sWriter end wsStopAll -- Starts a scene from its first frame. pFile is for scene 4 only: an -- animated WebP file of your own to play instead of the one made here. command wsPlay pScene, pFile local tError wsStopAll put pScene into sScene put pScene into sLast put 0 into sStep put empty into sData if pScene is 5 and sOwn then put sMine into sPicture put sMinePNG into sPNGSize else put sBuiltIn into sPicture put sBuiltInPNG into sPNGSize end if lock screen wsClearStage wsMarkScene pScene put wsAbout(pScene) into field "About" of me if not wsHasLibrary() then wsNotice put empty into sScene unlock screen exit wsPlay end if try switch pScene case 1 wsBeginSqueeze break case 2 wsBeginLossless break case 3 wsBeginWindow break case 4 wsBeginAnimate pFile break case 5 wsBeginTurn break end switch catch tError wsFail end try unlock screen -- Your turn waits for a choice: it has no frames if sScene is not empty and sScene is not 5 then wsSchedule end if end wsPlay command wsSchedule if sScene is not empty and sTimer is empty then send "wsTick" to me in kTick milliseconds put the result into sTimer end if end wsSchedule -- One frame of the scene that is playing. command wsTick local tDone, tError put empty into sTimer if sScene is empty or sScene is 5 then exit wsTick end if add 1 to sStep lock screen try switch sScene case 1 put wsFrameSqueeze(sStep) into tDone break case 2 put wsFrameLossless(sStep) into tDone break case 3 put wsFrameWindow(sStep) into tDone break case 4 put wsFrameAnimate(sStep) into tDone break end switch catch tError put "failed" into tDone wsFail end try unlock screen if tDone is "done" then wsSceneDone else if tDone is not "failed" then wsSchedule end if end wsTick -- The last frame stays on the stage. With Play all on, the next scene -- starts a moment later. command wsSceneDone put empty into sScene if sTour then send "wsPlayNext" to me in 1600 milliseconds put the result into sNext end if end wsSceneDone -- Play all: the scene after the one that ended (5, Your turn, is never -- played by Play all). command wsPlayNext put empty into sNext wsPlay (sLast mod 4) + 1 end wsPlayNext -- A scene that stops says so plainly, and the stack goes on answering. command wsFail local tWhy put empty into sScene put wpLastError() into tWhy if tWhy is empty then put "This scene stopped on an error." into tWhy else put "This scene stopped: " & tWhy into tWhy end if wsHud tWhy, kBad end wsFail command wsMarkScene pScene local tN repeat with tN = 1 to 5 if tN is pScene then set the backgroundColor of button ("Scene" && tN) of me to kOrange else set the backgroundColor of button ("Scene" && tN) of me to kButton end if end repeat end wsMarkScene command wsShowTour if sTour then set the backgroundColor of button "Tour" of me to kOrange set the label of button "Tour" of me to "Play all: on" else set the backgroundColor of button "Tour" of me to kButton set the label of button "Tour" of me to "Play all: off" end if end wsShowTour command wsShowSetting local tN repeat with tN = 1 to 4 if tN is sSetting then set the backgroundColor of button ("Setting" && tN) of me to kOrange else set the backgroundColor of button ("Setting" && tN) of me to kButton end if end repeat end wsShowSetting function wsAbout pScene switch pScene case 1 return "The picture written as a WebP file again and again, each time at a lower quality, and read back. Left of the line is the original; right of it, what came back from the file. Watch the file shrink to a sliver of the PNG before the eye sees a change." case 2 return "Written without loss, the picture comes back the same to the last pixel, in a file smaller than the PNG. The lens shows it four times larger, beside the same place from a lossy file at quality 10." case 3 return "A window cut in the picture: outside it is see-through. The picture is written as a WebP file with its transparency and read back many times a second. The moving checks are not in the file: they show through it." case 4 return "Thirty frames are drawn by this stack and written one at a time into one animated WebP file. Then that file is opened and played back from the file, a frame at a time." case 5 return "Your turn. Open a picture of your own: a WebP, PNG, JPEG or GIF, and an animated WebP plays. Choose how it is written, drag the line to compare, and save the WebP file." end switch return empty end wsAbout ------------------------------------------------------------------------ -- The stage ------------------------------------------------------------------------ command wsClearStage local tN repeat for each item tN in "Before,After,Zoom 1,Zoom 2,Frame,Play,Checker,Strip" hide image tN of me end repeat hide group "Back" of me hide group "Split" of me repeat for each item tN in "Divider,Knob,Lens,Bar 1,Bar 2,Bar 3" hide graphic tN of me end repeat repeat for each item tN in "Left tag,Right tag,HUD" hide field tN of me end repeat repeat for each item tN in "Big,Notice,Readout,Bar label 1,Bar label 2,Bar label 3,Zoom label 1,Zoom label 2,Frame caption,Play caption,Strip caption,Verdict" put empty into field tN of me end repeat end wsClearStage command wsNotice put "This engine does not have webpxt, the WebP library, so no WebP file can be written or read here." & return & return & "Install webpxt from the xTalk Suite, or open this stack in the OXT-Beyond web gallery, where it is built in." into field "Notice" of me wsShowPicture "Before", sPicture, kPicture set the blendLevel of image "Before" of me to 75 wsHud "webpxt is missing", kBad end wsNotice -- The small box at the top left of the stage, sized to its words. command wsHud pText, pColor if pColor is empty then put kDim into pColor end if put pText into field "HUD" of me set the textColor of field "HUD" of me to pColor set the width of field "HUD" of me to min(640, the formattedWidth of field "HUD" of me + 8) set the left of field "HUD" of me to 34 show field "HUD" of me end wsHud -- Puts a picture (an array with width, height, imageData and, when it -- has one, alphaData) into image pName, shown in pRect: the size is set -- before the pixels, because the engine reads them against the image's -- size, and the image is then stretched to pRect. A picture the library -- says is not see-through ("translucent" false) is shown without its -- alphaData, which is all solid. command wsShowPicture pName, pPicture, pRect local tImage put the long id of image pName of me into tImage set the lockLoc of tImage to true -- a picture shown here before may have left its transparency behind set the text of tImage to empty set the rect of tImage to item 1 of pRect, item 2 of pRect, item 1 of pRect + pPicture["width"], item 2 of pRect + pPicture["height"] set the imageData of tImage to pPicture["imageData"] if pPicture["alphaData"] is not empty and pPicture["translucent"] is not false then set the alphaData of tImage to pPicture["alphaData"] end if set the rect of tImage to pRect set the blendLevel of tImage to 0 show tImage end wsShowPicture -- The rect a picture of pWidth x pHeight takes in the middle of pArea, -- made smaller to fit; larger than it is only when pGrow is true. function wsFitRect pWidth, pHeight, pArea, pGrow local tScale, tW, tH, tLeft, tTop put min((item 3 of pArea - item 1 of pArea) / pWidth, (item 4 of pArea - item 2 of pArea) / pHeight) into tScale if pGrow is not true then put min(1, tScale) into tScale end if put max(1, round(pWidth * tScale)) into tW put max(1, round(pHeight * tScale)) into tH put (item 1 of pArea + item 3 of pArea - tW) div 2 into tLeft put (item 2 of pArea + item 4 of pArea - tH) div 2 into tTop return tLeft & comma & tTop & comma & tLeft + tW & comma & tTop + tH end wsFitRect -- A small tag on the picture, sized to its words. command wsTag pName, pText, pLeft, pRight, pTop put pText into field pName of me set the width of field pName of me to the formattedWidth of field pName of me + 10 if pRight is not empty then set the topRight of field pName of me to pRight, pTop else set the topLeft of field pName of me to pLeft, pTop end if show field pName of me end wsTag -- A bar and its words, the bar pWidth of the pFull pixels it may take. command wsBar pN, pText, pLeft, pTop, pWidth, pColor, pFull if pFull is empty then put 560 into pFull end if put pText into field ("Bar label" && pN) of me set the rect of field ("Bar label" && pN) of me to pLeft - 4, pTop - 4, pLeft + pFull + 4, pTop + 18 set the backgroundColor of graphic ("Bar" && pN) of me to pColor set the rect of graphic ("Bar" && pN) of me to pLeft, pTop + 20, pLeft + max(2, round(pWidth)), pTop + 30 show graphic ("Bar" && pN) of me end wsBar ------------------------------------------------------------------------ -- The split view: the original left of a line, the file read back right -- of it (scenes 1 and 5) ------------------------------------------------------------------------ -- The rect the picture takes on the stage, and the file read back placed -- over it, whole, inside group "Split", which shows only its right part. command wsBeginSplit pRect put pRect into sData["rect"] wsShowPicture "Before", sPicture, pRect set the rect of group "Split" of me to pRect show group "Split" of me put item 1 of pRect + (item 3 of pRect - item 1 of pRect) div 2 into sData["line"] set the rect of graphic "Divider" of me to sData["line"] - 1, item 2 of pRect, sData["line"] + 1, item 4 of pRect show graphic "Divider" of me set the loc of graphic "Knob" of me to sData["line"], (item 2 of pRect + item 4 of pRect) div 2 show graphic "Knob" of me wsSplitAt sData["line"] end wsBeginSplit -- Moves the line to pX: left of it the original, right of it the file. command wsSplitAt pX local tRect put sData["rect"] into tRect put max(item 1 of tRect + 1, min(item 3 of tRect - 1, round(pX))) into pX put pX into sData["line"] set the rect of group "Split" of me to pX, item 2 of tRect, item 3 of tRect, item 4 of tRect set the left of graphic "Divider" of me to pX - 1 set the loc of graphic "Knob" of me to pX, (item 2 of tRect + item 4 of tRect) div 2 end wsSplitAt -- Writes the picture as pSetting says ("Lossless" or a quality), reads -- the file back into the right of the split, and says what it cost. command wsWriteSplit pSetting local tStart, tWrite, tBack, tRead, tRect, tHow, tBytes, tPNG, tTimes put the milliseconds into tStart if pSetting is "Lossless" then put wpEncodeAdvanced(sPicture["imageData"], sPicture["alphaData"], sPicture["width"], sPicture["height"], 0, true, 1) into sFile put "wpEncodeAdvanced, lossless" into tHow else put wpEncode(sPicture["imageData"], sPicture["alphaData"], sPicture["width"], sPicture["height"], pSetting) into sFile put "wpEncode at quality " & pSetting into tHow end if put the milliseconds - tStart into tWrite if sFile is empty then wsHud "The picture could not be written: " & wpLastError(), kBad exit wsWriteSplit end if put the milliseconds into tStart put wpDecode(sFile) into tBack put the milliseconds - tStart into tRead if tBack["width"] is empty then wsHud "The file could not be read back: " & wpLastError(), kBad exit wsWriteSplit end if put sData["rect"] into tRect wsShowPicture "After", tBack, tRect put the number of bytes in sFile into tBytes put wsTimes(tBytes, sPNGSize) into tTimes put wsTagRect() into tRect wsTag "Left tag", "Original", item 1 of tRect + 8, empty, item 2 of tRect + 8 if pSetting is "Lossless" then wsTag "Right tag", "WebP, lossless: " & wsCount(tBytes) && "bytes", empty, item 3 of tRect - 8, item 2 of tRect + 8 else wsTag "Right tag", "WebP, quality " & pSetting & ": " & wsCount(tBytes) && "bytes", empty, item 3 of tRect - 8, item 2 of tRect + 8 end if put tBytes into sData["bytes"] wsBar 1, "PNG: " & wsCount(sPNGSize) && "bytes", 78, 524, 560, "#4A5370" put "WebP: " & wsCount(tBytes) && "bytes, " & tTimes into sData["barText"] put 560 * tBytes / sPNGSize into sData["barTarget"] if sData["barNow"] is empty then put sData["barTarget"] into sData["barNow"] end if wsBar 2, sData["barText"], 78, 558, sData["barNow"], kOrange wsHud tHow & ": " & wsCount(tBytes) && "bytes in " & wsMs(tWrite) & " | wpDecode read it back in " & wsMs(tRead), kDim put "PNG: " & wsCount(sPNGSize) && "bytes" & return & "WebP: " & wsCount(tBytes) && "bytes, " & tTimes into field "Readout" of me end wsWriteSplit -- Where the two tags go: on the picture's top corners, or on the -- stage's when the picture is too narrow to hold both. function wsTagRect if item 3 of sData["rect"] - item 1 of sData["rect"] < 400 then return kPicture end if return sData["rect"] end wsTagRect -- The left tag names your picture when it is yours. command wsOwnTag local tRect if sOwn then put wsTagRect() into tRect wsTag "Left tag", "Your picture", item 1 of tRect + 8, empty, item 2 of tRect + 8 end if end wsOwnTag -- The WebP bar grows or shrinks towards its new length a little each -- frame, instead of jumping. command wsEaseBar if sData["barTarget"] is empty then exit wsEaseBar end if add (sData["barTarget"] - sData["barNow"]) * 0.3 to sData["barNow"] wsBar 2, sData["barText"], 78, 558, sData["barNow"], kOrange end wsEaseBar ------------------------------------------------------------------------ -- 1. Squeeze it ------------------------------------------------------------------------ command wsBeginSqueeze wsBeginSplit kPicture put 0 into sData["quality"] wsNextQuality end wsBeginSqueeze command wsNextQuality add 1 to sData["quality"] wsWriteSplit item sData["quality"] of kQualities end wsNextQuality -- The line sweeps to and fro every frame; a new quality every 18. function wsFrameSqueeze pStep local tRect, tHalf put sData["rect"] into tRect put (item 3 of tRect - item 1 of tRect) div 2 into tHalf wsSplitAt item 1 of tRect + tHalf + (tHalf - 50) * sin(pStep * 2 * pi / 150) wsEaseBar if pStep mod 18 is 0 then if sData["quality"] >= the number of items of kQualities then return "done" end if wsNextQuality end if return empty end wsFrameSqueeze ------------------------------------------------------------------------ -- 2. Every pixel kept ------------------------------------------------------------------------ command wsBeginLossless wsShowPicture "Before", sPicture, "40,128,320,318" put "40,128,320,318" into sData["small"] wsHud "Writing the picture without loss...", kDim put "Lossless: every pixel, 4 times larger" into field "Zoom label 1" of me put "Lossy at quality 10, 4 times larger" into field "Zoom label 2" of me end wsBeginLossless -- The lossless file is written in frame 1, after the words above are on -- the screen, and the lossy one in frame 2; from frame 3 the lens moves. function wsFrameLossless pStep local tT, tX, tY if pStep is 1 then wsWriteLossless return empty end if if sData["lossless"] is empty then return "done" end if if pStep is 2 then wsWriteLossy return empty end if if sData["lossy"] is empty then return "done" end if put pStep * 0.035 into tT put sPicture["width"] div 2 + (sPicture["width"] div 2 - 40) * sin(tT * 0.9) into tX put sPicture["height"] div 2 + (sPicture["height"] div 2 - 26) * sin(tT * 1.3 + 1) into tY wsLens tX, tY if pStep >= 230 then return "done" end if return empty end wsFrameLossless -- The picture written without loss, read back and compared, byte for -- byte. command wsWriteLossless local tStart, tTime, tLossless, tBack, tSame, tRead put the milliseconds into tStart put wpEncodeAdvanced(sPicture["imageData"], sPicture["alphaData"], sPicture["width"], sPicture["height"], 0, true, 1) into tLossless put the milliseconds - tStart into tTime if tLossless is empty then wsHud "The picture could not be written: " & wpLastError(), kBad exit wsWriteLossless end if put the milliseconds into tRead put wpDecode(tLossless) into tBack put the milliseconds - tRead into tRead put tBack["imageData"] into sData["lossless"] put the number of bytes in tLossless into sData["losslessBytes"] -- byte for byte: a comparison that ignored case would let a few -- bytes differ set the caseSensitive to true put tBack["imageData"] is sPicture["imageData"] into tSame wsBar 1, "PNG: " & wsCount(sPNGSize) && "bytes", 376, 132, 280, "#4A5370", 280 wsBar 2, "WebP, lossless: " & wsCount(the number of bytes in tLossless) && "bytes", 376, 182, 280 * the number of bytes in tLossless / sPNGSize, kGood, 280 if tSame then put "Read back: every one of " & wsCount(sPicture["width"] * sPicture["height"]) && "pixels the same" into field "Verdict" of me set the textColor of field "Verdict" of me to kGood wsHud "wpEncodeAdvanced, lossless: " & wsCount(the number of bytes in tLossless) && "bytes in " & wsMs(tTime) & " | read back exact in " & wsMs(tRead), kGood else put "Read back: not every pixel the same" into field "Verdict" of me set the textColor of field "Verdict" of me to kBad wsHud "wpEncodeAdvanced, lossless: the file did not come back the same", kBad end if put "Lossless: " & wsTimes(the number of bytes in tLossless, sPNGSize) into field "Readout" of me end wsWriteLossless -- The same picture written at quality 10 and read back, for the lens. command wsWriteLossy local tLossy, tBack put wpEncode(sPicture["imageData"], sPicture["alphaData"], sPicture["width"], sPicture["height"], 10) into tLossy if tLossy is empty then wsHud "The picture could not be written: " & wpLastError(), kBad exit wsWriteLossy end if put wpDecode(tLossy) into tBack put tBack["imageData"] into sData["lossy"] wsBar 3, "WebP, quality 10: " & wsCount(the number of bytes in tLossy) && "bytes", 376, 232, 280 * the number of bytes in tLossy / sPNGSize, kOrange, 280 put "Lossless: " & wsTimes(sData["losslessBytes"], sPNGSize) & return & "Quality 10: " & wsTimes(the number of bytes in tLossy, sPNGSize) into field "Readout" of me show graphic "Lens" of me end wsWriteLossy -- The lens at pX,pY of the picture: 70 x 46 pixels of each file, shown -- four times larger. command wsLens pX, pY local tLeft, tTop, tSmall, tScale put max(0, min(sPicture["width"] - 70, round(pX) - 35)) into tLeft put max(0, min(sPicture["height"] - 46, round(pY) - 23)) into tTop wsShowCrop "Zoom 1", sData["lossless"], tLeft, tTop, "40,364,320,548" wsShowCrop "Zoom 2", sData["lossy"], tLeft, tTop, "376,364,656,548" put sData["small"] into tSmall put (item 3 of tSmall - item 1 of tSmall) / sPicture["width"] into tScale set the rect of graphic "Lens" of me to item 1 of tSmall + round(tLeft * tScale), item 2 of tSmall + round(tTop * tScale), item 1 of tSmall + round((tLeft + 70) * tScale), item 2 of tSmall + round((tTop + 46) * tScale) end wsLens -- 70 x 46 pixels of pData from pLeft,pTop, into image pName, stretched to -- pRect. command wsShowCrop pName, pData, pLeft, pTop, pRect local tY, tCrop, tRow, tWidth put sPicture["width"] * 4 into tRow repeat with tY = pTop to pTop + 45 put byte (tY * tRow + pLeft * 4 + 1) to (tY * tRow + pLeft * 4 + 280) of pData after tCrop end repeat put 70 into tWidth set the lockLoc of image pName of me to true set the rect of image pName of me to item 1 of pRect, item 2 of pRect, item 1 of pRect + tWidth, item 2 of pRect + 46 set the imageData of image pName of me to tCrop set the rect of image pName of me to pRect show image pName of me end wsShowCrop ------------------------------------------------------------------------ -- 3. See-through ------------------------------------------------------------------------ command wsBeginWindow put sPicture["width"] into sData["w"] put sPicture["height"] into sData["h"] -- see-through and solid rows to cut each row of the window from put empty into sData["clear"] put empty into sData["solid"] repeat sData["w"] times put numToByte(0) after sData["clear"] put numToByte(255) after sData["solid"] end repeat set the rect of group "Back" of me to kPicture set the topLeft of image "Checker" of me to item 1 of kPicture - 64, item 2 of kPicture - 64 show image "Checker" of me show group "Back" of me set the rect of group "Split" of me to kPicture show group "Split" of me wsCutWindow 0 end wsBeginWindow function wsFrameWindow pStep local tShift -- the checks drift every frame; the window changes every third put pStep mod 64 into tShift set the topLeft of image "Checker" of me to item 1 of kPicture - 64 + tShift, item 2 of kPicture - 64 + tShift if pStep mod 3 is 0 then wsCutWindow pStep end if if pStep >= 200 then return "done" end if return empty end wsFrameWindow -- A round window that drifts and breathes; outside it see-through. -- Written with the window, read back, shown. Every pixel is either solid -- or clear: the engine takes ten times as long to show a picture with -- half see-through pixels, and this one is shown many times a second. command wsCutWindow pStep local tW, tH, tCx, tCy, tR, tY, tHalf, tAlpha, tFrom, tStart, tFile, tBack, tInfo, tTime put sData["w"] into tW put sData["h"] into tH put tW div 2 + 120 * sin(pStep * 0.021) into tCx put tH div 2 + 40 * sin(pStep * 0.033 + 0.6) into tCy put 110 + 45 * sin(pStep * 0.05) into tR -- each row: see-through, solid, see-through; tFrom pixels before -- the window repeat with tY = 0 to tH - 1 put 0 into tHalf if abs(tY - tCy) < tR then put min(tW div 2, round(sqrt(tR * tR - (tY - tCy) * (tY - tCy)))) into tHalf end if if tHalf < 1 then put sData["clear"] after tAlpha next repeat end if put max(0, min(tW - 2 * tHalf, round(tCx) - tHalf)) into tFrom put byte 1 to tFrom of sData["clear"] & byte 1 to (2 * tHalf) of sData["solid"] & byte 1 to (tW - tFrom - 2 * tHalf) of sData["clear"] after tAlpha end repeat put the milliseconds into tStart put wpEncode(sPicture["imageData"], tAlpha, tW, tH, 80) into tFile put wpDecode(tFile) into tBack put the milliseconds - tStart into tTime if tBack["width"] is empty then wsHud "The picture could not be written: " & wpLastError(), kBad exit wsCutWindow end if wsShowPicture "After", tBack, kPicture put wpInfo(tFile) into tInfo wsHud "wpEncode with transparency: " & wsCount(the number of bytes in tFile) && "bytes | wpInfo: alpha " & tInfo["alpha"] & " | written and read back in " & wsMs(tTime), kGood put "The file: " & wsCount(the number of bytes in tFile) && "bytes, " & tW & " x " & tH & return & "Its transparency: " & tInfo["alpha"] & return & "Read back: translucent " & tBack["translucent"] into field "Readout" of me end wsCutWindow ------------------------------------------------------------------------ -- 4. Animate ------------------------------------------------------------------------ -- With pFile (an animated WebP of your own) it only plays, for ever; -- otherwise it draws thirty frames into a new file first. command wsBeginAnimate pFile local tInfo if pFile is not empty then put true into sData["own"] put 0 into sData["written"] put "Your animation, played from its file" into field "Big" of me set the rect of field "Play caption" of me to 56, 566, 660, 590 wsOpenAnimation pFile, "60,170,656,556" exit wsBeginAnimate end if put false into sData["own"] wsBackground 300, 200 put wpAnimEncoderCreate(300, 200, 0, 80, false) into sWriter if sWriter is 0 then wsHud "The animation could not be started: " & wpLastError(), kBad exit wsBeginAnimate end if put 0 into sData["written"] set the rect of field "Frame caption" of me to 36, 380, 344, 404 set the rect of field "Play caption" of me to 372, 380, 680, 404 put "Being written" into field "Big" of me wsBeginStrip put "wpAnimEncoderCreate: 300 x 200, quality 80, loops for ever" & return & "Frames added: 0" into field "Readout" of me end wsBeginAnimate -- The strip of the file's frames under the pictures: thumbnails of 50 x -- 33, ten to a row, each added as its frame is written. command wsBeginStrip local tRow, tData put wsRun(255, 6, 8, 18, 536) into tRow repeat 107 times put tRow after tData end repeat put tData into sData["strip"] set the lockLoc of image "Strip" of me to true set the text of image "Strip" of me to empty set the rect of image "Strip" of me to kStrip set the imageData of image "Strip" of me to sData["strip"] show image "Strip" of me set the rect of field "Strip caption" of me to item 1 of kStrip - 4, item 4 of kStrip + 6, item 3 of kStrip + 4, item 4 of kStrip + 26 put "In the file so far: no frames" into field "Strip caption" of me end wsBeginStrip -- Frame pIndex (from 1) into the strip, made small by the engine. command wsThumb pIndex, pImageData local tLoader, tThumb, tR, tLeft, tTop, tAt put the long id of image "Loader" of me into tLoader set the lockLoc of tLoader to true set the text of tLoader to empty set the resizeQuality of tLoader to "good" set the rect of tLoader to 0, 0, 300, 200 set the imageData of tLoader to pImageData set the rect of tLoader to 0, 0, 50, 33 put the imageData of tLoader into tThumb set the text of tLoader to empty put ((pIndex - 1) mod 10) * 54 into tLeft put ((pIndex - 1) div 10) * 37 into tTop repeat with tR = 0 to 32 put ((tTop + tR) * 536 + tLeft) * 4 + 1 into tAt put byte (tR * 200 + 1) to (tR * 200 + 200) of tThumb into byte tAt to (tAt + 199) of sData["strip"] end repeat set the imageData of image "Strip" of me to sData["strip"] end wsThumb -- The frame playing, marked in the strip. command wsMarkThumb pIndex local tLeft, tTop put item 1 of kStrip + ((pIndex - 1) mod 10) * 54 into tLeft put item 2 of kStrip + ((pIndex - 1) div 10) * 37 into tTop set the rect of graphic "Lens" of me to tLeft - 2, tTop - 2, tLeft + 52, tTop + 35 show graphic "Lens" of me end wsMarkThumb -- The picture, made 300 x 200, as the background of every frame. command wsBackground pWidth, pHeight local tRect put the rect of image "Source" of me into tRect set the lockLoc of image "Source" of me to true set the resizeQuality of image "Source" of me to "good" set the rect of image "Source" of me to 0, 0, pWidth, round(pWidth * sPicture["height"] / sPicture["width"]) put byte 1 to (pWidth * pHeight * 4) of the imageData of image "Source" of me into sData["back"] set the rect of image "Source" of me to tRect end wsBackground -- Frame pIndex (from 0) of a ball bouncing across the evening, as -- imageData, 300 x 200. function wsBallFrame pIndex local tData, tT, tCx, tCy, tY, tHalf, tStart, tBall, tLight, tShade put sData["back"] into tData put pIndex / kFrames into tT put round(24 + tT * 252) into tCx put round(150 - abs(sin(tT * 3 * pi)) * 110) into tCy -- runs of one colour, long enough for the widest row put wsRun(255, 255, 138, 61, 30) into tBall put wsRun(255, 255, 214, 170, 10) into tLight put wsRun(255, 16, 20, 34, 30) into tShade -- its shadow on the water, smaller the higher it is put round(14 - (150 - tCy) / 12) into tHalf repeat with tY = 172 to 175 put (tY * 300 + tCx - tHalf) * 4 + 1 into tStart put byte 1 to (8 * tHalf) of tShade into byte tStart to (tStart + 8 * tHalf - 1) of tData end repeat -- the ball, and a light on it repeat with tY = tCy - 13 to tCy + 13 put round(sqrt(max(0, 169 - (tY - tCy) * (tY - tCy)))) into tHalf if tHalf > 0 then put (tY * 300 + tCx - tHalf) * 4 + 1 into tStart put byte 1 to (8 * tHalf) of tBall into byte tStart to (tStart + 8 * tHalf - 1) of tData end if if abs(tY - (tCy - 5)) < 4 then put round(sqrt(16 - (tY - tCy + 5) * (tY - tCy + 5))) into tHalf if tHalf > 0 then put (tY * 300 + tCx - 5 - tHalf) * 4 + 1 into tStart put byte 1 to (8 * tHalf) of tLight into byte tStart to (tStart + 8 * tHalf - 1) of tData end if end if end repeat return tData end wsBallFrame -- pCount pixels of one colour, as imageData. function wsRun pUnused, pRed, pGreen, pBlue, pCount local tPixel, tOut put numToByte(pUnused) & numToByte(pRed) & numToByte(pGreen) & numToByte(pBlue) into tPixel repeat pCount times put tPixel after tOut end repeat return tOut end wsRun -- A frame written every second frame; then the file, played twice. function wsFrameAnimate pStep local tPicture, tFile, tInfo if sData["written"] < kFrames and not sData["own"] then if pStep mod 2 is 1 then put 300 into tPicture["width"] put 200 into tPicture["height"] put wsBallFrame(sData["written"]) into tPicture["imageData"] if not wpAnimEncoderAddFrame(sWriter, tPicture["imageData"], empty, kFrameMs) then wsHud "Frame " & sData["written"] + 1 & " was refused: " & wpLastError(), kBad return "done" end if add 1 to sData["written"] wsShowPicture "Frame", tPicture, "40,170,340,370" put "Frame " & wpAnimEncoderFrameCount(sWriter) & " of " & kFrames & " written" into field "Frame caption" of me wsThumb sData["written"], tPicture["imageData"] if sData["written"] is 1 then put "In the file so far: 1 frame" into field "Strip caption" of me else put "In the file so far:" && sData["written"] && "frames" into field "Strip caption" of me end if put "wpAnimEncoderCreate: 300 x 200, quality 80, loops for ever" & return & "Frames added: " & wpAnimEncoderFrameCount(sWriter) & return & "Being written: " & wpAnimEncoderOpenCount() into field "Readout" of me wsHud "wpAnimEncoderAddFrame: frame " & sData["written"] & " of " & kFrames, kDim if sData["written"] is kFrames then put wpAnimEncoderFinish(sWriter) into tFile -- finishing closes the writer: its handle is no longer valid put 0 into sWriter if tFile is empty then wsHud "The animation could not be written: " & wpLastError(), kBad return "done" end if put tFile into sData["file"] put "Written, and played back from the file" into field "Big" of me put "The file's" && kFrames && "frames; the one playing is marked" into field "Strip caption" of me wsOpenAnimation tFile, "376,170,676,370" end if end if return empty end if if sAnim is 0 then return "done" end if if pStep >= sData["due"] then wsNextFrame end if if not sData["own"] and sData["shown"] >= 2 * wpAnimFrameCount(sAnim) then wpAnimClose sAnim put 0 into sAnim return "done" end if return empty end wsFrameAnimate -- Opens a WebP file to read a frame at a time, shown in image "Play" -- inside pArea. command wsOpenAnimation pFile, pArea local tInfo put wpAnimOpen(pFile) into sAnim if sAnim is 0 then wsHud "That file could not be opened as an animation: " & wpLastError(), kBad exit wsOpenAnimation end if put wpInfo(pFile) into tInfo put pArea into sData["area"] put 0 into sData["shown"] put sStep into sData["due"] put the number of bytes in pFile into sData["bytes"] wsHud "wpAnimOpen: one file of " & wsCount(sData["bytes"]) && "bytes, " & wpAnimFrameCount(sAnim) && "frames, played with wpAnimNextFrame", kGood put "Size: " & wpAnimWidth(sAnim) & " x " & wpAnimHeight(sAnim) & return & "Frames: " & wpAnimFrameCount(sAnim) & ", loops: " & wsLoops(wpAnimLoopCount(sAnim)) & return & "File: " & wsCount(sData["bytes"]) && "bytes" & return & "Open: " & wpAnimOpenCount() & "; being written: " & wpAnimEncoderOpenCount() into field "Readout" of me end wsOpenAnimation function wsLoops pLoops if pLoops is 0 then return "for ever" end if return pLoops end wsLoops -- The next frame of the open animation, and when the one after is due. -- After the last frame it starts again. command wsNextFrame local tFrame put wpAnimNextFrame(sAnim) into tFrame if tFrame["width"] is empty then wpAnimRewind sAnim put wpAnimNextFrame(sAnim) into tFrame end if if tFrame["width"] is empty then wsHud "A frame could not be read: " & wpLastError(), kBad put sStep + 100000 into sData["due"] exit wsNextFrame end if add 1 to sData["shown"] -- an animation of your own is shown as large as the stage lets it be wsShowPicture "Play", tFrame, wsFitRect(tFrame["width"], tFrame["height"], sData["area"], sData["own"]) put "Frame " & tFrame["index"] & " of " & wpAnimFrameCount(sAnim) & ", " & tFrame["duration"] & " ms, read from the file" into field "Play caption" of me if not sData["own"] then wsMarkThumb tFrame["index"] end if put sStep + max(1, round(tFrame["duration"] / kTick)) into sData["due"] end wsNextFrame ------------------------------------------------------------------------ -- 5. Your turn ------------------------------------------------------------------------ command wsBeginTurn put false into sTour wsShowTour wsShowSetting -- your picture, as large as the stage lets it be, so that what the -- file did to it can be seen wsBeginSplit wsFitRect(sPicture["width"], sPicture["height"], kPicture, true) wsWriteSplit item sSetting of kSettings put sData["barTarget"] into sData["barNow"] wsBar 2, sData["barText"], 78, 558, sData["barNow"], kOrange wsOwnTag end wsBeginTurn command wsPickSetting pN put pN into sSetting wsShowSetting if sScene is not 5 then wsPlay 5 else lock screen wsWriteSplit item sSetting of kSettings put sData["barTarget"] into sData["barNow"] wsBar 2, sData["barText"], 78, 558, sData["barNow"], kOrange wsOwnTag unlock screen end if end wsPickSetting command wsSaveWebP local tPath if not wsHasLibrary() then wsPlay 5 exit wsSaveWebP end if if sScene is not 5 then wsPlay 5 end if if sFile is empty then exit wsSaveWebP end if ask file "Save the picture as:" with "picture.webp" put it into tPath if tPath is empty then exit wsSaveWebP end if put sFile into url ("binfile:" & tPath) put "Saved " & wsCount(the number of bytes in sFile) && "bytes of WebP." into field "Readout" of me end wsSaveWebP command wsOpenAPicture local tPath if not wsHasLibrary() then wsPlay 5 exit wsOpenAPicture end if answer file "Choose a picture (WebP, PNG, JPEG or GIF):" put it into tPath if tPath is empty then exit wsOpenAPicture end if wsOpenPictureData url ("binfile:" & tPath) end wsOpenAPicture -- The bytes of a picture file. A WebP file is read by the library, a -- large one decoded straight to a size that fits; any other kind by the -- engine's own image object. An animated WebP plays. command wsOpenPictureData pData local tInfo, tPicture, tRect, tPNG put false into sTour wsShowTour put wpInfo(pData) into tInfo if tInfo["width"] is not empty then if tInfo["animated"] is true then wsPlay 4, pData exit wsOpenPictureData end if put wsFitRect(tInfo["width"], tInfo["height"], "0,0,560,380") into tRect if tInfo["width"] > 560 or tInfo["height"] > 380 then put wpDecodeScaled(pData, item 3 of tRect - item 1 of tRect, item 4 of tRect - item 2 of tRect) into tPicture else put wpDecode(pData) into tPicture end if if tPicture["width"] is empty then wsPlay 5 wsHud "That WebP file could not be read: " & wpLastError(), kBad exit wsOpenPictureData end if else put wsOtherPicture(pData) into tPicture if tPicture["width"] is empty then wsPlay 5 wsHud "That file is not a picture this stack can open. Try a WebP, a PNG, a JPEG or a GIF.", kBad exit wsOpenPictureData end if end if put tPicture into sMine if sMine["translucent"] is not true and sMine["alphaData"] is not empty and not wsAnyClear(sMine["alphaData"]) then put empty into sMine["alphaData"] end if -- the size it would have as a PNG, the yardstick of every bar lock screen set the lockLoc of image "Loader" of me to true set the rect of image "Loader" of me to 0, 0, sMine["width"], sMine["height"] set the imageData of image "Loader" of me to sMine["imageData"] if sMine["alphaData"] is not empty then set the alphaData of image "Loader" of me to sMine["alphaData"] end if export image "Loader" of me to tPNG as PNG set the text of image "Loader" of me to empty unlock screen put the number of bytes in tPNG into sMinePNG put true into sOwn wsPlay 5 end wsOpenPictureData -- True when some pixel is see-through, even a little. function wsAnyClear pAlpha local tSolid set the caseSensitive to true put numToByte(255) into tSolid repeat while the number of bytes in tSolid < the number of bytes in pAlpha put tSolid after tSolid end repeat return byte 1 to (the number of bytes in pAlpha) of tSolid is not pAlpha end wsAnyClear -- A PNG, JPEG or GIF, decoded by the engine at a size that fits. function wsOtherPicture pData local tImage, tWide, tHigh, tRect, tPicture if not (byte 2 to 4 of pData is "PNG" or byte 1 to 4 of pData is "GIF8" or (byteToNum(byte 1 of pData) is 255 and byteToNum(byte 2 of pData) is 216)) then return empty end if put the long id of image "Loader" of me into tImage lock screen set the lockLoc of tImage to false set the text of tImage to pData put the formattedWidth of tImage into tWide put the formattedHeight of tImage into tHigh if tWide < 1 or tHigh < 1 then set the text of tImage to empty unlock screen return empty end if put wsFitRect(tWide, tHigh, "0,0,560,380") into tRect set the lockLoc of tImage to true set the resizeQuality of tImage to "good" set the rect of tImage to tRect put the width of tImage into tPicture["width"] put the height of tImage into tPicture["height"] put the imageData of tImage into tPicture["imageData"] put the alphaData of tImage into tPicture["alphaData"] set the text of tImage to empty unlock screen return tPicture end wsOtherPicture ------------------------------------------------------------------------ -- Clicks and drags ------------------------------------------------------------------------ on mouseUp local tName put false into sDragging put the short name of the target into tName if word 1 of tName is "Scene" and the number of words in tName is 2 then if word 2 of tName is 5 then put false into sTour end if wsPlay word 2 of tName exit mouseUp end if if word 1 of tName is "Setting" then wsPickSetting word 2 of tName exit mouseUp end if switch tName case "Tour" put not sTour into sTour wsShowTour if sTour and (sScene is empty or sScene is 5) then wsPlay (sLast mod 4) + 1 end if break case "Open a picture" wsOpenAPicture break case "Save WebP" wsSaveWebP break end switch end mouseUp -- In Your turn, the line follows a drag across the picture. on mouseDown if sScene is 5 and the mouseLoc is within sData["rect"] then put true into sDragging wsSplitAt item 1 of the mouseLoc end if pass mouseDown end mouseDown on mouseMove pX, pY if sDragging and sScene is 5 then wsSplitAt pX end if end mouseMove on mouseRelease put false into sDragging end mouseRelease