Skip to content

Commit

Permalink
GITPIL
Browse files Browse the repository at this point in the history
  • Loading branch information
jiayuqi7813 committed Aug 14, 2022
1 parent 7938a3f commit ec138e5
Show file tree
Hide file tree
Showing 8 changed files with 363 additions and 0 deletions.
75 changes: 75 additions & 0 deletions ImgType.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,75 @@
package gopic

import (
"image"
"path/filepath"
)

// ImageType represents an image type value.
type ImageType int

type PicFile struct {
img image.Image
format string
err error
}

type NewPic struct {
img *image.RGBA
width int
height int
}

const (
// UNKNOWN represents an unknow image type value.
UNKNOWN ImageType = iota
// JPEG represents the JPEG image type.
JPEG
// WEBP represents the WEBP image type.
WEBP
// PNG represents the PNG image type.
PNG
// TIFF represents the TIFF image type.
TIFF
// GIF represents the GIF image type.
GIF
// PDF represents the PDF type.
PDF
// SVG represents the SVG image type.
SVG
// MAGICK represents the libmagick compatible genetic image type.
MAGICK
// HEIF represents the HEIC/HEIF/HVEC image type
HEIF
// AVIF represents the AVIF image type.
AVIF
)

// ImageTypes stores as pairs of image types supported and its alias names.
var ImageTypes = map[ImageType]string{
JPEG: "jpeg",
PNG: "png",
WEBP: "webp",
TIFF: "tiff",
GIF: "gif",
PDF: "pdf",
SVG: "svg",
MAGICK: "magick",
HEIF: "heif",
AVIF: "avif",
}

//ImgType returns the image type of the given filename.
func ImgType(filename string) ImageType {
ext := filepath.Ext(filename)
for k, v := range ImageTypes {
if ext == ".jpg" || ext == ".jpeg" {
return 1
}
if v == ext[1:] {
return k
}

}
return UNKNOWN
}
64 changes: 64 additions & 0 deletions Split.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,64 @@
package gopic

import (
"golang.org/x/image/draw"
"image"
"image/color"
)

//ImgSplit Split the image into r,g,b channels
func (p *PicFile) ImgSplit() (r, g, b *NewPic) {
w, h := p.Size()
r1 := image.NewRGBA(image.Rect(0, 0, w, h))
g1 := image.NewRGBA(image.Rect(0, 0, w, h))
b1 := image.NewRGBA(image.Rect(0, 0, w, h))
for i := 0; i < w; i++ {
for j := 0; j < h; j++ {
red, green, blue, _ := p.img.At(i, j).RGBA()
r1.Set(i, j, color.RGBA{uint8(red), 0, 0, 255})
g1.Set(i, j, color.RGBA{0, uint8(green), 0, 255})
b1.Set(i, j, color.RGBA{0, 0, uint8(blue), 255})
}
}
return &NewPic{r1, w, h}, &NewPic{g1, w, h}, &NewPic{b1, w, h}
}

//Img2Array convert image to array
func (p *PicFile) Img2Array() [][][3]float32 {
bounds := p.img.Bounds()
width, height := bounds.Max.X, bounds.Max.Y
iaa := make([][][3]float32, height)
src_rgba := image.NewRGBA(p.img.Bounds())
draw.Copy(src_rgba, image.Point{}, p.img, p.img.Bounds(), draw.Src, nil)

for y := 0; y < height; y++ {
row := make([][3]float32, width)
for x := 0; x < width; x++ {
idx_s := (y*width + x) * 4
pix := src_rgba.Pix[idx_s : idx_s+4]
row[x] = [3]float32{float32(pix[0]), float32(pix[1]), float32(pix[2])}
}
iaa[y] = row
}
return iaa
}

//Img2Array convert image to array
func (p *NewPic) Img2Array() [][][3]float32 {
bounds := p.img.Bounds()
width, height := bounds.Max.X, bounds.Max.Y
iaa := make([][][3]float32, height)
src_rgba := image.NewRGBA(p.img.Bounds())
draw.Copy(src_rgba, image.Point{}, p.img, p.img.Bounds(), draw.Src, nil)

for y := 0; y < height; y++ {
row := make([][3]float32, width)
for x := 0; x < width; x++ {
idx_s := (y*width + x) * 4
pix := src_rgba.Pix[idx_s : idx_s+4]
row[x] = [3]float32{float32(pix[0]), float32(pix[1]), float32(pix[2])}
}
iaa[y] = row
}
return iaa
}
41 changes: 41 additions & 0 deletions file.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,41 @@
package gopic

import (
"errors"
"image/jpeg"
"image/png"
"os"
)

// ImageFile Import the image and determine the type according to the suffix
//only jpg or png
func ImageFile(filename string) *PicFile {
imgType := ImgType(filename)
switch imgType {
case 1: //JPEG
img, err := FileOpen(filename)
if err != nil {
return nil
}
jpgfile, _ := jpeg.Decode(img)
return &PicFile{jpgfile, "jpg", nil}
case 3: //PNG
img, err := FileOpen(filename)
if err != nil {
return nil
}
pngfile, _ := png.Decode(img)
return &PicFile{pngfile, "png", nil}
default:
return &PicFile{nil, "None", errors.New("unsupported image type")}
}
}

//FileOpen Open the file and determine the type according to the suffix
func FileOpen(filename string) (*os.File, error) {
file, err := os.Open(filename)
if err != nil {
return nil, err
}
return file, nil
}
84 changes: 84 additions & 0 deletions getColor.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,84 @@
package gopic

