<commit_before>module Graphics.ChalkBoard.CBIR where

import Foreign.Ptr (Ptr)
import Foreign.C.Types (CUChar)
import Data.Word
import Graphics.ChalkBoard.Types (UI,RGB(..),RGBA(..))
import Data.Array.Unboxed
import Data.Array.MArray
import Data.Array.Storable
import Graphics.ChalkBoard.IStorable as IS
import Graphics.ChalkBoard.Core
import Graphics.ChalkBoard.Internals

import Data.Binary
import qualified Data.ByteString as BS
import Data.ByteString (ByteString)
import Control.Monad

{-What do we need?

    * A way of creating a canvas
    * The canvas has size, pixels, depth, RGB/BW, opt alpha, perhaps 1-bit array.
    * We have ways of placing slices (rectangles?) of these canvases onto the 'screen'. 

        Difference places, sizes, rotations.

    * Ways drawing these to a canvas
    * Ways of importing and exporting canvases 
-}


type BufferId       = Int		     -- make abstact later
type StreamId       = Int
type FragFunctionId = Int


data Depth = BitDepth		-- 1 bit per pixel
           | G8BitDepth        -- 8 bits per pixel (grey)
           | RGB24Depth	        -- (R,G,B), 8 bits per pixel
           | RGBADepth		-- (R,G,B,A), 8 bits per pixel
        deriving (Show, Eq)

-- In a sense, the Depth is the type of our CBBO.

-- KM: I would say leave as 0-1 since that's what OpenGL defaults to anyway for colors. Plus then if you want to use it for something else like you did with PointMap?, you can.

data Background
           = BackgroundBit Bool
{-
AG: does a pixel mean later 'draw this *color* (white/black), or draw this pixel *if* black?

KM: I would say the "draw this pixel if true" approach might be faster? Then could use the

    8Bit version if you want to force it to draw all pixels?
-}
           | BackgroundG8Bit UI
              -- this may have the same issue?
           | BackgroundRGB24Depth RGB
           | BackgroundRGBADepth RGBA
	   | BackgroundByteString ByteString 

instance Show Background where
	show (BackgroundBit b) = "(BackgroundBit $ " ++ show b ++ ")"
	show (BackgroundG8Bit g) = "(BackgroundG8Bit $ " ++ show g ++ ")"
	show (BackgroundRGB24Depth c) = "(BackgroundRGB24Depth $ " ++ show c ++ ")"
	show (BackgroundRGBADepth c) = "(BackgroundRGBADepth $ " ++ show c ++ ")"
	show (BackgroundByteString bs) = "(BackgroundByteString ...)"
	
        

-- type RGBA = (UI,UI,UI,UI)

type UIPoint = (UI,UI)


-- A mapping from a point on the source CBBO to a corresponding point on the canvas CBBO.
data PointMap = PointMap UIPoint UIPoint
        deriving Show

-- Telling CopyBoard whether to use the source alpha or the destination alpha (use source for a /complete/ copy)
data WithAlpha = WithSrcAlpha
               | WithDestAlpha
        deriving Show


-- AG: The depth is determined by the Background, we only need one!

