Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add EAN barcodes. #57

Merged
merged 116 commits into from
Dec 7, 2023
Merged
Show file tree
Hide file tree
Changes from 106 commits
Commits
Show all changes
116 commits
Select commit Hold shift + click to select a range
ecc8997
Refactor to support version2.
Dec 4, 2023
3d06b74
Update gold files for new SDK's better alpha blending
Dec 4, 2023
dddb92e
Move some code from texture.toit to common.toit
Dec 4, 2023
7ea4e41
Assemble all the test outputs in one big png.
Dec 4, 2023
30edbe9
Tighter bounds on partial updates
Dec 4, 2023
6ec2d36
Don't be limited by update width so much
Dec 4, 2023
6830c36
Update gold files
Dec 4, 2023
20e353f
Add the simple elements.
Dec 4, 2023
0b46c59
Remove the textures, now it's Elements only.
Dec 4, 2023
4796157
Add style objects.
Dec 4, 2023
e964f81
Add CSS-style gradients as backgrounds.
Dec 4, 2023
7b6a099
Borders with rounded corners and drop shadows.
Dec 4, 2023
9a6e8d1
PngElement for displaying 1-8 bit PNGs.
Dec 4, 2023
1fc3573
EAN barcodes.
Dec 4, 2023
a5205e8
Remove more references to textures
Dec 4, 2023
9fd7994
Merge branch 'erik-400-remove-textures' into erik-500-styles
Dec 4, 2023
063a4ee
Merge branch 'erik-500-styles' into erik-550-gradients
Dec 4, 2023
978a60f
Merge branch 'erik-550-gradients' into erik-600-rounded-corners
Dec 4, 2023
0a08de7
Merge branch 'erik-600-rounded-corners' into erik-650-png
Dec 4, 2023
70e59c6
Remove barcodes
Dec 4, 2023
c7c5d4b
Add back rudimentary bar code file.
Dec 4, 2023
e54ac0d
Add EAN barcodes.
Dec 4, 2023
4e4c621
Fix command line (non-CI) submodule setup
Dec 5, 2023
b93ed5e
Merge branch 'erik-225-use-pngdiff' into erik-250-tighter-updates
Dec 5, 2023
556a507
Merge branch 'erik-250-tighter-updates' into erik-300-add-elements
Dec 5, 2023
e43d5a6
Merge branch 'erik-300-add-elements' into erik-400-remove-textures
Dec 5, 2023
3e98cb9
Merge branch 'erik-400-remove-textures' into erik-500-styles
Dec 5, 2023
d9bb619
Merge branch 'erik-500-styles' into erik-550-gradients
Dec 5, 2023
2b522f6
Merge branch 'erik-550-gradients' into erik-600-rounded-corners
Dec 5, 2023
cff70cf
Merge branch 'erik-600-rounded-corners' into erik-650-png
Dec 5, 2023
ea47c59
Merge branch 'erik-650-png' into erik-700-barcodes
Dec 5, 2023
0539190
feedback
Dec 5, 2023
84061af
feedback
Dec 5, 2023
b570730
Merge branch 'erik-225-use-pngdiff' into erik-250-tighter-updates
Dec 5, 2023
bc171ac
Merge branch 'erik-250-tighter-updates' into erik-300-add-elements
Dec 5, 2023
e7c64ce
Merge branch 'erik-300-add-elements' into erik-400-remove-textures
Dec 5, 2023
6d9e3b1
Merge branch 'erik-400-remove-textures' into erik-500-styles
Dec 5, 2023
554eda9
Merge branch 'erik-500-styles' into erik-550-gradients
Dec 5, 2023
9266ca7
Merge branch 'erik-550-gradients' into erik-600-rounded-corners
Dec 5, 2023
fa2fce3
Merge branch 'erik-600-rounded-corners' into erik-650-png
Dec 5, 2023
22ea90a
Merge branch 'erik-650-png' into erik-700-barcodes
Dec 5, 2023
528ad59
min_w and min_h no longer needed
Dec 5, 2023
e039294
min_w and min_h no longer needed
Dec 5, 2023
30b64d8
Fix too-narrow patches when we hit the display height
Dec 5, 2023
238cdd8
Merge branch 'erik-250-tighter-updates' into erik-300-add-elements
Dec 5, 2023
3ba1d5b
Merge branch 'erik-300-add-elements' into erik-400-remove-textures
Dec 5, 2023
6fd1f64
Merge branch 'erik-400-remove-textures' into erik-500-styles
Dec 5, 2023
1d03547
Merge branch 'erik-500-styles' into erik-550-gradients
Dec 5, 2023
a655834
Merge branch 'erik-550-gradients' into erik-600-rounded-corners
Dec 5, 2023
858088c
Merge branch 'erik-600-rounded-corners' into erik-650-png
Dec 5, 2023
0d734e2
Merge branch 'erik-650-png' into erik-700-barcodes
Dec 5, 2023
0e203d2
feedback
Dec 5, 2023
b9bba65
Merge branch 'erik-100-version-2' into erik-200-move-to-common
Dec 5, 2023
a3d88f4
Merge branch 'erik-200-move-to-common' into erik-225-use-pngdiff
Dec 5, 2023
fb8110c
Merge branch 'erik-225-use-pngdiff' into erik-250-tighter-updates
Dec 5, 2023
088223f
Merge branch 'erik-250-tighter-updates' into erik-300-add-elements
Dec 5, 2023
746788f
Merge branch 'erik-300-add-elements' into erik-400-remove-textures
Dec 5, 2023
d9df8c9
Merge branch 'erik-400-remove-textures' into erik-500-styles
Dec 5, 2023
69c550c
Merge branch 'erik-500-styles' into erik-550-gradients
Dec 5, 2023
990bf2c
Merge branch 'erik-550-gradients' into erik-600-rounded-corners
Dec 5, 2023
7684d46
Merge branch 'erik-600-rounded-corners' into erik-650-png
Dec 5, 2023
fd496ce
Merge branch 'erik-650-png' into erik-700-barcodes
Dec 5, 2023
ffb5c9c
Merge remote-tracking branch 'origin/main' into erik-100-version-2
Dec 5, 2023
8923389
Merge branch 'erik-100-version-2' into erik-200-move-to-common
Dec 5, 2023
aa00f7d
Merge remote-tracking branch 'origin/main' into erik-200-move-to-common
Dec 5, 2023
b36ee18
Merge branch 'erik-200-move-to-common' into erik-225-use-pngdiff
Dec 5, 2023
027a569
Merge remote-tracking branch 'origin/main' into erik-225-use-pngdiff
Dec 5, 2023
22bf74e
Merge branch 'erik-225-use-pngdiff' into erik-250-tighter-updates
Dec 5, 2023
e7bdcf8
Merge remote-tracking branch 'origin/main' into erik-250-tighter-updates
Dec 5, 2023
186dd59
Merge branch 'erik-250-tighter-updates' into erik-300-add-elements
Dec 5, 2023
1595417
Merge branch 'erik-300-add-elements' into erik-400-remove-textures
Dec 5, 2023
3a29de2
Merge branch 'erik-400-remove-textures' into erik-500-styles
Dec 5, 2023
b526026
Merge branch 'erik-500-styles' into erik-550-gradients
Dec 5, 2023
0d38264
Merge branch 'erik-550-gradients' into erik-600-rounded-corners
Dec 5, 2023
0bb65f4
Merge branch 'erik-600-rounded-corners' into erik-650-png
Dec 5, 2023
45e901b
Merge branch 'erik-650-png' into erik-700-barcodes
Dec 5, 2023
5e12953
feedback
Dec 6, 2023
c8d6417
feedback
Dec 6, 2023
ab38eca
feedback
Dec 6, 2023
ed116f2
Merge remote-tracking branch 'origin/erik-300-add-elements' into erik…
Dec 6, 2023
3b85734
Merge remote-tracking branch 'origin/erik-400-remove-textures' into e…
Dec 6, 2023
dd9e645
Merge remote-tracking branch 'origin/erik-500-styles' into erik-550-g…
Dec 6, 2023
d610c27
Merge remote-tracking branch 'origin/erik-550-gradients' into erik-60…
Dec 6, 2023
f683412
Merge remote-tracking branch 'origin/erik-600-rounded-corners' into e…
Dec 6, 2023
bbd6489
Merge remote-tracking branch 'origin/erik-650-png' into erik-700-barc…
Dec 6, 2023
6f40faa
Return any from get_element_by_id
Dec 6, 2023
baa5cf2
Merge remote-tracking branch 'origin/main' into erik-300-add-elements
Dec 6, 2023
afae253
Merge remote-tracking branch 'origin/erik-300-add-elements' into erik…
Dec 6, 2023
a632094
Merge remote-tracking branch 'origin/main' into erik-400-remove-textures
Dec 6, 2023
7c1b708
Merge remote-tracking branch 'origin/erik-400-remove-textures' into e…
Dec 6, 2023
ee4d578
Merge remote-tracking branch 'origin/main' into erik-500-styles
Dec 6, 2023
78d7be8
Merge branch 'erik-500-styles' into erik-550-gradients
Dec 6, 2023
7de1907
Merge remote-tracking branch 'origin/main' into erik-550-gradients
Dec 6, 2023
8fa1edf
Merge branch 'erik-550-gradients' into erik-600-rounded-corners
Dec 6, 2023
0201a91
Merge remote-tracking branch 'origin/erik-600-rounded-corners' into e…
Dec 6, 2023
ccaff34
Merge remote-tracking branch 'origin/erik-650-png' into erik-700-barc…
Dec 6, 2023
1b16d03
Merge remote-tracking branch 'origin/main' into erik-500-styles
Dec 7, 2023
687905c
Merge branch 'erik-500-styles' into erik-550-gradients
Dec 7, 2023
8be1ad5
Merge branch 'erik-550-gradients' into erik-600-rounded-corners
Dec 7, 2023
c239f43
Merge branch 'erik-600-rounded-corners' into erik-650-png
Dec 7, 2023
590d1b7
Merge branch 'erik-650-png' into erik-700-barcodes
Dec 7, 2023
bf60019
Feedback, some reformatting, 3-component loops
Dec 7, 2023
c69ac99
feedback
Dec 7, 2023
81b87a2
Merge branch 'erik-600-rounded-corners' into erik-700-barcodes
Dec 7, 2023
0e410e7
Merge branch 'erik-600-rounded-corners' into erik-650-png
Dec 7, 2023
a30f18f
Merge branch 'erik-650-png' into erik-700-barcodes
Dec 7, 2023
e9af793
Fix the NoBorder inheritance issue
Dec 7, 2023
77b7b86
Merge remote-tracking branch 'origin/erik-600-rounded-corners' into e…
Dec 7, 2023
45c1abb
Fix wrong refactoring
Dec 7, 2023
3bb7b17
Merge remote-tracking branch 'origin/erik-600-rounded-corners' into e…
Dec 7, 2023
05c3103
Merge remote-tracking branch 'origin/main' into erik-600-rounded-corners
Dec 7, 2023
57b6f6a
Merge branch 'erik-600-rounded-corners' into erik-650-png
Dec 7, 2023
4fdb831
Merge branch 'erik-650-png' into erik-700-barcodes
Dec 7, 2023
4a51825
feedback
Dec 7, 2023
335bc8c
Merge remote-tracking branch 'origin/main' into erik-650-png
Dec 7, 2023
1af0289
Merge branch 'erik-650-png' into erik-700-barcodes
Dec 7, 2023
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
9 changes: 9 additions & 0 deletions package.lock
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
sdk: ^2.0.0-alpha.121
prefixes:
png-tools: toit-png-tools
packages:
toit-png-tools:
url: github.com/toitware/toit-png-tools
name: png-tools
version: 1.0.0
hash: ee472ac9d4333a138206f9d3a817d3c5432d6f87
4 changes: 4 additions & 0 deletions package.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -2,3 +2,7 @@ name: pixel_display
description: Common code to drive all pixel-based displays.
environment:
sdk: ^2.0.0-alpha.121
dependencies:
png-tools:
url: github.com/toitware/toit-png-tools
version: ^1.0.0
113 changes: 113 additions & 0 deletions src/bar_code.toit
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,12 @@
// Use of this source code is governed by an MIT-style license that can be
// found in the LICENSE file.

