Skip to content

Commit bc04f42

Browse files
committed
working png to ovg
1 parent 47a29bf commit bc04f42

File tree

3 files changed

+52
-39
lines changed

3 files changed

+52
-39
lines changed

ovg.png

-80.6 KB
Loading

ovg_to_png.py

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -10,8 +10,8 @@
1010
# Describes command block and RLE routine
1111

1212
# variables; adjust me
13-
filename = "example_bins/ops_ovg.bin"
14-
width = 172
13+
filename = "example_bins/mex_ovg.bin"
14+
width = 39
1515
# height automatically calculated
1616

1717
# First, deal with RLE compression as defined by the NXP PDF above
@@ -36,12 +36,12 @@
3636
gPixel = ord(file.read(1))
3737
bPixel = ord(file.read(1))
3838
aPixel = ord(file.read(1))
39+
# print(f"{pixels} pixels of [{rPixel}, {gPixel}, {bPixel}, {aPixel}] added to the array.")
3940
for x in range(pixels):
4041
bytesOut.append(rPixel)
4142
bytesOut.append(gPixel)
4243
bytesOut.append(bPixel)
4344
bytesOut.append(aPixel)
44-
# print(f"{pixels} pixels of [{rPixel}, {gPixel}, {bPixel}, {aPixel}] added to the array.")
4545
# Read next command block
4646
cmd = file.read(1)
4747
else:

png_to_ovg.py

Lines changed: 49 additions & 36 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,3 @@
1-
import numpy as np
21
from PIL import Image
32
from numpy import binary_repr
43
from io import StringIO
@@ -8,6 +7,7 @@
87
# Credits/sources in ovg_to_png.py
98

109
filename = "ovg.png"
10+
outfile = "output.ovg"
1111

1212
finalBytes = b""
1313

@@ -17,16 +17,15 @@ def bitstring_to_bytes(s):
1717

1818

1919
def output_bitstream(pixels, compressed, cmdBlock):
20-
print(f"Bitstream {compressed}: {cmdBlock} // {pixels}")
21-
print(bitstring_to_bytes(f"{cmdBlock}{pixels}"))
20+
# print(f"Bitstream {compressed}: {cmdBlock} // {pixels}")
21+
# print(bitstring_to_bytes(f"{cmdBlock}{pixels}"))
2222
global finalBytes
2323
finalBytes = finalBytes + bitstring_to_bytes(f"{cmdBlock}{pixels}")
2424

2525

2626
# todo: pixels to be a list we can iterate through
2727
def construct_bitstream(count, pixels, compressed):
28-
count = count - 1 # pdf spec fix
29-
print(f"Pixel input: {count}, {pixels}, {compressed}")
28+
# print(f"Pixel input: {count}, {pixels}, {compressed}")
3029
# Make it a string we can read like a file
3130
sio = StringIO(pixels)
3231
# Figure out our overflow (127, spoilers)
@@ -39,16 +38,16 @@ def construct_bitstream(count, pixels, compressed):
3938
else:
4039
bitsOut = sio.read(binary_max * 4) # 32bpp RRGGBBAA
4140
output_bitstream(bitsOut, compressed, cmdBlock)
42-
count = count - binary_max
41+
count = count - binary_max - 1
4342
# If any are left after that, output them too
4443
# fixme: just do this in the loop above?
4544
if count > 0:
46-
cmdBlock = str(compressed) + binary_repr(count, 7)
45+
cmdBlock = str(compressed) + binary_repr(count - 1, 7)
4746
if compressed:
4847
bitsOut = pixels
4948
else:
5049
bitsOut = sio.read(count * 32) # 32bpp RRGGBBAA
51-
print(f"Bitsout: {bitsOut}, Count: {count * 32}")
50+
# print(f"Bitsout: {bitsOut}, Count: {count * 32}")
5251
output_bitstream(bitsOut, compressed, cmdBlock)
5352

5453