import (
"fmt"
"fyne.io/fyne/v2"
"fyne.io/fyne/v2/app"
"fyne.io/fyne/v2/canvas"
"image/color"
"image/jpeg"
"image/png"
"os"
)

//GetPixel returns RGB of the pixel at (x, y).Not Alpha
func (p *PicFile) GetPixel(x, y int) []int {
//判断图片颜色类型
switch p.Mode() {
case "color.Gray":
return GetGraypicColor(p, x, y)
case "color.NRGBA":
return GetRGBpicColor(p, x, y)
case "color.CMYK":
return GetCMYKpicColor(p, x, y)
case "color.YCbCr":
return GetYcbcrpicColor(p, x, y)
default:
return nil
}
}

//GetGraypicColor returns Gray of the pixel at (x, y).Not Alpha
func GetGraypicColor(file *PicFile, x, y int) []int {
col := file.img.At(x, y).(color.Gray)
return []int{int(col.Y)}
}

//GetRGBpicColor returns RGB of the pixel at (x, y).Not Alpha
func GetRGBpicColor(file *PicFile, x, y int) []int {
col := file.img.At(x, y).(color.NRGBA)
return []int{int(col.R), int(col.G), int(col.B), int(col.A)}
}

//GetCMYKpicColor returns CMYK of the pixel at (x, y).Not Alpha
func GetCMYKpicColor(file *PicFile, x, y int) []int {
col := file.img.At(x, y).(color.CMYK)
return []int{int(col.C), int(col.M), int(col.Y), int(col.K)}
}

//GetYcbcrpicColor returns YCbCr of the pixel at (x, y).Not Alpha
func GetYcbcrpicColor(file *PicFile, x, y int) []int {
col := file.img.At(x, y).(color.YCbCr)
return []int{int(col.Y), int(col.Cb), int(col.Cr)}
}

//Save saves the image to file.
func (p *PicFile) Save(file string) {
f, err := os.Create(file)
if err != nil {
fmt.Println(err)
return
}
filetype := ImgType(file)
switch filetype {
case 1: //JPEG
jpeg.Encode(f, p.img, nil)
case 3: //PNG
png.Encode(f, p.img)
default:
fmt.Println("Unsupported file type")
}

}

//Show shows the image.
func (p *PicFile) Show() {
//随机文件名
a := app.New()
w := a.NewWindow("Images")
img := canvas.NewImageFromImage(p.img)
w.SetContent(img)
x, y := p.Size()
w.Resize(fyne.NewSize(float32(x), float32(y)))
w.ShowAndRun()
}
3 changes: 3 additions & 0 deletions go.mod
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
module github.com/jiayuqi7813/gopic

go 1.17
28 changes: 28 additions & 0 deletions goPic.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
package gopic

import (
"fmt"
"image/color"
)

//Size returns the size of the image.
func (p *PicFile) Size() (int, int) {
width := p.img.Bounds().Max.X
height := p.img.Bounds().Max.Y
return width, height
}

//Format returns the format of the image.
func (p *PicFile) Format() string {
return p.format
}

//Mode returns the mode of the image.
func (p *PicFile) Mode() string {
//col := color.RGBA{}
//colmode := p.img.ColorModel().Convert(col)
//return fmt.Sprintf("%T", colmode)
//输出图片色彩模式
c := color.ModelFunc(p.img.ColorModel().Convert)
return fmt.Sprintf("%T", c.Convert(color.RGBA{}))
}
54 changes: 54 additions & 0 deletions putColor.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,54 @@
package gopic

import (
"fmt"
"fyne.io/fyne/v2"
"fyne.io/fyne/v2/app"
"fyne.io/fyne/v2/canvas"
"image"
"image/color"
"image/png"
"os"
)

//NewPicFile returns a new PicFile.
func NewPicFile(width, height int, palette color.Palette) *NewPic {
newimg := image.NewRGBA(image.Rect(0, 0, width, height))
for x := 0; x < width; x++ {
for y := 0; y < height; y++ {
newimg.Set(x, y, palette.Convert(color.RGBA{}))
}
}
return &NewPic{newimg, width, height}
}

//PutPixel puts the pixel at (x, y) to the image.
func (p *NewPic) PutPixel(x, y int, palette color.Palette) {

p.img.Set(x, y, palette.Convert(color.RGBA{}))
}

//Save saves the image to file.
func (p *NewPic) Save(file string) {
f, err := os.Create(file)
if err != nil {
fmt.Println(err)
return
}
err = png.Encode(f, p.img)
if err != nil {
fmt.Println(err)
return
}
}

//Show shows the image.
func (p *NewPic) Show() {
//随机文件名
a := app.New()
w := a.NewWindow("Images")
img := canvas.NewImageFromImage(p.img)
w.SetContent(img)
w.Resize(fyne.NewSize(float32(p.width), float32(p.height)))
w.ShowAndRun()
}
14 changes: 14 additions & 0 deletions resize.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
package gopic

import "github.com/nfnt/resize"

//Resize resizes the image to the specified width and height.
func (p *PicFile) Resize(width, height int) *PicFile {
m := resize.Resize(uint(width), uint(height), p.img, resize.Lanczos3)
return &PicFile{m, p.format, nil}
}

func (p *NewPic) Resize(width, height int) *PicFile {
m := resize.Resize(uint(width), uint(height), p.img, resize.Lanczos3)
return &PicFile{m, "png", nil}
}

0 comments on commit ec138e5

Please sign in to comment.