Skip to content

Instantly share code, notes, and snippets.

@mattli001
Created April 13, 2017 10:09
Show Gist options
  • Save mattli001/463dab826a03487909931e145c72ec1f to your computer and use it in GitHub Desktop.
Save mattli001/463dab826a03487909931e145c72ec1f to your computer and use it in GitHub Desktop.
calc downsize of video resolution
package main
import "fmt"
// Calculates scaling factors using old and new image dimensions.
func calcFactors(width, height uint, oldWidth, oldHeight float64) (scaleX, scaleY float64) {
if width == 0 {
if height == 0 {
scaleX = 1.0
scaleY = 1.0
} else {
scaleY = oldHeight / float64(height)
scaleX = scaleY
}
} else {
scaleX = oldWidth / float64(width)
if height == 0 {
scaleY = scaleX
} else {
scaleY = oldHeight / float64(height)
}
}
return
}
func resize(width, height uint, orgWidth, orgHeight int) (w, h int) {
scaleX, scaleY := calcFactors(width, height, float64(orgWidth), float64(orgHeight))
if width == 0 {
width = uint(float64(orgWidth) / scaleX)
if width%2 != 0 {
width++
}
}
if height == 0 {
height = uint(float64(orgHeight) / scaleY)
if height%2 != 0 {
height++
}
}
return int(width), int(height)
}
func main() {
type Resolution struct {
width int
height int
}
// res169List := []Resolution{{3840, 2160}, {2560, 1440}, {1920, 1080},
// {1280, 720}, {960, 540}, {854, 480}, {640, 360}, {512, 288}}
// res916List := []Resolution{{2160, 3840}, {1440, 2560}, {1080, 1920},
// {720, 1280}, {540, 960}, {480, 854}, {360, 640}, {288, 512}}
res43List := []Resolution{{2048, 1536}, {1600, 1200}, {1280, 960},
{800, 600}, {640, 480}, {512, 384}, {480, 360}, {400, 300}, {320, 240}}
res34List := []Resolution{{1536, 2048}, {1200, 1600}, {960, 1280},
{600, 800}, {480, 640}, {384, 512}, {360, 480}, {300, 400}, {240, 320}}
for _, item := range res43List {
var w, h int
input := item.height
fmt.Printf("Org %d x %d\n", item.width, item.height)
switch {
case input > 1080:
w, h = resize(0, 1080, item.width, item.height)
fmt.Printf("High %d x %d\n", w, h)
w, h = resize(0, 480, item.width, item.height)
fmt.Printf("Normal %d x %d\n", w, h)
case input > 720:
w, h = resize(0, 720, item.width, item.height)
fmt.Printf("High %d x %d\n", w, h)
w, h = resize(0, 480, item.width, item.height)
fmt.Printf("Normal %d x %d\n", w, h)
case input > 480:
w, h = resize(0, 480, item.width, item.height)
fmt.Printf("High %d x %d\n", w, h)
w, h = resize(0, 360, item.width, item.height)
fmt.Printf("Normal %d x %d\n", w, h)
case input > 360:
w, h = resize(0, 360, item.width, item.height)
fmt.Printf("Normal %d x %d\n", w, h)
}
fmt.Printf("\n")
}
fmt.Printf("========================\n")
for _, item := range res34List {
var w, h int
input := item.width
fmt.Printf("Org %d x %d\n", item.width, item.height)
switch {
case input > 1080:
w, h = resize(1080, 0, item.width, item.height)
fmt.Printf("High %d x %d\n", w, h)
w, h = resize(480, 0, item.width, item.height)
fmt.Printf("Normal %d x %d\n", w, h)
case input > 720:
w, h = resize(720, 0, item.width, item.height)
fmt.Printf("High %d x %d\n", w, h)
w, h = resize(480, 0, item.width, item.height)
fmt.Printf("Normal %d x %d\n", w, h)
case input > 480:
w, h = resize(480, 0, item.width, item.height)
fmt.Printf("High %d x %d\n", w, h)
w, h = resize(360, 0, item.width, item.height)
fmt.Printf("Normal %d x %d\n", w, h)
case input > 360:
w, h = resize(360, 0, item.width, item.height)
fmt.Printf("Normal %d x %d\n", w, h)
}
fmt.Printf("\n")
}
}
/* exmaple 16:9
Org 3840 x 2160
High 1920 x 1080
Normal 854 x 480
Org 2560 x 1440
High 1920 x 1080
Normal 854 x 480
Org 1920 x 1080
High 1280 x 720
Normal 854 x 480
Org 1280 x 720
High 854 x 480
Normal 640 x 360
Org 960 x 540
High 854 x 480
Normal 640 x 360
Org 854 x 480
Normal 640 x 360
Org 640 x 360
Org 512 x 288
========================
Org 2160 x 3840
High 1080 x 1920
Normal 480 x 854
Org 1440 x 2560
High 1080 x 1920
Normal 480 x 854
Org 1080 x 1920
High 720 x 1280
Normal 480 x 854
Org 720 x 1280
High 480 x 854
Normal 360 x 640
Org 540 x 960
High 480 x 854
Normal 360 x 640
Org 480 x 854
Normal 360 x 640
Org 360 x 640
Org 288 x 512
*/
/* example 4:3
Org 2048 x 1536
High 1440 x 1080
Normal 640 x 480
Org 1600 x 1200
High 1440 x 1080
Normal 640 x 480
Org 1280 x 960
High 960 x 720
Normal 640 x 480
Org 800 x 600
High 640 x 480
Normal 480 x 360
Org 640 x 480
Normal 480 x 360
Org 512 x 384
Normal 480 x 360
Org 400 x 300
Org 320 x 240
========================
Org 1536 x 2048
High 1080 x 1440
Normal 480 x 640
Org 1200 x 1600
High 1080 x 1440
Normal 480 x 640
Org 960 x 1280
High 720 x 960
Normal 480 x 640
Org 600 x 800
High 480 x 640
Normal 360 x 480
Org 480 x 640
Normal 360 x 480
Org 384 x 512
Normal 360 x 480
Org 300 x 400
Org 240 x 320
*/
@cage1016
Copy link

Org High Normal
16:9 Horizontal
3840 x 2160 1920 x 1080 854 x 480
2560 x 1440 1920 x 1080 854 x 480
1920 x 1080 1280 x 720 854 x 480
1280 x 720 854 x 480 640 x 360
960 x 540 854 x 480 640 x 360
854 x 480 640 x 360
640 x 360
512 x 288
16:9 portrait 2160 x 3840 1080 x 1920 480 x 854
1440 x 2560 1080 x 1920 480 x 854
1080 x 1920 720 x 1280 480 x 854
720 x 1280 480 x 854 360 x 640
540 x 960 480 x 854 360 x 640
480 x 854 360 x 640
360 x 640
288 x 512
4:3 Horizontal
2048 x 1536 1440 x 1080 640 x 480
1600 x 1200 1440 x 1080 640 x 480
1280 x 960 960 x 720 640 x 480
800 x 600 640 x 480 480 x 360
640 x 480 480 x 360
512 x 384 480 x 360
400 x 300
320 x 240
4:3 portrait
1536 x 2048 1080 x 1440 480 x 640
1200 x 1600 1080 x 1440 480 x 640
960 x 1280 720 x 960 480 x 640
600 x 800 480 x 640 360 x 480
480 x 640 360 x 480
384 x 512 360 x 480
300 x 400
240 x 320

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment