Go语言中对图像进行缩放

摘要:
由谷歌开发的简单、高效、开源的Go语言已经成为一种新的流行语言。它专门针对多处理器系统应用程序的编程进行了优化,使Go编译的程序与C或C++代码一样快,并且更安全,支持并行进程。Go语言支持Go1版本上的Windows、Apple Mac OS X、Linux和FreeBSD操作系统。Go支持面向对象,并具有真正的封装和反射功能。在学习曲线方面,派克认为围棋与Java相似,Java开发人员应该能够轻松学习围棋。

由Google开发,简洁、高效、开源的Go语言日渐成为语言新宠。它专门针对多处理器系统应用程序的编程进行优化,使得Go编译的程序与C或C++代码的速度相媲美,且更安全、支持并行进程。Go语言在Go1版本上支持Windows, 苹果Mac OS X, Linux和FreeBSD操作系统。Go支持面向对象,而且具有真正的封装(closures)和反射 (reflection)等功能。

在学习曲线方面,派克认为Go与Java类似,对于Java开发者来说,应该能够轻松学会 Go。同样,对于C#开发者来说,也很轻松学会GO语言。

笔者对图形图像研究很感兴趣,让我们来看看Go语言中对图像进行缩放的解决方案:

package resize


import (
"image"
"image/color"
)


// average convert the sums to averages and returns the result.
func average(sum []uint64, w, h int, n uint64) *image.RGBA {
ret := image.NewRGBA(image.Rect(0, 0, w, h))
for y := 0; y < h; y++ {
for x := 0; x < w; x++ {
index := 4 * (y*w + x)
pix := ret.Pix[y*ret.Stride+x*4:]
pix[0] = uint8(sum[index+0] / n)
pix[1] = uint8(sum[index+1] / n)
pix[2] = uint8(sum[index+2] / n)
pix[3] = uint8(sum[index+3] / n)
}
}
return ret
}


// ResizeRGBA returns a scaled copy of the RGBA image slice r of m.
// The returned image has width w and height h.
func ResizeRGBA(m *image.RGBA, r image.Rectangle, w, h int) *image.RGBA {
ww, hh := uint64(w), uint64(h)
dx, dy := uint64(r.Dx()), uint64(r.Dy())
// See comment in Resize.
n, sum := dx*dy, make([]uint64, 4*w*h)
for y := r.Min.Y; y < r.Max.Y; y++ {
pix := m.Pix[(y-r.Min.Y)*m.Stride:]
for x := r.Min.X; x < r.Max.X; x++ {
// Get the source pixel.
p := pix[(x-r.Min.X)*4:]
r64 := uint64(p[0])
g64 := uint64(p[1])
b64 := uint64(p[2])
a64 := uint64(p[3])
// Spread the source pixel over 1 or more destination rows.
py := uint64(y) * hh
for remy := hh; remy > 0; {
qy := dy - (py % dy)
if qy > remy {
qy = remy
}
// Spread the source pixel over 1 or more destination columns.
px := uint64(x) * ww
index := 4 * ((py/dy)*ww + (px / dx))
for remx := ww; remx > 0; {
qx := dx - (px % dx)
if qx > remx {
qx = remx
}
qxy := qx * qy
sum[index+0] += r64 * qxy
sum[index+1] += g64 * qxy
sum[index+2] += b64 * qxy
sum[index+3] += a64 * qxy
index += 4
px += qx
remx -= qx
}
py += qy
remy -= qy
}
}
}
return average(sum, w, h, n)
}


// ResizeNRGBA returns a scaled copy of the RGBA image slice r of m.
// The returned image has width w and height h.
func ResizeNRGBA(m *image.NRGBA, r image.Rectangle, w, h int) *image.RGBA {
ww, hh := uint64(w), uint64(h)
dx, dy := uint64(r.Dx()), uint64(r.Dy())
// See comment in Resize.
n, sum := dx*dy, make([]uint64, 4*w*h)
for y := r.Min.Y; y < r.Max.Y; y++ {
pix := m.Pix[(y-r.Min.Y)*m.Stride:]
for x := r.Min.X; x < r.Max.X; x++ {
// Get the source pixel.
p := pix[(x-r.Min.X)*4:]
r64 := uint64(p[0])
g64 := uint64(p[1])
b64 := uint64(p[2])
a64 := uint64(p[3])
r64 = (r64 * a64) / 255
g64 = (g64 * a64) / 255
b64 = (b64 * a64) / 255
// Spread the source pixel over 1 or more destination rows.
py := uint64(y) * hh
for remy := hh; remy > 0; {
qy := dy - (py % dy)
if qy > remy {
qy = remy
}
// Spread the source pixel over 1 or more destination columns.
px := uint64(x) * ww
index := 4 * ((py/dy)*ww + (px / dx))
for remx := ww; remx > 0; {
qx := dx - (px % dx)
if qx > remx {
qx = remx
}
qxy := qx * qy
sum[index+0] += r64 * qxy
sum[index+1] += g64 * qxy
sum[index+2] += b64 * qxy
sum[index+3] += a64 * qxy
index += 4
px += qx
remx -= qx
}
py += qy
remy -= qy
}
}
}
return average(sum, w, h, n)
}