import font show Font

import .common
import .element show CustomElement
import .style

EAN_13_QUIET_ZONE_WIDTH ::= 9
EAN_13_START_WIDTH ::= 3
EAN_13_MIDDLE_WIDTH ::= 5
Expand All @@ -15,3 +21,110 @@ EAN_13_L_CODES_ ::= [0x0d, 0x19, 0x13, 0x3d, 0x23, 0x31, 0x2f, 0x3b, 0x37, 0x0b]
EAN_13_G_CODES_ ::= [0x27, 0x33, 0x1b, 0x21, 0x1d, 0x39, 0x05, 0x11, 0x09, 0x17]
// Encoding of the first (invisible) digit.
EAN_13_FIRST_CODES_ ::= [0x00, 0x0b, 0x0d, 0x0e, 0x13, 0x19, 0x1c, 0x15, 0x16, 0x1a]

// Element that draws a standard EAN-13 bar code. TODO: Other scales.
class BarCodeEanElement extends CustomElement:
color_/int? := 0
background_ := 0xff
sans10_ ::= Font.get "sans10"
number_height_ := EAN_13_BOTTOM_SPACE

type -> string: return "bar-code-ean"

set_attribute key/string value -> none:
if key == "color":
if color_ != value:
invalidate
color_ = value
else:
super key value