@@ -58,7 +57,7 @@ def pixels_to_binary(pixel):
5857
g = pixel[1]
5958
b = pixel[2]
6059
a = pixel[3]
61-
if r == 0 & g == 0 & b == 0 & a == 0:
60+
if r == 0 and g == 0 and b == 0 and a == 0:
6261
r = 255
6362
g = 255
6463
b = 255
@@ -81,38 +80,52 @@ def pixels_to_binary(pixel):
8180
uniqueOutput = ""
8281

8382
# Loop over every pixel in the loaded image
84-
for pixel in pixels:
85-
# Pixel was the same. Add to the tally.
83+
for pixelIndex in range(len(pixels)):
84+
pixel = pixels[pixelIndex]
85+
# print(pixel)
8686
if pixel == lastPixel:
87+
# A repeat.
8788
repeatCount = repeatCount + 1
88-
continue
89-
# A new pixel colour!
90-
if repeatCount == 0:
91-
# First pixel of the image; record it
92-
lastPixel = pixel
93-
continue
94-
if repeatCount > 2:
9589
if uniqueCount > 0:
96-
# We just ended a run of unique pixels.
97-
print(f"> {uniqueCount}\t unique pixels ready - needs pixel count but no RLE repeat flag")
98-
# Construct output
90+
# print(f"ended a run of uniques. there were {uniqueCount} unique pixels: {uniqueOutput}")
9991
construct_bitstream(uniqueCount, uniqueOutput, 0)
100-
# Reset unique accumulators
101-
uniqueOutput = ""
10292
uniqueCount = 0
103-
continue
104-
# We just ended a run of repeated pixels
105-
print(f"> {repeatCount}\t repeat pixels of {pixel} ready - needs pixel count and RLE repeat flag")
106-
# Construct output
107-
construct_bitstream(repeatCount, pixels_to_binary(pixel), 1)
108-
repeatCount = 0
109-
continue
110-
# This is a unique pixel; start a run of uniques
111-
uniqueCount = uniqueCount + 1
112-
# Concatenate and construct output
113-
uniqueOutput = f"{uniqueOutput}{pixels_to_binary(pixel)}"
93+
else:
94+
# A new pixel.
95+
if pixelIndex > 0:
96+
uniqueCount = uniqueCount + 1
97+
nextUnique = pixels[pixelIndex - 1]
98+
# print(f"unique added: {pixelIndex} {nextUnique}")
99+
uniqueOutput = f"{uniqueOutput}{pixels_to_binary(nextUnique)}"
100+
# Now, what to do with that information. Were we previously on a roll?
101+
if repeatCount > 0:
102+
repeatCount = repeatCount + 1
103+
# print(f"ended a run of repeats. there were {repeatCount} repeated pixels of {lastPixel} - {pixels_to_binary(lastPixel)}.")
104+
construct_bitstream(repeatCount, pixels_to_binary(lastPixel), 1)
105+
# end run
106+
repeatCount = 0
107+
# was not unique
108+
uniqueCount = 0
109+
uniqueOutput = ""
110+
lastPixel = pixel
111+
112+
# loop ended. check accumulators
113+
# fixme: dry
114+
if repeatCount > 0:
115+
repeatCount = repeatCount + 1
116+
# print(f"ended a run of repeats. there were {repeatCount} repeated pixels of {lastPixel} - {pixels_to_binary(lastPixel)}.")
117+
construct_bitstream(repeatCount, pixels_to_binary(lastPixel), 1)
118+
# end run
114119
repeatCount = 0
115-
116-
f = open("output.ovg", "wb")
120+
# was not unique
121+
uniqueCount = 0
122+
uniqueOutput = ""
123+
if uniqueCount > 0:
124+
# print(f"ended a run of uniques. there were {uniqueCount} unique pixels: {uniqueOutput}")
125+
construct_bitstream(uniqueCount, uniqueOutput, 0)
126+
uniqueCount = 0
127+
128+
f = open(outfile, "wb")
129+
print(f"saved as {outfile}")
117130
f.write(finalBytes)
118131
f.close()

0 commit comments

Comments
 (0)