// Resample returns a resampled copy of the image slice r of m.
// The returned image has width w and height h.
func Resample(m image.Image, r image.Rectangle, w, h int) *image.RGBA {
if w < 0 || h < 0 {
return nil
}
if w == 0 || h == 0 || r.Dx() <= 0 || r.Dy() <= 0 {
return image.NewRGBA(image.Rect(0, 0, w, h))
}
curw, curh := r.Dx(), r.Dy()
img := image.NewRGBA(image.Rect(0, 0, w, h))
for y := 0; y < h; y++ {
for x := 0; x < w; x++ {
// Get a source pixel.
subx := x * curw / w
suby := y * curh / h
r32, g32, b32, a32 := m.At(subx, suby).RGBA()
r := uint8(r32 >> 8)
g := uint8(g32 >> 8)
b := uint8(b32 >> 8)
a := uint8(a32 >> 8)
img.SetRGBA(x, y, color.RGBA{r, g, b, a})
}
}
return img

}

有了上面的基础,下文讲解 利用Go语言上传图像并生成缩略图

免责声明:文章转载自《Go语言中对图像进行缩放》仅用于学习参考。如对内容有疑问,请及时联系本站处理。

上篇教你一招:修复win7 系统自带的截图工具损坏Fastdfs 存储下篇

宿迁高防,2C2G15M,22元/月;香港BGP,2C5G5M,25元/月 雨云优惠码:MjYwNzM=

相关文章

Go语言JSON数据相互转换

目录 结构体转json map转json int转json slice转json json反序列化为结构体 json反序列化为map 结构体转json 结构体转json示例: package main import ( "encoding/json" "fmt" ) //用户 type User struct { UserName st...

go语言学习--内核态和用户态(协程)

go中的一个特点就是引入了相比于线程更加轻量级的协程(用户态的线程),那么什么是用户态和内核态呢? 一、什么是用户态和内核态 当一个任务(进程)执行系统调用而陷入内核代码中执行时,我们就称进程处于内核运行态(或简称为内核态)。此时处理器处于特权级最高的(0级)内核代码中执行。当进程处于内核态时,执行的内核代码会使用当前进程的内核栈。每个进程都有自己的内核栈...

GO程序设计1——快速入门

1.GO语言开发环境搭建                                             1.1编译环境安装 点击链接http://code.google.com/p/go/downloads/list 选择一个版本,本人下载的是go1.0.3.windows-386.msi 双击安装,选择安装一个安装主目录:例如,C:\Go,不需...

Sublime Text 3 安装Go语言相关插件gosublime

1.打开Sublime Text,使用快捷键 ctrl+` (左上角Tab键上方,Esc键下方)或者使用菜单 View > Show Console menu,此时将出现Sublime Text的控制台,将如下代码分别放入执行(按回车)即可。 import urllib.request,os,hashlib; h = 'df21e130d211cfc...

【Go语言系列】在VsCode中配置Go的开发环境

一、为什么选VSCode 这个系列的初宗是带领公司的PHPer转Go,在正式写这篇博文前,咱们先说说Go有哪些主流的IDE 1、GoLand(收费) JetBrains出品必属精品,除了贵没有其它缺点,如果你用这款IDE,我只想说出门右转,不送!~~~~ 2、LiteIDE(免费)LiteIDE是一个简单的开源IDE。值得注意的是,它是Go语言2012年正...

go语言基础学习

go基础学习,面向对象-方法在Go语言中,可以给任意自定义类型(包括内置类型,但不包括指针类型)添加相应的方法 使用= 和:=的区别: // = 使用必须使用先var声明例如: var a a=100 //或 var b = 100 //或 var c int = 100 // := 是声明并赋值,并且系统自动推断类型,不需要var关键字 d :=...