code_ := ? // 13 digit code as a string.
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

?

Suggested change
code_ := ? // 13 digit code as a string.
code_/string := ? // 13 digit code as a string.


code= value/string -> none:
if value != code_: invalidate
code_ = value

code -> string: return code_

/**
$code_: The 13 digit product code.
$x: The left edge of the barcode in the coordinate system of the transform.
$y: The top edge of the barcode in the coordinate system of the transform.
Use $set_styles to set the background to white and the color to black.
*/
constructor .code_/string --x/int?=null --y/int?=null:
// The numbers go below the bar code in a way that depends on the size
// of the digits, so we need to take that into account when calculating
// the bounding box.
number_height_ = (sans10_.text_extent "8")[1]
height := EAN_13_HEIGHT + number_height_ - EAN_13_BOTTOM_SPACE
w := EAN_13_WIDTH
h := height + 1
super --x=x --y=y --w=w --h=h

l_ digit:
return EAN_13_L_CODES_[digit & 0xf]

g_ digit:
return EAN_13_G_CODES_[digit & 0xf]

r_ digit:
return (l_ digit) ^ 0x7f

// Make a white background behind the bar code and draw the digits along the bottom.
draw_background_ canvas/Canvas:
// Bar code coordinates.
text_x := EAN_13_QUIET_ZONE_WIDTH + EAN_13_START_WIDTH
text_y := EAN_13_HEIGHT + number_height_ - EAN_13_BOTTOM_SPACE + 1

