<commit_before>package main

import (
	"fmt"
	"math/rand"
	"github.com/SudoQ/smArt/data"
	"os"
	"flag"

	//"encoding/base64"
	"image"
	"log"
	//"strings"

	_ "image/gif"
	"image/png"
	_"image/jpeg"
	"time"
	"image/color"
	//"image/draw"
)

func loadImage(filename string) []*data.Data{
	// Decode the JPEG data. If reading from file, create a reader with

	reader, err := os.Open(filename)
	if err != nil {
	    log.Fatal(err)
	}
	defer reader.Close()

	//reader := base64.NewDecoder(base64.StdEncoding, strings.NewReader(data))
	m, _, err := image.Decode(reader)
	if err != nil {
		log.Fatal(err)
	}
	bounds := m.Bounds()

	dataSet := make([]*data.Data, 0)

	// Calculate a 16-bin histogram for m's red, green, blue and alpha components.
	//
	// An image's bounds do not necessarily start at (0, 0), so the two loops start
	// at bounds.Min.Y and bounds.Min.X. Looping over Y first and X second is more
	// likely to result in better memory access patterns than X first and Y second.
	for y := bounds.Min.Y; y < bounds.Max.Y; y++ {
		for x := bounds.Min.X; x < bounds.Max.X; x++ {
			r, g, b, _ := m.At(x, y).RGBA()
			// A color's RGBA method returns values in the range [0, 65535].
			// Shifting by 8 reduces this to the range [0, 255].
			a0 := float64(r>>8)
			a1 := float64(g>>8)
			a2 := float64(b>>8)
			d := data.New([]float64{a0, a1, a2}, 5)
			dataSet = append(dataSet, d)

		}
	}
	// Print the results.
	return dataSet
}

func saveCentroids(centroids []*data.Data, filename string){
	outfile, err := os.Create(filename)
	if err != nil {
		log.Println(err)
		return
	}
	imgRect := image.Rect(0, 0, 100, 100)
	img := image.NewRGBA(imgRect)
	//draw.Draw(img, img.Bounds(), &image.Uniform{color.White}, image.ZP, draw.Src)
	for y := 0; y < 100; y += 1 {
		ci := int((float64(y)/100)*5);
		r := uint8(centroids[ci].Attributes[0])
		g := uint8(centroids[ci].Attributes[1])
		b := uint8(centroids[ci].Attributes[2])
		for x := 0; x < 100; x += 1 {
			img.SetRGBA(x,y,color.RGBA{r,g,b,255})
			//fill := &image.Uniform{color.RGBA{r, g, b, 255}}
			//draw.Draw(img, image.Rect(x, y, x+10, y+10), fill, image.ZP, draw.Src)
		}
	}

	err = png.Encode(outfile, img)
	if err != nil {
		fmt.Println(err)
		os.Exit(1)
	}

	fmt.Printf("Generated image to %s\n",filename)
}

func applyModel(inputFilename, outputFilename string, centroids []*data.Data){
	reader, err := os.Open(inputFilename)
	if err != nil {
	    log.Fatal(err)
	}
	defer reader.Close()

	//reader := base64.NewDecoder(base64.StdEncoding, strings.NewReader(data))
	m, _, err := image.Decode(reader)
	if err != nil {
		log.Fatal(err)
	}
	bounds := m.Bounds()
	writer, err := os.Create(outputFilename)
	if err != nil {
		log.Println(err)
		return
	}
	defer writer.Close()

	imgRect := image.Rect(bounds.Min.X, bounds.Min.Y, bounds.Max.X, bounds.Max.Y)
	img := image.NewRGBA(imgRect)

	// An image's bounds do not necessarily start at (0, 0), so the two loops start
	// at bounds.Min.Y and bounds.Min.X. Looping over Y first and X second is more
	// likely to result in better memory access patterns than X first and Y second.
	for y := bounds.Min.Y; y < bounds.Max.Y; y++ {
		for x := bounds.Min.X; x < bounds.Max.X; x++ {
			r, g, b, _ := m.At(x, y).RGBA()
			// A color's RGBA method returns values in the range [0, 65535].
			// Shifting by 8 reduces this to the range [0, 255].
			a0 := float64(r>>8)
			a1 := float64(g>>8)
			a2 := float64(b>>8)
			d := data.New([]float64{a0, a1, a2}, 5)
			d.UpdateClassification(centroids)
			class := d.Classification
			b0 := uint8(centroids[class].Attributes[0])
			b1 := uint8(centroids[class].Attributes[1])
			b2 := uint8(centroids[class].Attributes[2])
			img.SetRGBA(x,y,color.RGBA{b0, b1, b2, 255})
		}
	}
	err = png.Encode(writer, img)
	if err != nil {
		fmt.Println(err)
		os.Exit(1)
	}

	fmt.Printf("Generated image to %s\n",outputFilename)
}

var inFilename string
var paletteFilename string
var outFilename string
func init() {
	flag.StringVar(&inFilename, "infile", "default_input.png", "Input filename")
	flag.StringVar(&paletteFilename, "palette", "default_palette.png", "Output palette filename")
	flag.StringVar(&outFilename, "outfile", "default_output.png", "Output filename")
}

