<commit_before>package core

import (
	"fmt"
	"image"
	"image/color"
	"image/png"
	"io/ioutil"
	"math"
	"os"
	"time"
)

// The Pxl struct definition
type Pxl struct {
	IsEncodeMode   bool
	IsDecodeMode   bool
	IsDebugMode    bool
	Source         string
	Target         string
	encodedPayload image.Image
	decodedPayload []byte
}

// Checks the context on the Pxl struct
// Encode the Source if Pxl.IsEncodeMode
// Decode the Source if Pxl.IsDecodeMode
func (p Pxl) Process() (bool, error) {

	if p.IsEncodeMode {
		if err := p.Encode(); err != nil {
			return false, err
		}
		p.DebugString()

		f, _ := os.OpenFile(p.Target, os.O_WRONLY|os.O_CREATE, 0600)
		defer f.Close()
		//******************************************
		start := time.Now()
		//==========================================
		png.Encode(f, p.encodedPayload)
		//******************************************
		elapsed := time.Since(start)
		fmt.Printf("encoding to png: %s\n", elapsed)
		//==========================================
	}

	if p.IsDecodeMode {
		if err := p.Decode(); err != nil {
			return false, err
		}

		err := ioutil.WriteFile(p.Target, p.decodedPayload, 0644)
		if err != nil {
			return false, err
		}
	}

	DisplayDebug(p.DebugString("DONE"))

	return true, nil
}

// Encodes the Pxl.Source and stores it to Pxl.encodedPayload
func (p *Pxl) Encode() error {

	msg, err := ioutil.ReadFile(p.Source)
	if err != nil {
		return err
	}

	dimensions := int(math.Sqrt(float64(len(msg)/4))) + 1

	var pxl [][][]byte

	var line [][]byte
	var pixel []byte

	//******************************************
	start := time.Now()
	//==========================================

	//loop msg bytes
	for i, c := range msg {
		if i > 0 && (i%4) == 0 {
			line = append(line, pixel)
			pixel = nil
		}
		pixel = append(pixel, c)

		if len(line) > 0 && (len(line)%dimensions) == 0 {
			pxl = append(pxl, line)
			line = nil
		}
	}

	//calculate n missing values in incomplete color
	if len(pixel) < 4 {
		missing := 4 - len(pixel)
		for i := 0; i < missing; i++ {
			pixel = append(pixel, 255)
		}
	}

	//append incomplete color to line
	line = append(line, pixel)

	//calculate n missing values in line
	if len(line) < dimensions {
		missing := (dimensions - len(line))
		missingColor := []byte{0, 0, 0, 255}
		//add n missing colors to line
		for i := 0; i < missing; i++ {
			line = append(line, missingColor)
		}
	}
	pxl = append(pxl, line)

	//******************************************
	elapsed := time.Since(start)
	fmt.Printf("generating pixel: %s\n", elapsed)
	//==========================================

	//create image with dimensions x dimensions
	img := image.NewNRGBA((image.Rect(0, 0, dimensions, dimensions)))

	//******************************************
	start2 := time.Now()
	//==========================================
	x := 0
	y := 0
	for i, line := range pxl {
		x = 0
		if i > 0 {
			y++
		}

		//each pixel in line
		for _, pixel := range line {
			img.Set(x, y, color.NRGBA{pixel[0], pixel[1], pixel[2], pixel[3]})
			x++
		}
	}

	//******************************************
	elapsed2 := time.Since(start2)
	fmt.Printf("generating image: %s\n", elapsed2)
	//==========================================

	p.encodedPayload = img

	return nil

}

