Scaling images in the go Language

Source: Internet
Author: User

Developed by Google, the simple, efficient, and open-source go language is becoming a new favorite of languages. It is specially optimized for the programming of multi-processor system applications, making the program compiled by go comparable to the speed of C or C ++ code, and more secure and supports parallel processes. Go supports windows, Apple Mac OS X, Linux, and FreeBSD operating systems in go1. Go supports object-oriented features such as closures and reflection.

In terms of learning curves, Parker believes that go is similar to Java, and Java developers should be able to easily learn go. Similarly, C # developers can easily learn the go language.

I am very interested in graph image research. Let's take a look at the solution for image scaling in the go language:

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 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 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, })
}
}
Return img

}

(Longgang Minsheng network Co., http://www.lgms.net)

With the above basics, the following describes how to use the go language to upload images and generate thumbnails.

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.