
# Copyright 2019 Rich Altmaier  richalt2@yahoo.com
#
#
#   This program is free software: you can redistribute it and/or modify
#   it under the terms of the GNU General Public License as published by
#   the Free Software Foundation, either version 3 of the License, or
#   (at your option) any later version.
#
#   This program is distributed in the hope that it will be useful,
#   but WITHOUT ANY WARRANTY; without even the implied warranty of
#   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
#   GNU General Public License for more details.
#
#   You should have received a copy of the GNU General Public License
#   along with this program.  If not, see <http://www.gnu.org/licenses/>.
#
import sys
import png
import numpy
import math

if len(sys.argv) != 2:
    print("require filename as argument")
    exit()

PNGFilename = sys.argv[1]

#r = png.Reader(filename='200x225-dad.png')
r = png.Reader(filename=PNGFilename)

r.validate_signature()

#x,y,pixels,info = r.asRGBA8()
x,y,pixels,info = r.asDirect()

print ( "//   x", x, "y", y, "pixels", pixels, "info", info)
if info['alpha'] == True:
    datums = 4
    print ("//  Alpha value 0..255 is 4th datum in RGB set")
else:
    datums = 3
    print ("// no Alpha values")

if info['greyscale'] == True:
    print("no decoding of true greyscale PNG format.  Exit.")
    exit()
if info['bitdepth'] != 8:
    print("only understand bitdepth of 8!  Exit.")
    exit()

#
# Output openscad array, with one row per pixel row (so there will be y such rows),
# and each row is a series of rgb color values in a sub list,
# [
#  [ pixel row of format [r,g,b], [r,g,b], [r,g,b], ...  x lists


print("//")
print(PNGFilename.replace(".", "").replace("-", "_").replace("png", ""),  " = [")  
firstrow = True
for row in pixels:
    #print("//  row", row)

    # extract the datums from the iterator and stuff into an array.
    # the array allows the indexing needed to calculate!
    firstcol = True
    L = []
    for items in row:
        L = L + [items]

    # Now we have array L !

    #print("//  Llen", len(L), "alpha", info['alpha'], "datums", datums, "greyscale", info['greyscale'])

    if firstrow == True:
        firstrow = False
    else:
        print(",") 

    print(" [", end='') # row begins
    firstcol = True
    for pix in range(0, (datums*x)-1, datums):
        #iterate by pixel, which is a variable (datums) number of values.

        #converting RGB including Alpha if present
        if info['alpha'] == True:
            AlphaIntensity  =  L[pix+3] / 255.0
        else:
            AlphaIntensity = 1.0

        # print("r", L[pix], "g", L[pix+1], "b", L[pix+2], "grey", grey, "greyA", greyA, end=' ')
        if firstcol is False: 
            print(',', end='')
        else:
            firstcol = False

        #print("[", L[pix], ",", L[pix+1], ",", L[pix+2],  end='')
        print("[", math.floor(AlphaIntensity * L[pix]), ",", math.floor(AlphaIntensity * L[pix+1]), ",", math.floor(AlphaIntensity*L[pix+2]),  end='')
        if info['alpha'] == True:
            print(",", AlphaIntensity, end='')
        print("]", end=' ')

    print(" ]", end='')

print("")
print("  ];")