canvas.text 1 text_y --text=code_[..1] --color=color_ --font=sans10_

code_[1..7].split "":
if it != "":
canvas.text text_x text_y --text=it --color=color_ --font=sans10_
text_x += EAN_13_DIGIT_WIDTH
text_x += EAN_13_MIDDLE_WIDTH - 1
code_[7..13].split "":
if it != "":
canvas.text text_x text_y --text=it --color=color_ --font=sans10_
text_x += EAN_13_DIGIT_WIDTH
marker_width := (sans10_.text_extent ">")[0]
text_x += EAN_13_START_WIDTH + EAN_13_QUIET_ZONE_WIDTH - marker_width
canvas.text text_x text_y --text=">" --color=color_ --font=sans10_

// Redraw routine.
custom_draw canvas/Canvas:
draw_background_ canvas

x := EAN_13_QUIET_ZONE_WIDTH
long_height := EAN_13_HEIGHT
short_height := EAN_13_HEIGHT - EAN_13_BOTTOM_SPACE
// Start bars: 101.
canvas.rectangle x 0 --w=1 --h=long_height --color=color_
canvas.rectangle x + 2 0 --w=1 --h=long_height --color=color_
x += 3
first_code := EAN_13_FIRST_CODES_[code_[0] & 0xf]
// Left digits using the L or G mapping.
for i := 1; i < 7; i++:
digit := code_[i]
code := ((first_code >> (6 - i)) & 1) == 0 ? (l_ digit) : (g_ digit)
for b := 6; b >= 0; b--:
if ((1 << b) & code) != 0:
canvas.rectangle x 0 --w=1 --h=short_height --color=color_
x++
// Middle bars: 01010
canvas.rectangle x + 1 0 --w=1 --h=long_height --color=color_
canvas.rectangle x + 3 0 --w=1 --h=long_height --color=color_
x += 5
// Left digits using the R mapping.
for i := 7; i < 13; i++:
digit := code_[i]
code := r_ digit
for b := 6; b >= 0; b--:
if ((1 << b) & code) != 0:
canvas.rectangle x 0 --w=1 --h=short_height --color=color_
x++
// End bars: 101.
canvas.rectangle x 0 --w=1 --h=long_height --color=color_
canvas.rectangle x + 2 0 --w=1 --h=long_height --color=color_
16 changes: 16 additions & 0 deletions src/common.toit
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@
import font show Font
import bitmap show ORIENTATION_0 ORIENTATION_90 ORIENTATION_180 ORIENTATION_270

