<commit_before>package tga

import (
	"encoding/binary"
	"errors"
	"image"
	"image/draw"
	"io"
)

// Encode encodes an image into TARGA format.
func Encode(w io.Writer, m image.Image) (err error) {
	b := m.Bounds()
	mw, mh := b.Dx(), b.Dy()

	h := rawHeader{
		Width:  uint16(mw),
		Height: uint16(mh),
	}

	if int(h.Width) != mw || int(h.Height) != mh {
		return errors.New("uint16 width/height overflow")
	}

	h.Flags = flagOriginTop

	switch tm := m.(type) {
	case *image.Gray:
		h.ImageType = imageTypeMonoChrome
		err = encodeGray(w, tm, h)

	case *image.NRGBA:
		h.ImageType = imageTypeTrueColor
		err = encodeRGBA(w, tm, h, attrTypeAlpha)

	case *image.RGBA:
		h.ImageType = imageTypeTrueColor
		err = encodeRGBA(w, (*image.NRGBA)(tm), h, attrTypePremultipliedAlpha)

	default:
		// convert to non-premultiplied alpha by default
		newm := image.NewNRGBA(b)
		draw.Draw(newm, b, m, b.Min, draw.Src)
		err = encodeRGBA(w, newm, h, attrTypeAlpha)
	}

	return
}<commit_msg>encode: fix invalid image type on converted images<commit_after>package tga

import (
	"encoding/binary"
	"errors"
	"image"
	"image/draw"
	"io"
)

// Encode encodes an image into TARGA format.
func Encode(w io.Writer, m image.Image) (err error) {
	b := m.Bounds()
	mw, mh := b.Dx(), b.Dy()

	h := rawHeader{
		Width:  uint16(mw),
		Height: uint16(mh),
	}

	if int(h.Width) != mw || int(h.Height) != mh {
		return errors.New("uint16 width/height overflow")
	}

	h.Flags = flagOriginTop

	switch tm := m.(type) {
	case *image.Gray:
		h.ImageType = imageTypeMonoChrome
		err = encodeGray(w, tm, h)

	case *image.NRGBA:
		h.ImageType = imageTypeTrueColor
		err = encodeRGBA(w, tm, h, attrTypeAlpha)

	case *image.RGBA:
		h.ImageType = imageTypeTrueColor
		err = encodeRGBA(w, (*image.NRGBA)(tm), h, attrTypePremultipliedAlpha)

	default:
		// convert to non-premultiplied alpha by default
		h.ImageType = imageTypeTrueColor
		newm := image.NewNRGBA(b)
		draw.Draw(newm, b, m, b.Min, draw.Src)
		err = encodeRGBA(w, newm, h, attrTypeAlpha)
	}

	return
}
{'fraction_non_alphanumeric': 0.14673913043478262, 'fraction_numerical': 0.005928853754940711, 'mean_word_length': 3.7535211267605635, 'pattern_counts': {'":': 0, '<': 3, '<?xml version=': 0, '>': 3, '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': '6901245', 'n_tokens_mistral': 875, 'n_tokens_neox': 814, 'n_words': 197}