<commit_before>package image

import (
	"bytes"
	"encoding/base64"
	"github.com/labstack/echo"
	"golang.org/x/image/draw"
	goimg "image"
	"image/gif"
	"image/jpeg"
	"image/png"
	"net/http"
	"xorkevin.dev/governor"
	"xorkevin.dev/governor/service/fileloader"
)

const (
	defaultThumbnailWidth  = 24
	defaultThumbnailHeight = 24
	defaultQuality         = 85
	defaultThumbQuality    = 50
)

type (
	// Image is an open image file
	Image interface {
		Duplicate() Image
		Resize(width, height int)
		ResizeFit(width, height int)
		ResizeLimit(width, height int)
		Crop(bounds goimg.Rectangle)
		ResizeFill(width, height int)
		ToJpeg(quality int) (*bytes.Buffer, error)
		ToBase64(quality int) (string, error)
	}

	imageData struct {
		img goimg.Image
	}

	// Options represent the image options of the loaded image
	Options struct {
		Width        int
		Height       int
		ThumbWidth   int
		ThumbHeight  int
		Quality      int
		ThumbQuality int
		Fill         bool
	}
)

// LoadJpeg reads an image from a form and encodes it as a jpeg
func LoadJpeg(c echo.Context, formField string, opt Options) (*bytes.Buffer, string, error) {
	if opt.Width < 1 || opt.Height < 1 {
		opt.Width = defaultThumbnailWidth
		opt.Height = defaultThumbnailHeight
	}
	if opt.ThumbWidth < 1 || opt.ThumbHeight < 1 {
		opt.ThumbWidth = defaultThumbnailWidth
		opt.ThumbHeight = defaultThumbnailHeight
	}
	if opt.Quality < 1 || opt.Quality > 100 {
		opt.Quality = defaultQuality
	}
	if opt.ThumbQuality < 1 || opt.ThumbQuality > 100 {
		opt.ThumbQuality = defaultThumbQuality
	}

	img, err := LoadImage(c, formField)
	if err != nil {
		return nil, "", err
	}
	if opt.Fill {
		img.ResizeFill(opt.Width, opt.Height)
	} else {
		img.ResizeLimit(opt.Width, opt.Height)
	}<commit_msg>Impl FromImage in image service to intake go std Image<commit_after>package image

import (
	"bytes"
	"encoding/base64"
	"github.com/labstack/echo"
	"golang.org/x/image/draw"
	goimg "image"
	"image/gif"
	"image/jpeg"
	"image/png"
	"net/http"
	"xorkevin.dev/governor"
	"xorkevin.dev/governor/service/fileloader"
)

const (
	defaultThumbnailWidth  = 24
	defaultThumbnailHeight = 24
	defaultQuality         = 85
	defaultThumbQuality    = 50
)

type (
	// Image is an open image file
	Image interface {
		Duplicate() Image
		Resize(width, height int)
		ResizeFit(width, height int)
		ResizeLimit(width, height int)
		Crop(bounds goimg.Rectangle)
		ResizeFill(width, height int)
		ToJpeg(quality int) (*bytes.Buffer, error)
		ToBase64(quality int) (string, error)
	}

	imageData struct {
		img goimg.Image
	}

	// Options represent the image options of the loaded image
	Options struct {
		Width        int
		Height       int
		ThumbWidth   int
		ThumbHeight  int
		Quality      int
		ThumbQuality int
		Fill         bool
	}
)

func FromImage(img goimg.Image) Image {
	return &imageData{
		img: img,
	}
}

// LoadJpeg reads an image from a form and encodes it as a jpeg
func LoadJpeg(c echo.Context, formField string, opt Options) (*bytes.Buffer, string, error) {
	if opt.Width < 1 || opt.Height < 1 {
		opt.Width = defaultThumbnailWidth
		opt.Height = defaultThumbnailHeight
	}
	if opt.ThumbWidth < 1 || opt.ThumbHeight < 1 {
		opt.ThumbWidth = defaultThumbnailWidth
		opt.ThumbHeight = defaultThumbnailHeight
	}
	if opt.Quality < 1 || opt.Quality > 100 {
		opt.Quality = defaultQuality
	}
	if opt.ThumbQuality < 1 || opt.ThumbQuality > 100 {
		opt.ThumbQuality = defaultThumbQuality
	}

	img, err := LoadImage(c, formField)
	if err != nil {
		return nil, "", err
	}
	if opt.Fill {
		img.ResizeFill(opt.Width, opt.Height)
	} else {
		img.ResizeLimit(opt.Width, opt.Height)
	}
{'fraction_non_alphanumeric': 0.1032293679271322, 'fraction_numerical': 0.013248688931824454, 'mean_word_length': 3.4141291108404386, 'pattern_counts': {'":': 0, '<': 15, '<?xml version=': 0, '>': 7, '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': '6163841', 'n_tokens_mistral': 1415, 'n_tokens_neox': 1285, 'n_words': 333}