1
- import numpy as np
2
1
from PIL import Image
3
2
from numpy import binary_repr
4
3
from io import StringIO
8
7
# Credits/sources in ovg_to_png.py
9
8
10
9
filename = "ovg.png"
10
+ outfile = "output.ovg"
11
11
12
12
finalBytes = b""
13
13
@@ -17,16 +17,15 @@ def bitstring_to_bytes(s):
17
17
18
18
19
19
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}"))
22
22
global finalBytes
23
23
finalBytes = finalBytes + bitstring_to_bytes (f"{ cmdBlock } { pixels } " )
24
24
25
25
26
26
# todo: pixels to be a list we can iterate through
27
27
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}")
30
29
# Make it a string we can read like a file
31
30
sio = StringIO (pixels )
32
31
# Figure out our overflow (127, spoilers)
@@ -39,16 +38,16 @@ def construct_bitstream(count, pixels, compressed):
39
38
else :
40
39
bitsOut = sio .read (binary_max * 4 ) # 32bpp RRGGBBAA
41
40
output_bitstream (bitsOut , compressed , cmdBlock )
42
- count = count - binary_max
41
+ count = count - binary_max - 1
43
42
# If any are left after that, output them too
44
43
# fixme: just do this in the loop above?
45
44
if count > 0 :
46
- cmdBlock = str (compressed ) + binary_repr (count , 7 )
45
+ cmdBlock = str (compressed ) + binary_repr (count - 1 , 7 )
47
46
if compressed :
48
47
bitsOut = pixels
49
48
else :
50
49
bitsOut = sio .read (count * 32 ) # 32bpp RRGGBBAA
51
- print (f"Bitsout: { bitsOut } , Count: { count * 32 } " )
50
+ # print(f"Bitsout: {bitsOut}, Count: {count * 32}")
52
51
output_bitstream (bitsOut , compressed , cmdBlock )
53
52
54
53
@@ -58,7 +57,7 @@ def pixels_to_binary(pixel):
58
57
g = pixel [1 ]
59
58
b = pixel [2 ]
60
59
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 :
62
61
r = 255
63
62
g = 255
64
63
b = 255
@@ -81,38 +80,52 @@ def pixels_to_binary(pixel):
81
80
uniqueOutput = ""
82
81
83
82
# 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)
86
86
if pixel == lastPixel :
87
+ # A repeat.
87
88
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 :
95
89
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}")
99
91
construct_bitstream (uniqueCount , uniqueOutput , 0 )
100
- # Reset unique accumulators
101
- uniqueOutput = ""
102
92
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
114
119
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 } " )
117
130
f .write (finalBytes )
118
131
f .close ()
0 commit comments