Skip to content

Commit

Permalink
2 new primitives for Bitmap: Create, Copy-To
Browse files Browse the repository at this point in the history
  • Loading branch information
John Chen committed Jan 28, 2024
1 parent 2c961fe commit 4cc3b49
Show file tree
Hide file tree
Showing 2 changed files with 54 additions and 1 deletion.
44 changes: 43 additions & 1 deletion engine/src/main/coffee/extensions/bitmap.coffee
Original file line number Diff line number Diff line change
Expand Up @@ -77,6 +77,46 @@ copyToPColors = (world) -> (image, useNetLogoColors) ->
importPColorsImage(getTopology, getPatchSize, getPatchAt, useNetLogoColors, image)
return

# (Int, Int) => ImageData
create = (width, height) ->
# Create a new ImageData with the specified size
newData = new Uint8ClampedArray(width * height * 4)
new ImageData(newData, width, height)

# (ImageData, ImageData, Int, Int) => Unit
copyTo = (source, target, offsetX, offsetY) ->
checkIsImage(source)
checkIsImage(target)

# Get the original dimensions
oldWidth = source.width
oldHeight = source.height
newWidth = target.width
newHeight = target.height
data = source.data
newData = target.data

# Calculate the offset to place the original image at the center
if typeof offsetX is 'undefined'
offsetX = Math.floor((newWidth - oldWidth) / 2)
if typeof offsetY is 'undefined'
offsetY = Math.floor((newHeight - oldHeight) / 2)

# Copy pixels from the original image to the new image
for y in [0...oldHeight]
for x in [0...oldWidth]
# Calculate the coordinates in the new image
newX = x + offsetX
newY = y + offsetY

# Check if the new coordinates are within the boundaries
if newX >= 0 and newX < newWidth and newY >= 0 and newY < newHeight
# Get the pixel values from the original image
pixelData = getPixel(data, oldWidth, x, y)

# Set the pixel values in the new image
setPixel(newData, newWidth, newX, newY, pixelData)

# (ImageData, ImageData) => ImageData
differenceRgb = (image1, image2) ->
checkIsImage(image1)
Expand Down Expand Up @@ -259,7 +299,7 @@ bilinearScale = (image, width, height) ->
# (ImageData, Int, Int) => ImageData
scaled = (image, width, height) ->
checkIsImage(image)
if image.width = width and image.height = image.height
if image.width is width and image.height is height
image
else if image.width > width and image.height > height
boxScale(image, width, height)
Expand Down Expand Up @@ -293,9 +333,11 @@ bitmapExtension = {
, prims: {
"AVERAGE-COLOR": averageColor
, "CHANNEL": channel
, "COPY-TO": copyTo
, "COPY-TO-DRAWING": copyToDrawing
, "COPY-TO-SHAPE": copyToShape
, "COPY-TO-PCOLORS": copyToPColors(workspace.world)
, "CREATE": create
, "DIFFERENCE-RGB": differenceRgb
, "EXPORT": exportError
, "FROM-BASE64": fromBase64
Expand Down
11 changes: 11 additions & 0 deletions engine/src/main/coffee/extensions/bitmap.json
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,12 @@
, "argTypes": ["wildcard", "number"]
, "returnType": "wildcard"
}
, {
"name": "copy-to"
, "argTypes": ["wildcard", "wildcard", { "type": "number", "isRepeatable": true }]
, "defaultArgCount": 2
, "returnType": "unit"
}
, {
"name": "copy-to-drawing"
, "argTypes": ["wildcard", "number", "number"]
Expand All @@ -27,6 +33,11 @@
, "defaultArgCount": 2
, "returnType": "unit"
}
, {
"name": "create"
, "argTypes": ["number", "number"]
, "returnType": "wildcard"
}
, {
"name": "difference-rgb"
, "argTypes": ["wildcard", "wildcard"]
Expand Down

0 comments on commit 4cc3b49

Please sign in to comment.