func main(){
	flag.Parse()
	dataSet := loadImage(inFilename)
	fmt.Println(len(dataSet))
	rand.Seed(time.Now().UTC().UnixNano())

	// Read image

	K := 5
	centroids := []*data.Data{<commit_msg>Updated input flags and cleanup<commit_after>package main

import (
	"fmt"
	"math/rand"
	"github.com/SudoQ/smArt/data"
	"os"
	"flag"

	//"encoding/base64"
	"image"
	"log"
	//"strings"

	_ "image/gif"
	"image/png"
	_"image/jpeg"
	"time"
	"image/color"
	//"image/draw"
)

func loadImage(filename string) []*data.Data{
	// Decode the JPEG data. If reading from file, create a reader with

	reader, err := os.Open(filename)
	if err != nil {
	    log.Fatal(err)
	}
	defer reader.Close()

	m, _, err := image.Decode(reader)
	if err != nil {
		log.Fatal(err)
	}
	bounds := m.Bounds()

	dataSet := make([]*data.Data, 0)

	for y := bounds.Min.Y; y < bounds.Max.Y; y++ {
		for x := bounds.Min.X; x < bounds.Max.X; x++ {
			r, g, b, _ := m.At(x, y).RGBA()
			// A color's RGBA method returns values in the range [0, 65535].
			// Shifting by 8 reduces this to the range [0, 255].
			a0 := float64(r>>8)
			a1 := float64(g>>8)
			a2 := float64(b>>8)
			d := data.New([]float64{a0, a1, a2}, 5)
			dataSet = append(dataSet, d)

		}
	}
	return dataSet
}

func saveCentroids(centroids []*data.Data, filename string){
	outfile, err := os.Create(filename)
	if err != nil {
		log.Println(err)
		return
	}
	imgRect := image.Rect(0, 0, 100, 100)
	img := image.NewRGBA(imgRect)
	for y := 0; y < 100; y += 1 {
		ci := int((float64(y)/100)*5);
		r := uint8(centroids[ci].Attributes[0])
		g := uint8(centroids[ci].Attributes[1])
		b := uint8(centroids[ci].Attributes[2])
		for x := 0; x < 100; x += 1 {
			img.SetRGBA(x,y,color.RGBA{r,g,b,255})
		}
	}

	err = png.Encode(outfile, img)
	if err != nil {
		fmt.Println(err)
		os.Exit(1)
	}

	fmt.Printf("Generated image to %s\n",filename)
}

func applyModel(inputFilename, outputFilename string, centroids []*data.Data){
	reader, err := os.Open(inputFilename)
	if err != nil {
	    log.Fatal(err)
	}
	defer reader.Close()

	m, _, err := image.Decode(reader)
	if err != nil {
		log.Fatal(err)
	}
	bounds := m.Bounds()
	writer, err := os.Create(outputFilename)
	if err != nil {
		log.Println(err)
		return
	}
	defer writer.Close()

	imgRect := image.Rect(bounds.Min.X, bounds.Min.Y, bounds.Max.X, bounds.Max.Y)
	img := image.NewRGBA(imgRect)

	for y := bounds.Min.Y; y < bounds.Max.Y; y++ {
		for x := bounds.Min.X; x < bounds.Max.X; x++ {
			r, g, b, _ := m.At(x, y).RGBA()
			a0 := float64(r>>8)
			a1 := float64(g>>8)
			a2 := float64(b>>8)
			d := data.New([]float64{a0, a1, a2}, 5)
			d.UpdateClassification(centroids)
			class := d.Classification
			b0 := uint8(centroids[class].Attributes[0])
			b1 := uint8(centroids[class].Attributes[1])
			b2 := uint8(centroids[class].Attributes[2])
			img.SetRGBA(x,y,color.RGBA{b0, b1, b2, 255})
		}
	}
	err = png.Encode(writer, img)
	if err != nil {
		fmt.Println(err)
		os.Exit(1)
	}

	fmt.Printf("Generated image to %s\n",outputFilename)
}

var inFilename string
var paletteFilename string
var outFilename string
func init() {
	flag.StringVar(&inFilename, "in", "default_input.png", "Input filename")
	flag.StringVar(&paletteFilename, "pal", "default_palette.png", "Output palette filename")
	flag.StringVar(&outFilename, "out", "default_output.png", "Output filename")
}

func main(){
	flag.Parse()
	dataSet := loadImage(inFilename)
	fmt.Println(len(dataSet))
	rand.Seed(time.Now().UTC().UnixNano())

	// Read image

	K := 5
	centroids := []*data.Data{
{'fraction_non_alphanumeric': 0.1779419388284085, 'fraction_numerical': 0.030326594090202177, 'mean_word_length': 3.560874704491726, 'pattern_counts': {'":': 0, '<': 15, '<?xml version=': 0, '>': 27, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '1282011', 'n_tokens_mistral': 3346, 'n_tokens_neox': 3045, 'n_words': 743}