-- We now use Inst var, but use Inst CBBO in most cases.
data Inst var
     = Allocate 
        var             -- tag for this ChalkBoardBufferObject
        (Int,Int)       -- size of ChalkBoardBufferObject
        Depth           -- depth of buffer
        Background      -- what to draw at allocation

       -- ^ This means allocate a buffer, n * m pixels, with a specific depth, and a background (default) color.
{-
AG: other considerations include

    * Do we project into it using 0..1 x 0..1? (say yes for now) 

        (KM: Sounds fine as a first approach. Could support both exact position and percentage (0-1) eventually.)

    * Does it loop round, when viewed? 

        (KM: I would say not when printed onto the screen, at least. Maybe internally for 'from' CBBOs doing splats, but the 'to' board probably won't want it to wrap, just be off the edge.)
-}
       --- Everything can be written as triangles (for now)!
     | SplatTriangle
       var	--  to write from (may be a color, but will be allocated)
       var	--  to write to
       PointMap
       PointMap
       PointMap
     
     | SplatPolygon
       var
       var
       [PointMap]
     
     | SplatColor
       RGBA --Should probably make a color type or something?
       var
       Bool		-- do you do alpha blending (True), or just copy bits (False) (TODO: consider removing)
       [UIPoint]

     | SplatWithFunction
       var			-- FragFunId
       [(String,var)]		-- argument BufferId(s)
       [(String,UniformArgument)]		-- the extra uniform args
       var			-- target BufferId
       [PointMap]
{-
     | SplatWholeBoardColor
	RGBA
	var
-}

     | SplatBuffer
       var		-- src
       var		-- dest

     | CopyBuffer
       WithAlpha
       var		-- src
       var		-- dest
     
     | AllocateImage
       var
       FilePath
     
     | SaveImage
       var
       FilePath
       
     | OpenStream
       StreamId
       String
     
     | WriteStream
       BufferId
       StreamId
     
     | CloseStream
       StreamId
       
     | Delete
       var
       
--     | ScaleAlpha var UI	-- LATER


    | Nested String [Inst var]

    | Exit


	-- GLSL extensions to CBIR

    | AllocFragmentShader var String [String]	-- GLSL object, and names of args

    | ShadeFragmentWith var [(String,var)] [Inst var]	-- Use a specific object, in the context of the given framement function

    | DeleteFragmentShader var

        deriving Show


copyBoard :: var -> var -> Inst var
copyBoard src target = CopyBuffer WithSrcAlpha src target

colorBoard :: RGB -> var -> Inst var
colorBoard (RGB r g b) target = SplatColor (RGBA r g b 1) target False [ p | p <- [(0,0),(0,1),(1,1),(1,0)]]<commit_msg>Adding Blend into CBIR. Still to be used inside CBIR.<commit_after>module Graphics.ChalkBoard.CBIR where

import Foreign.Ptr (Ptr)
import Foreign.C.Types (CUChar)
import Data.Word
import Graphics.ChalkBoard.Types (UI,RGB(..),RGBA(..))
import Data.Array.Unboxed
import Data.Array.MArray
import Data.Array.Storable
import Graphics.ChalkBoard.IStorable as IS
import Graphics.ChalkBoard.Core
import Graphics.ChalkBoard.Internals

import Data.Binary
import qualified Data.ByteString as BS
import Data.ByteString (ByteString)
import Control.Monad

{-What do we need?

    * A way of creating a canvas
    * The canvas has size, pixels, depth, RGB/BW, opt alpha, perhaps 1-bit array.
    * We have ways of placing slices (rectangles?) of these canvases onto the 'screen'. 

        Difference places, sizes, rotations.

    * Ways drawing these to a canvas
    * Ways of importing and exporting canvases 
-}


type BufferId       = Int		     -- make abstact later
type StreamId       = Int
type FragFunctionId = Int


data Depth = BitDepth		-- 1 bit per pixel
           | G8BitDepth        -- 8 bits per pixel (grey)
           | RGB24Depth	        -- (R,G,B), 8 bits per pixel
           | RGBADepth		-- (R,G,B,A), 8 bits per pixel
        deriving (Show, Eq)

-- In a sense, the Depth is the type of our CBBO.

-- KM: I would say leave as 0-1 since that's what OpenGL defaults to anyway for colors. Plus then if you want to use it for something else like you did with PointMap?, you can.

data Background
           = BackgroundBit Bool
{-
AG: does a pixel mean later 'draw this *color* (white/black), or draw this pixel *if* black?

KM: I would say the "draw this pixel if true" approach might be faster? Then could use the

    8Bit version if you want to force it to draw all pixels?
-}
           | BackgroundG8Bit UI
              -- this may have the same issue?
           | BackgroundRGB24Depth RGB
           | BackgroundRGBADepth RGBA
	   | BackgroundByteString ByteString 

instance Show Background where
	show (BackgroundBit b) = "(BackgroundBit $ " ++ show b ++ ")"
	show (BackgroundG8Bit g) = "(BackgroundG8Bit $ " ++ show g ++ ")"
	show (BackgroundRGB24Depth c) = "(BackgroundRGB24Depth $ " ++ show c ++ ")"
	show (BackgroundRGBADepth c) = "(BackgroundRGBADepth $ " ++ show c ++ ")"
	show (BackgroundByteString bs) = "(BackgroundByteString ...)"
	
        

-- type RGBA = (UI,UI,UI,UI)

type UIPoint = (UI,UI)

-- A mapping from a point on the source CBBO to a corresponding point on the canvas CBBO.
data PointMap = PointMap UIPoint UIPoint
        deriving Show

-- Telling CopyBoard whether to use the source alpha or the destination alpha (use source for a /complete/ copy)
data WithAlpha = WithSrcAlpha
               | WithDestAlpha
        deriving Show

-- Move into CBIR
data Write = Blend | Copy deriving (Eq, Show)

-- AG: The depth is determined by the Background, we only need one!

-- We now use Inst var, but use Inst CBBO in most cases.
data Inst var
     = Allocate 
        var             -- tag for this ChalkBoardBufferObject
        (Int,Int)       -- size of ChalkBoardBufferObject
        Depth           -- depth of buffer
        Background      -- what to draw at allocation

       -- ^ This means allocate a buffer, n * m pixels, with a specific depth, and a background (default) color.
{-
AG: other considerations include

    * Do we project into it using 0..1 x 0..1? (say yes for now) 

        (KM: Sounds fine as a first approach. Could support both exact position and percentage (0-1) eventually.)

    * Does it loop round, when viewed? 

        (KM: I would say not when printed onto the screen, at least. Maybe internally for 'from' CBBOs doing splats, but the 'to' board probably won't want it to wrap, just be off the edge.)
-}
       --- Everything can be written as triangles (for now)!
     | SplatTriangle
       var	--  to write from (may be a color, but will be allocated)
       var	--  to write to
       PointMap
       PointMap
       PointMap
     
     | SplatPolygon
       var
       var
       [PointMap]
     
     | SplatColor
       RGBA --Should probably make a color type or something?
       var
       Bool		-- do you do alpha blending (True), or just copy bits (False) (TODO: consider removing)
       [UIPoint]

     | SplatWithFunction
       var			-- FragFunId
       [(String,var)]		-- argument BufferId(s)
       [(String,UniformArgument)]		-- the extra uniform args
       var			-- target BufferId
       [PointMap]
{-
     | SplatWholeBoardColor
	RGBA
	var
-}

     | SplatBuffer
       var		-- src
       var		-- dest

     | CopyBuffer
       WithAlpha
       var		-- src
       var		-- dest
     
     | AllocateImage
       var
       FilePath
     
     | SaveImage
       var
       FilePath
       
     | OpenStream
       StreamId
       String
     
     | WriteStream
       BufferId
       StreamId
     
     | CloseStream
       StreamId
       
     | Delete
       var
       
--     | ScaleAlpha var UI	-- LATER


    | Nested String [Inst var]

    | Exit

	-- GLSL extensions to CBIR

    | AllocFragmentShader var String [String]	-- GLSL object, and names of args

    | ShadeFragmentWith var [(String,var)] [Inst var]	-- Use a specific object, in the context of the given framement function

    | DeleteFragmentShader var		-- TODO: write code for, and use

        deriving Show


copyBoard :: var -> var -> Inst var
copyBoard src target = CopyBuffer WithSrcAlpha src target

colorBoard :: RGB -> var -> Inst var
colorBoard (RGB r g b) target = SplatColor (RGBA r g b 1) target False [ p | p <- [(0,0),(0,1),(1,1),(1,0)]]
{'fraction_non_alphanumeric': 0.08211908616657672, 'fraction_numerical': 0.0062960964202194636, 'mean_word_length': 2.271256251838776, 'pattern_counts': {'":': 0, '<': 5, '<?xml version=': 0, '>': 11, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '2331141', 'n_tokens_mistral': 3380, 'n_tokens_neox': 3168, 'n_words': 1314}