Skip to content
GitLab
Explore
Sign in
Register
Primary navigation
Search or go to…
Project
F
firmware
Manage
Activity
Members
Labels
Plan
Issues
Issue boards
Milestones
External wiki
Code
Merge requests
Repository
Branches
Commits
Tags
Repository graph
Compare revisions
Snippets
Build
Pipelines
Jobs
Pipeline schedules
Artifacts
Deploy
Releases
Model registry
Monitor
Service Desk
Analyze
Model experiments
Help
Help
Support
GitLab documentation
Compare GitLab plans
Community forum
Contribute to GitLab
Provide feedback
Terms and privacy
Keyboard shortcuts
?
Snippets
Groups
Projects
Show more breadcrumbs
card10
firmware
Commits
2acbd18c
Commit
2acbd18c
authored
4 years ago
by
schneider
Browse files
Options
Downloads
Patches
Plain Diff
change(png): Change from 565+alpha to RGB5551
parent
294627cf
No related branches found
Branches containing commit
No related tags found
Tags containing commit
1 merge request
!457
PNG support
Changes
2
Hide whitespace changes
Inline
Side-by-side
Showing
2 changed files
pycardium/modules/py/png.py
+25
-20
25 additions, 20 deletions
pycardium/modules/py/png.py
pycardium/modules/sys_png.c
+21
-11
21 additions, 11 deletions
pycardium/modules/sys_png.c
with
46 additions
and
31 deletions
pycardium/modules/py/png.py
+
25
−
20
View file @
2acbd18c
import
sys_png
import
color
RGB8
=
0
RGBA8
=
1
RGB565
=
2
RGBA5551
=
3
def
decode
(
png_data
,
format
=
"
RGB
"
,
bg
=
color
.
BLACK
):
"""
...
...
@@ -8,17 +13,17 @@ def decode(png_data, format="RGB", bg=color.BLACK):
:param str format: The intended output format:
- ``RGB``: 24 bit RGB.
- ``RGBA``: 24 bit RGB + 8 bit alpha.
- ``565``: 16 bit RGB. This consumes 1 byte less RAM per pixel than ``RGB``.
- ``
565A
``: 1
6
bit RGB +
8
bit alpha.
- ``
png.
RGB
8
``: 24 bit RGB.
- ``
png.
RGBA
8
``: 24 bit RGB + 8 bit alpha.
- ``
png.RGB
565``: 16 bit RGB. This consumes 1 byte less RAM per pixel than ``
png.
RGB
8
``.
- ``
png.RGBA5551
``: 1
5
bit RGB +
1
bit alpha.
Default is ``RGB``.
Default is ``
png.
RGB
8
``.
:param Color bg: Background color.
If the PNG contains an alpha channel but no alpha
channel is requested in the output (``RGB`` or ``565``)
channel is requested in the output (``
png.
RGB
8
`` or ``
png.RGB
565``)
this color will be used as the background color.
Default is ``Color.BLACK``.
...
...
@@ -27,7 +32,7 @@ def decode(png_data, format="RGB", bg=color.BLACK):
.. versionadded:: 1.17
**Example with RGB data:**
**Example with RGB
A8
data:**
.. code-block:: python
...
...
@@ -36,15 +41,15 @@ def decode(png_data, format="RGB", bg=color.BLACK):
# Draw a PNG file to the display
f = open(
"
example.png
"
)
w, h, img = png.decode(f.read())
w, h, img = png.decode(f.read()
, png.RGBA8
)
f.close()
with display.open() as d:
d.clear()
d.blit(0, 0, w, h, img)
d.blit(0, 0, w, h, img
, display.RGBA8
)
d.update()
**Example with
rgb
565 data:**
**Example with
RGB
565 data:**
.. code-block:: python
...
...
@@ -53,24 +58,24 @@ def decode(png_data, format="RGB", bg=color.BLACK):
# Draw a PNG file to the display
f = open(
"
example.png
"
)
w, h, img = png.decode(f.read(),
"
565
"
)
w, h, img = png.decode(f.read(),
png.RGB
565)
f.close()
with display.open() as d:
d.clear()
d.blit(0, 0, w, h, img, True)
d.blit(0, 0, w, h, img, True
, display.RGB565
)
d.update()
"""
formats
=
(
"
RGB
"
,
"
RGBA
"
,
"
565
"
,
"
565A
"
)
formats
=
(
RGB
8
,
RGBA
8
,
RGB
565
,
RGBA5551
)
if
format
not
in
formats
:
raise
ValueError
(
"
Supported formats:
"
+
"
,
"
.
join
(
formats
)
)
raise
ValueError
(
"
Output format not supported
"
)
if
format
==
"
RGB
"
:
return
sys_png
.
decode
(
png_data
,
0
,
0
,
bg
)
if
format
==
"
RGBA
"
:
return
sys_png
.
decode
(
png_data
,
0
,
1
,
bg
)
if
format
==
"
565
"
:
if
format
==
RGB8
:
return
sys_png
.
decode
(
png_data
,
1
,
0
,
bg
)
if
format
==
"
565A
"
:
if
format
==
RGBA8
:
return
sys_png
.
decode
(
png_data
,
1
,
1
,
bg
)
if
format
==
RGB565
:
return
sys_png
.
decode
(
png_data
,
0
,
0
,
bg
)
if
format
==
RGBA5551
:
return
sys_png
.
decode
(
png_data
,
0
,
1
,
bg
)
This diff is collapsed.
Click to expand it.
pycardium/modules/sys_png.c
+
21
−
11
View file @
2acbd18c
...
...
@@ -36,7 +36,18 @@ static void lode_raise(unsigned int status)
}
}
static
inline
uint16_t
rgb888_to_rgb565
(
uint8_t
*
bytes
)
static
inline
uint16_t
rgba8_to_rgba5551
(
uint8_t
*
bytes
)
{
uint16_t
c
=
((
bytes
[
0
]
&
0
b11111000
)
<<
8
)
|
((
bytes
[
1
]
&
0
b11111000
)
<<
3
)
|
((
bytes
[
2
]
&
0
b11111000
)
>>
2
);
if
(
bytes
[
3
]
>
127
)
{
c
|=
1
;
}
return
c
;
}
static
inline
uint16_t
rgb8_to_rgb565
(
uint8_t
*
bytes
)
{
return
((
bytes
[
0
]
&
0
b11111000
)
<<
8
)
|
((
bytes
[
1
]
&
0
b11111100
)
<<
3
)
|
(
bytes
[
2
]
>>
3
);
...
...
@@ -63,10 +74,10 @@ static mp_obj_t mp_png_decode(size_t n_args, const mp_obj_t *args)
{
mp_buffer_info_t
png_info
;
mp_obj_t
png
=
args
[
0
];
mp_obj_t
rgb
565
_out
=
args
[
1
];
mp_obj_t
alpha_out
=
args
[
2
];
mp_obj_t
bg
=
args
[
3
];
mp_obj_t
png
=
args
[
0
];
mp_obj_t
rgb
8
_out
=
args
[
1
];
mp_obj_t
alpha_out
=
args
[
2
];
mp_obj_t
bg
=
args
[
3
];
/* Load buffer and ensure it contains enough data */
if
(
!
mp_get_buffer
(
png
,
&
png_info
,
MP_BUFFER_READ
))
{
...
...
@@ -128,23 +139,22 @@ static mp_obj_t mp_png_decode(size_t n_args, const mp_obj_t *args)
raw_len
=
w
*
h
*
3
;
}
if
(
mp_obj_is_true
(
rgb
565
_out
))
{
if
(
!
mp_obj_is_true
(
rgb
8
_out
))
{
if
(
mp_obj_is_true
(
alpha_out
))
{
for
(
i
=
0
,
j
=
0
;
i
<
raw_len
;
i
+=
4
,
j
+=
3
)
{
uint16_t
c
=
rgb
88
8_to_rgb
565
(
&
raw
[
i
]);
for
(
i
=
0
,
j
=
0
;
i
<
raw_len
;
i
+=
4
,
j
+=
2
)
{
uint16_t
c
=
rgb
a
8_to_rgb
a5551
(
&
raw
[
i
]);
raw
[
j
]
=
c
&
0xFF
;
raw
[
j
+
1
]
=
c
>>
8
;
raw
[
j
+
2
]
=
raw
[
i
+
3
];
}
raw_len
=
w
*
h
*
3
;
}
else
{
for
(
i
=
0
,
j
=
0
;
i
<
raw_len
;
i
+=
3
,
j
+=
2
)
{
uint16_t
c
=
rgb8
88
_to_rgb565
(
&
raw
[
i
]);
uint16_t
c
=
rgb8_to_rgb565
(
&
raw
[
i
]);
raw
[
j
]
=
c
&
0xFF
;
raw
[
j
+
1
]
=
c
>>
8
;
}
raw_len
=
w
*
h
*
2
;
}
raw_len
=
w
*
h
*
2
;
}
if
(
raw_len
!=
raw_len_original
)
{
...
...
This diff is collapsed.
Click to expand it.
Preview
0%
Loading
Try again
or
attach a new file
.
Cancel
You are about to add
0
people
to the discussion. Proceed with caution.
Finish editing this message first!
Save comment
Cancel
Please
register
or
sign in
to comment