package main
import (
"fmt"
"image"
"log"
"os"
"path/filepath"
"runtime"
"strings"
)
var workers = runtime.NumCPU()
type tagInfo struct {
height int
width int
name string
}
func main() {
if len(os.Args) == 1 {
log.Fatal("Usage: %s <imagename1 [imagename2] ...>",
filepath.Base(os.Args[0]))
}
for _, str := range os.Args[1:] {
ext := strings.ToLower(filepath.Ext(str))
if ext != ".png" && ext != ".jpg" && ext != ".jpeg" {
log.Fatal("Usage: %s <imagename1 [imagename2] ...>",
filepath.Base(os.Args[0]))
}
}
images := make(chan string, workers*4)
results := make(chan tagInfo, 4)
procressImgTag(images, results, os.Args[1:])
showResult(results, len(os.Args[1:]))
}
func procressImgTag(img chan string, result chan tagInfo, imgname []string)
{
for i := 0; i != workers; i++ {
go doJob(img, result)
}
for _, str := range imgname {
img <- str
}
close(img)
}
func doJob(img <-chan string, result chan<- tagInfo) {
for filename := range img {
if info, err := os.Stat(filename); err != nil ||
(info.Mode()&os.ModeType == 1) {
log.Print("Failed to check file ", filename, ". Error is ", err)
return
}
file, err := os.Open(filename)
if err != nil {
log.Print("Failed to open file ", filename, ". Error is ", err)
return
}
defer file.Close()
config, _, err := image.DecodeConfig(file)
if err != nil {
log.Print("Failed to decode file ", filename, ". Error is ",
err)
return
}
result <- tagInfo{width: config.Width, height: config.Height, name:
filename}
}
}
func showResult(result chan tagInfo, total int) {
for i := 0; i != total; i++ {
rlt := <-result
fmt.Println(rlt)
}
}
--
You received this message because you are subscribed to the Google Groups "golang-nuts" group.
To unsubscribe from this group and stop receiving emails from it, send an email to [email protected].
For more options, visit https://groups.google.com/groups/opt_out.