<commit_before>package helpers

import (
	"C"
	"bytes"
	_ "image/jpeg"
	_ "image/png"
	"time"

	"image"
	"strings"

	"github.com/lucasb-eyer/go-colorful"
	"github.com/ungerik/go-cairo"
)

// Creates a Collage PNG Image from internet image urls (PNG or JPEG).
// imageUrls		: a slice with all image URLs. Empty strings will create an empty space in the collage.
// descriptions		: a slice with text that will be written on each tile. Can be empty.
// width			: the width of the result collage image.
// height			: the height of the result collage image.
// tileWidth		: the width of each tile image.
// tileHeight		: the height of each tile image.
// backgroundColour	: the background colour as a hex string.
func CollageFromUrls(imageUrls, descriptions []string, width, height, tileWidth, tileHeight int, backgroundColour string) (collageBytes []byte) {
	imageDataArray := make([][]byte, 0)
	// download images
	for _, imageUrl := range imageUrls {
		if imageUrl == "" {
			imageDataArray = append(imageDataArray, nil)
			continue
		}
		imageData, err := NetGetUAWithErrorAndTimeout(imageUrl, DEFAULT_UA, 15*time.Second)
		RelaxLog(err)
		if err == nil {
			imageDataArray = append(imageDataArray, imageData)
		} else {
			imageDataArray = append(imageDataArray, nil)
		}
	}

	// create surface with given background colour
	backgroundColourRGB, _ := colorful.Hex(backgroundColour)
	cairoSurface := cairo.NewSurface(cairo.FORMAT_RGB24, width, height)
	cairoSurface.SetSourceRGB(backgroundColourRGB.R, backgroundColourRGB.G, backgroundColourRGB.B)
	cairoSurface.Paint()

	var posX, posY int

	for i, imageData := range imageDataArray {
		// switch tile to new line if required
		if posX > 0 && posX+tileWidth > width {
			posY += tileHeight
			posX = 0
		}
		// draw image on tile if image exists
		if imageData != nil && len(imageData) > 0 {<commit_msg>[collage] add CollageFromBytes so that requesting from url isn't required<commit_after>package helpers

import (
	"C"
	"bytes"
	_ "image/jpeg"
	_ "image/png"
	"time"

	"image"
	"strings"

	"github.com/lucasb-eyer/go-colorful"
	"github.com/ungerik/go-cairo"
)

// Creates a Collage PNG Image from internet image urls (PNG or JPEG).
// imageUrls		: a slice with all image URLs. Empty strings will create an empty space in the collage.
// descriptions		: a slice with text that will be written on each tile. Can be empty.
// width			: the width of the result collage image.
// height			: the height of the result collage image.
// tileWidth		: the width of each tile image.
// tileHeight		: the height of each tile image.
// backgroundColour	: the background colour as a hex string.
func CollageFromUrls(imageUrls, descriptions []string, width, height, tileWidth, tileHeight int, backgroundColour string) (collageBytes []byte) {
	imageDataArray := make([][]byte, 0)
	// download images
	for _, imageUrl := range imageUrls {
		if imageUrl == "" {
			imageDataArray = append(imageDataArray, nil)
			continue
		}
		imageData, err := NetGetUAWithErrorAndTimeout(imageUrl, DEFAULT_UA, 15*time.Second)
		RelaxLog(err)
		if err == nil {
			imageDataArray = append(imageDataArray, imageData)
		} else {
			imageDataArray = append(imageDataArray, nil)
		}
	}

	return CollageFromBytes(imageDataArray, descriptions, width, height, tileWidth, tileHeight, backgroundColour)
}

// Creates a Collage PNG Image from image []byte (PNG or JPEG).
// imageDataArray   : a slice of all image []byte data
// descriptions		: a slice with text that will be written on each tile. Can be empty.
// width			: the width of the result collage image.
// height			: the height of the result collage image.
// tileWidth		: the width of each tile image.
// tileHeight		: the height of each tile image.
// backgroundColour	: the background colour as a hex string.
func CollageFromBytes(imageDataArray [][]byte, descriptions []string, width, height, tileWidth, tileHeight int, backgroundColour string) (collageBytes []byte) {

	// create surface with given background colour
	backgroundColourRGB, _ := colorful.Hex(backgroundColour)
	cairoSurface := cairo.NewSurface(cairo.FORMAT_RGB24, width, height)
	cairoSurface.SetSourceRGB(backgroundColourRGB.R, backgroundColourRGB.G, backgroundColourRGB.B)
	cairoSurface.Paint()

	var posX, posY int

	for i, imageData := range imageDataArray {
		// switch tile to new line if required
		if posX > 0 && posX+tileWidth > width {
			posY += tileHeight
			posX = 0
		}
		// draw image on tile if image exists
		if imageData != nil && len(imageData) > 0 {
{'fraction_non_alphanumeric': 0.09893522626441881, 'fraction_numerical': 0.00354924578527063, 'mean_word_length': 3.9278688524590164, 'pattern_counts': {'":': 0, '<': 3, '<?xml version=': 0, '>': 9, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 2, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '5729429', 'n_tokens_mistral': 1505, 'n_tokens_neox': 1405, 'n_words': 485}