import .element as element
import .element show Element
import .style

Expand Down Expand Up @@ -87,6 +88,21 @@ abstract class Canvas:
abstract text x/int y/int --text/string --color/int --font/Font --orientation/int
abstract text x/int y/int --text/string --color/int --font/Font

abstract bitmap x/int y/int -> none
--pixels/ByteArray
--alpha/ByteArray // 2-element byte array.
--palette/ByteArray // 6-element byte array.
--source_width/int // In pixels.
--source_line_stride/int // In bytes.

abstract bitmap x/int y/int -> none
--pixels/ByteArray
--alpha/ByteArray // 2-element byte array.
--palette/ByteArray // 6-element byte array.
--source_width/int // In pixels.
--source_line_stride/int // In bytes.
--orientation/int

pixmap x/int y/int
--pixels/ByteArray
--alpha/ByteArray=#[]
Expand Down
4 changes: 2 additions & 2 deletions src/element.toit
Original file line number Diff line number Diff line change
Expand Up @@ -43,8 +43,8 @@ abstract class Element extends ElementOrTexture_ implements Window:
if element_class:
if not classes: classes = []
classes.add element_class
background_=background
border_=border
background_ = background
border_ = border
if children: children.do: | child/Element |
child.change_tracker = this

Expand Down
6 changes: 6 additions & 0 deletions src/four_gray.toit
Original file line number Diff line number Diff line change
Expand Up @@ -40,3 +40,9 @@ class Canvas_ extends two_bit.Canvas_:
result := Canvas_ width_ height_
result.transform = transform
return result

static NEAREST_TABLE_ ::= #[BLACK, DARK_GRAY, LIGHT_GRAY, WHITE]

// Convert from a PNG color (0 = black, 255 = white) to a 2-bit 4-gray color.
nearest_color_ palette/ByteArray offset/int -> int:
return NEAREST_TABLE_[palette[offset] >> 6]
65 changes: 63 additions & 2 deletions src/one_byte.toit
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,7 @@ abstract class OneByteCanvas_ extends Canvas:
get_pixel_ x y:
return pixels_[x + width_ * y]

make_alpha_map --padding/int=0 -> Canvas:
make_alpha_map --padding/int=0 -> gray_scale_.Canvas_:
result := gray_scale_.Canvas_ (width_ + padding) (height_ + padding)
result.transform=transform
return result
Expand All @@ -46,6 +46,59 @@ abstract class OneByteCanvas_ extends Canvas:
transform.xyo x y orientation: | x2 y2 o2 |
bytemap_draw_text x2 y2 color o2 text font pixels_ width_

bitmap x/int y/int -> none
--pixels/ByteArray
--alpha/ByteArray // 2-element byte array.
--palette/ByteArray // 4 element byte array.
--source_width/int // In pixels.
--source_line_stride/int // In bytes.
--orientation/int=ORIENTATION_0:
source_byte_width := (source_width + 7) >> 3
zero_alpha := alpha[0]
// Fast case if the alpha is either 0 or 0xff, because we can use the
// primitives that paint 1's with a particular color and leave the zeros
// transparent. We don't check for the case where 0 is opaque and 1 is
// transparent, because pngunzip fixes that for us.
if alpha[1] == 0xff and (zero_alpha == 0xff or zero_alpha == 0):
if zero_alpha == 0xff: // If the zeros in the bitmap are drawn (opaque).
h := (pixels.size + source_line_stride - source_byte_width ) / source_line_stride
// Draw the zeros.
rectangle x y --w=source_width --h=h --color=palette[0]
// Draw the ones.
transform.xyo x y orientation: | x2 y2 o2 |
bitmap_draw_bitmap x2 y2
--color = palette[3]
--orientation = o2
--source = pixels
--source_width = source_width
--source_line_stride = source_line_stride
--destination = pixels_
--destination_width = width_
--bytewise
return
// Unfortunately one of the alpha values is not 0 or 0xff, so we can't use
// the bitmap draw primitive. We can blow it up to bytes, then use the
// bitmap_draw_bytemap.
h := (pixels.size + source_line_stride - source_byte_width ) / source_line_stride
bytemap := ByteArray source_width * h
bitmap_draw_bitmap 0 0
--color = 1
--source = pixels
--source_width = source_width
--source_line_stride = source_line_stride
--destination = bytemap
--destination_width = source_width
--bytewise
transform.xyo x y 0: | x2 y2 o2 |
bitmap_draw_bytemap x2 y2
--alpha = alpha
--orientation = o2
--source = bytemap
--source_width = source_width
--palette = palette
--destination = pixels_
--destination_width = width_

