|
1 |
| -// This file is part of the CircuitPython project: https://circuitpython.org |
2 |
| -// |
3 |
| -// SPDX-FileCopyrightText: Copyright (c) 2018 Scott Shawcroft for Adafruit Industries |
4 |
| -// |
5 |
| -// SPDX-License-Identifier: MIT |
| 1 | +/* |
| 2 | + * This file is part of the Micro Python project, http://micropython.org/ |
| 3 | + * |
| 4 | + * The MIT License (MIT) |
| 5 | + * |
| 6 | + * Copyright (c) 2018 Scott Shawcroft for Adafruit Industries |
| 7 | + * |
| 8 | + * Permission is hereby granted, free of charge, to any person obtaining a copy |
| 9 | + * of this software and associated documentation files (the "Software"), to deal |
| 10 | + * in the Software without restriction, including without limitation the rights |
| 11 | + * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell |
| 12 | + * copies of the Software, and to permit persons to whom the Software is |
| 13 | + * furnished to do so, subject to the following conditions: |
| 14 | + * |
| 15 | + * The above copyright notice and this permission notice shall be included in |
| 16 | + * all copies or substantial portions of the Software. |
| 17 | + * |
| 18 | + * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR |
| 19 | + * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, |
| 20 | + * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE |
| 21 | + * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER |
| 22 | + * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, |
| 23 | + * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN |
| 24 | + * THE SOFTWARE. |
| 25 | + */ |
6 | 26 |
|
7 | 27 | #include "shared-bindings/displayio/Bitmap.h"
|
8 | 28 |
|
@@ -49,7 +69,7 @@ void common_hal_displayio_bitmap_construct_from_buffer(displayio_bitmap_t *self,
|
49 | 69 | self->x_shift = 0; // Used to divide the index by the number of pixels per word. Its used in a
|
50 | 70 | // shift which effectively divides by 2 ** x_shift.
|
51 | 71 | uint32_t power_of_two = 1;
|
52 |
| - while (power_of_two < ALIGN_BITS / bits_per_value) { |
| 72 | + while (power_of_two < 8 / bits_per_value) { |
53 | 73 | self->x_shift++;
|
54 | 74 | power_of_two <<= 1;
|
55 | 75 | }
|
@@ -90,13 +110,14 @@ uint32_t common_hal_displayio_bitmap_get_pixel(displayio_bitmap_t *self, int16_t
|
90 | 110 | return 0;
|
91 | 111 | }
|
92 | 112 | int32_t row_start = y * self->stride;
|
93 |
| - uint32_t bytes_per_value = self->bits_per_value / 8; |
| 113 | + uint32_t *row = self->data + row_start; |
| 114 | + uint8_t bytes_per_value = self->bits_per_value / 8; |
| 115 | + uint8_t values_per_byte = 8 / self->bits_per_value; |
94 | 116 | if (bytes_per_value < 1) {
|
95 |
| - uint32_t word = self->data[row_start + (x >> self->x_shift)]; |
96 |
| - |
97 |
| - return (word >> (sizeof(uint32_t) * 8 - ((x & self->x_mask) + 1) * self->bits_per_value)) & self->bitmask; |
| 117 | + uint8_t bits = ((uint8_t *)row)[x >> self->x_shift]; |
| 118 | + uint8_t bit_position = (values_per_byte - (x & self->x_mask) - 1) * self->bits_per_value; |
| 119 | + return (bits >> bit_position) & self->bitmask; |
98 | 120 | } else {
|
99 |
| - uint32_t *row = self->data + row_start; |
100 | 121 | if (bytes_per_value == 1) {
|
101 | 122 | return ((uint8_t *)row)[x];
|
102 | 123 | } else if (bytes_per_value == 2) {
|
@@ -134,16 +155,16 @@ void displayio_bitmap_write_pixel(displayio_bitmap_t *self, int16_t x, int16_t y
|
134 | 155 |
|
135 | 156 | // Update one pixel of data
|
136 | 157 | int32_t row_start = y * self->stride;
|
137 |
| - uint32_t bytes_per_value = self->bits_per_value / 8; |
| 158 | + uint32_t *row = self->data + row_start; |
| 159 | + uint8_t bytes_per_value = self->bits_per_value / 8; |
| 160 | + uint8_t values_per_byte = 8 / self->bits_per_value; |
138 | 161 | if (bytes_per_value < 1) {
|
139 |
| - uint32_t bit_position = (sizeof(uint32_t) * 8 - ((x & self->x_mask) + 1) * self->bits_per_value); |
140 |
| - uint32_t index = row_start + (x >> self->x_shift); |
141 |
| - uint32_t word = self->data[index]; |
142 |
| - word &= ~(self->bitmask << bit_position); |
143 |
| - word |= (value & self->bitmask) << bit_position; |
144 |
| - self->data[index] = word; |
| 162 | + uint8_t bits = ((uint8_t *)row)[x >> self->x_shift]; |
| 163 | + uint8_t bit_position = (values_per_byte - (x & self->x_mask) - 1) * self->bits_per_value; |
| 164 | + bits &= ~(self->bitmask << bit_position); |
| 165 | + bits |= (value & self->bitmask) << bit_position; |
| 166 | + ((uint8_t *)row)[x >> self->x_shift] = bits; |
145 | 167 | } else {
|
146 |
| - uint32_t *row = self->data + row_start; |
147 | 168 | if (bytes_per_value == 1) {
|
148 | 169 | ((uint8_t *)row)[x] = value;
|
149 | 170 | } else if (bytes_per_value == 2) {
|
|
0 commit comments