Hi Mike,
That is yet another very clever stack you made. Thank you.
Regarding textSize slider: changing the textSize automatically adjusts also the textHeight.
The following code keeps the textHeight to what it was set to via the scrollbar for density:
Code for scrollbar for textSize:
I was intrigued by the relatively long time it takes to convertImageToText.
It is for me just short of 4 seconds for the "made with livecode" and "indiana jones" images.
I thought this might be something for "StyledText".
I rewrote handler "ImageToAscii" in the card script to use styledText instead of operating on the chars of the field directly.
StyledText is not the most obvious way to build complex text as in your case.
However now I get build times around 120 milliseconds for those two images.
I am not suggesting to use styledText, it is just a showcase for using it.
(You could also use htmlText instead of styledText to get a similar speed increase)
Kind regards
Bernd
That is yet another very clever stack you made. Thank you.
Regarding textSize slider: changing the textSize automatically adjusts also the textHeight.
The following code keeps the textHeight to what it was set to via the scrollbar for density:
Code for scrollbar for textSize:
CODE:
on scrollbarDrag pNewPosition local tTH lock screen put the textheight of field "TextArtField" into tTH set the textSize of field "TextArtField" to round(pNewPosition) set the textHeight of field "TextArtField" to tTHend scrollbarDrag
It is for me just short of 4 seconds for the "made with livecode" and "indiana jones" images.
I thought this might be something for "StyledText".
I rewrote handler "ImageToAscii" in the card script to use styledText instead of operating on the chars of the field directly.
StyledText is not the most obvious way to build complex text as in your case.
However now I get build times around 120 milliseconds for those two images.
I am not suggesting to use styledText, it is just a showcase for using it.
(You could also use htmlText instead of styledText to get a similar speed increase)
CODE:
command ImageToAscii local tImageWidth, tImageHeight local tDisplayChar local tStartTime, tEndTime, tElapsedTime local tImageData, tRed, tGreen, tBlue local x, y local tLineSize, tBytesPerPixel lock screen -- Check if the selected image ID is valid if gSelectedImageID is empty or not (there is an image ID gSelectedImageID) then answer "No image selected. Please double-click an image to select." exit ImageToAscii end if -- Get image dimensions put the width of image ID gSelectedImageID into tImageWidth put the height of image ID gSelectedImageID into tImageHeight -- Check if the image has valid dimensions if not ((tImageWidth <= kImageMaxWidth and tImageHeight <= kImageMaxHeight)) then answer "The maximum image size is 130 x 100 pixels" exit ImageToAscii end if -- Determine the character to use based on the selected radio button if the hilite of button "BlockCharButton" of group "CharSelectionGroup" then put kBlockChar into tDisplayChar else if the hilite of button "DotCharButton" of group "CharSelectionGroup" then put kDotChar into tDisplayChar end if -- Start timing put the milliseconds into tStartTime -- Initialize the output field put empty into field "TextArtField" -- Lock screen updates for performance lock screen; lock messages -- Get the image data put the imageData of image ID gSelectedImageID into tImageData -- Each pixel is represented by 4 bytes in imageData put 4 into tBytesPerPixel -- Calculate line size put tImageWidth * tBytesPerPixel into tLineSize local tBytePosition, tLineNumber, tRunsNum, tStyleA, tCurrRGB, tPrevRGB, tPathToStyle put "style,textColor" into tPathToStyle split tPathToStyle by comma -- Loop through each pixel repeat with y = 0 to tImageHeight - 1 put y + 1 into tLineNumber put 0 into tRunsNum put empty into tPrevRGB repeat with x = 0 to tImageWidth - 1 -- Calculate the byte position in the imageData put (y * tLineSize) + (x * tBytesPerPixel) + 1 into tBytePosition -- Extract RGB values -- Byte 1: Ignored (tBytePosition) -- Byte 2: Red (tBytePosition + 1) -- Byte 3: Green (tBytePosition + 2) -- Byte 4: Blue (tBytePosition + 3) put charToNum(char tBytePosition + 1 of tImageData) into tRed put charToNum(char tBytePosition + 2 of tImageData) into tGreen put charToNum(char tBytePosition + 3 of tImageData) into tBlue put tRed & comma & tGreen & comma & tBlue into tCurrRGB if tCurrRGB <> tPrevRGB then add 1 to tRunsNum put tCurrRGB into tStyleA[tLineNumber]["runs"][tRunsNum][tPathToStyle] put tDisplayChar after tStyleA[tLineNumber]["runs"][tRunsNum]["Text"] put tCurrRGB into tPrevRGB else put tDisplayChar after tStyleA[tLineNumber]["runs"][tRunsNum]["Text"] end if -- -- Set the text color and add character to the output field -- put tDisplayChar after field "TextArtField" -- set the textColor of codeUnit -1 of field "TextArtField" to (tRed & "," & tGreen & "," & tBlue) end repeat --put return after field "TextArtField" end repeat set the styledText of field "TextArtField" to tStyleA -- End timing put the milliseconds into tEndTime put (tEndTime - tStartTime) / 1000 into tElapsedTime -- Display the elapsed time in the TimerField put "conversion time: " & tElapsedTime & " secs" into field "TimerField" -- Unlock screen updates unlock screen; unlock messagesend ImageToAscii
Bernd
Statistics: Posted by bn — Sun Oct 13, 2024 3:32 pm