pixmap x/int y/int
--pixels/ByteArray
--alpha/ByteArray=#[]
Expand All @@ -54,4 +107,12 @@ abstract class OneByteCanvas_ extends Canvas:
--orientation/int=ORIENTATION_0
--source_line_stride/int=source_width:
transform.xyo x y orientation: | x2 y2 o2 |
bitmap_draw_bytemap x2 y2 --alpha=alpha --orientation=o2 --source=pixels --source_width=source_width --source_line_stride=source_line_stride --palette=palette --destination=pixels_ --destination_width=width_
bitmap_draw_bytemap x2 y2
--alpha = alpha
--orientation = o2
--source = pixels
--source_width = source_width
--source_line_stride = source_line_stride
--palette = palette
--destination = pixels_
--destination_width = width_
55 changes: 55 additions & 0 deletions src/png.toit
Original file line number Diff line number Diff line change
@@ -0,0 +1,55 @@
// Copyright (C) 2023 Toitware ApS. All rights reserved.
// Use of this source code is governed by an MIT-style license that can be
// found in the LICENSE file.

import .common
import .element

import png_tools.png_reader show *

// Element that draws a PNG image.
class PngElement extends CustomElement:
w/int
h/int
png_/AbstractPng

constructor --x/int?=null --y/int?=null png_file/ByteArray:
info := PngInfo png_file
if info.uncompressed_random_access:
png_ = PngRandomAccess png_file
else:
png_ = Png png_file
if png_.bit_depth > 8: throw "UNSUPPORTED"
if png_.color_type == COLOR_TYPE_TRUECOLOR or png_.color_type == COLOR_TYPE_TRUECOLOR_ALPHA: throw "UNSUPPORTED"
w = png_.width
h = png_.height
super --x=x --y=y

// Redraw routine.
custom_draw canvas/Canvas:
y2 := 0
while y2 < h and (canvas.bounds_analysis 0 y2 w (h - y2)) != Canvas.DISJOINT:
png_.get_indexed_image_data y2 h
--accept_8_bit=canvas.supports_8_bit
--need_gray_palette=canvas.gray_scale: | y_from/int y_to/int bits_per_pixel/int pixels/ByteArray line_stride/int palette/ByteArray alpha_palette/ByteArray |
if bits_per_pixel == 1:
// Last line a little shorter because it has no stride padding.
adjust := line_stride - ((round_up w 8) >> 3)
pixels = pixels[0 .. (y_to - y_from) * line_stride - adjust]
canvas.bitmap 0 y_from
--pixels=pixels
--alpha=alpha_palette
--palette=palette
--source_width=w
--source_line_stride=line_stride
else:
adjust := line_stride - w
pixels = pixels[0 .. (y_to - y_from) * line_stride - adjust]
canvas.pixmap 0 y_from --pixels=pixels
--alpha=alpha_palette
--palette=palette
--source_width=w
--source_line_stride=line_stride
y2 = y_to

type -> string: return "png"
28 changes: 28 additions & 0 deletions src/several_color.toit
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@ import font show Font
import icons show Icon
import .pixel_display show SeveralColorPixelDisplay // For the doc comment.
import .one_byte
import .two_color as two_color

// The canvas contains a ByteArray.
// Initially all pixels have the 0 color.
Expand All @@ -29,3 +30,30 @@ class Canvas_ extends OneByteCanvas_:
result := Canvas_ width_ height_
result.transform=transform
return result

static NO_MIXING_ ::= #[
0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255,
255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255,
255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255,
255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255,
255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255,
255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255,
255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255,
255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255,
]

composit frame_opacity frame_canvas/OneByteCanvas_? painting_opacity painting_canvas/OneByteCanvas_:
fo := frame_opacity is ByteArray ? frame_opacity : frame_opacity.pixels_
po := painting_opacity is ByteArray ? painting_opacity : painting_opacity.pixels_
// We can't mix pixels on a palette-based display so all pixels must be 0 or 0xff.
blit fo fo fo.size --lookup_table=NO_MIXING_
blit po po fo.size --lookup_table=NO_MIXING_
composit_bytes pixels_ fo (frame_canvas ? frame_canvas.pixels_ : null) po painting_canvas.pixels_ false
Loading
Loading