// Decoded the Pxl.Source and stores it to Pxl.decodedPayload
func (p *Pxl) Decode() error {
	img, err := loadImage(p.Source)
	if err != nil {<commit_msg>initial refactoring<commit_after>package core

import (
	"errors"
	"fmt"
	"image"
	"image/color"
	"image/png"
	"io/ioutil"
	"math"
	"os"
	"time"
)

// The Pxl struct definition
type Pxl struct {
	IsEncodeMode   bool
	IsDecodeMode   bool
	IsDebugMode    bool
	Source         string
	Target         string
	encodedPayload image.Image
	decodedPayload []byte
}

// Checks the context on the Pxl struct
// Encode the Source if Pxl.IsEncodeMode
// Decode the Source if Pxl.IsDecodeMode
func (p Pxl) Process() (bool, error) {

	if p.IsEncodeMode {
		if err := p.Encode(); err != nil {
			return false, err
		}
		p.DebugString()

		f, err := os.OpenFile(p.Target, os.O_WRONLY|os.O_CREATE, 0600)

		if err != nil {
			panic(err)
		}

		defer f.Close()
		//******************************************
		start := time.Now()
		//==========================================
		png.Encode(f, p.encodedPayload)
		//******************************************
		elapsed := time.Since(start)
		fmt.Printf("encoding to png: %s\n", elapsed)
		//==========================================
	}

	if p.IsDecodeMode {
		if err := p.Decode(); err != nil {
			return false, err
		}

		err := ioutil.WriteFile(p.Target, p.decodedPayload, 0644)
		if err != nil {
			return false, err
		}
	}

	DisplayDebug(p.DebugString("DONE"))

	return true, nil
}

// Encodes the Pxl.Source and stores it to Pxl.encodedPayload
func (p *Pxl) Encode() error {

	//msg, err := ioutil.ReadFile(p.Source)
	//if err != nil {
	//	return err
	//}
	//
	f, err := os.OpenFile(p.Source, os.O_RDONLY, 0444)
	defer f.Close()

	finfo, err := os.Stat(p.Source)

	if err != nil {
		return err
	}

	dimensions := int(math.Sqrt(float64(finfo.Size()/4))) + 1

	//var pxl [][][]byte

	//var line [][]byte
	//var pixel []byte

	//******************************************
	start := time.Now()
	//==========================================

	x := 0
	y := 0

	bytesWritten := 0

	//create image with dimensions x dimensions
	img := image.NewNRGBA((image.Rect(0, 0, dimensions, dimensions)))

	//fillPx := color.NRGBA{0, 0, 0, 255}

	//draw.Draw(img, img.Bounds(), &image.Uniform{fillPx}, image.ZP, draw.Src)

	//******************************************
	elapsed := time.Since(start)
	fmt.Printf("generating pixel: %s\n", elapsed)
	//==========================================

	//******************************************
	start2 := time.Now()
	//==========================================

	var buffer = make([]byte, 838860800)
	tmp := make([]byte, 4)

	for {
		num, err := f.Read(buffer)

		fmt.Println(num, err)

		if err != errors.New("EOF") && num == 0 {
			break
		} else if num == 0 {
			break
		} else if err != nil {
			return err
		}

		bytesWritten += num

		//loop msg bytes
		for pos := 0; pos < num; pos += 4 {
			for p := 0; p < 4; p++ {
				if len(buffer) < pos+p {
					tmp[p] = byte(255)
				} else {
					tmp[p] = buffer[pos+p]
				}
			}

			img.Set(x, y, color.NRGBA{tmp[pos%4], tmp[pos%4+1], tmp[pos%4+2], tmp[pos%4+3]})

			x++
			if x >= dimensions {
				y++
				x = 0
			}
		}
	}

	for posY := y; posY < dimensions; posY++ {
		for posX := x; posX < dimensions; posX++ {
			img.Set(posX, posY, color.NRGBA{0, 0, 0, 255})
		}
	}

	fmt.Println(bytesWritten)

	//calculate n missing values in incomplete color
	//if len(pixel) < 4 {
	//	missing := 4 - len(pixel)
	//	for i := 0; i < missing; i++ {
	//		pixel = append(pixel, 255)
	//	}
	//}

	//append incomplete color to line
	//line = append(line, pixel)

	//calculate n missing values in line
	//if len(line) < dimensions {
	//	missing := (dimensions - len(line))
	//	missingColor := []byte{0, 0, 0, 255}
	//	//add n missing colors to line
	//	for i := 0; i < missing; i++ {
	//		line = append(line, missingColor)
	//	}
	//}
	//pxl = append(pxl, line)

	/*
		x := 0
		y := 0
		for i, line := range pxl {
			x = 0
			if i > 0 {
				y++
			}

			//each pixel in line
			for _, pixel := range line {
				img.Set(x, y, color.NRGBA{pixel[0], pixel[1], pixel[2], pixel[3]})
				x++
			}
		}
	*/

	//******************************************
	elapsed2 := time.Since(start2)
	fmt.Printf("generating image: %s\n", elapsed2)
	//==========================================

	p.encodedPayload = img

	return nil

}

// Decoded the Pxl.Source and stores it to Pxl.decodedPayload
func (p *Pxl) Decode() error {
	img, err := loadImage(p.Source)
	if err != nil {
{'fraction_non_alphanumeric': 0.2747579083279535, 'fraction_numerical': 0.016655907036797934, 'mean_word_length': 3.1892915089237426, 'pattern_counts': {'":': 0, '<': 16, '<?xml version=': 0, '>': 8, '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': '4346939', 'n_tokens_mistral': 3074, 'n_tokens_neox': 2706, 'n_words': 727}