Skip to content

Commit

Permalink
Render GIF
Browse files Browse the repository at this point in the history
  • Loading branch information
fluffy-melli committed Dec 27, 2024
1 parent 22d0502 commit c3bcd40
Show file tree
Hide file tree
Showing 8 changed files with 151 additions and 10 deletions.
1 change: 0 additions & 1 deletion RDR_CMP/api.txt

This file was deleted.

9 changes: 1 addition & 8 deletions RDR_CMP/rader.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,6 @@ import (
"net/http"
"net/url"
"strings"
"time"
)

type Response struct {
Expand All @@ -30,13 +29,7 @@ type Response struct {
} `json:"response"`
}

func Time() string {
currentTime := time.Now()
return currentTime.Format("20060102")
}

// time : YYYYMMDD | 20241226
func GetImages(apikey, time string) ([]string, error) {
func GetImagesURL(apikey, time string) ([]string, error) {
URL := "http://apis.data.go.kr/1360000/RadarImgInfoService/getCmpImg"
params := url.Values{}
params.Add("serviceKey", apikey)
Expand Down
11 changes: 10 additions & 1 deletion krapo.go
Original file line number Diff line number Diff line change
@@ -1,3 +1,12 @@
package krapo

import _ "github.com/fluffy-melli/krapo/RDR_CMP"
import (
"time"

_ "github.com/fluffy-melli/krapo/RDR_CMP" // https://www.data.go.kr/tcs/dss/selectApiDataDetailView.do?publicDataPk=15056924
)

func Time() string {
currentTime := time.Now()
return currentTime.Format("20060102")
}
38 changes: 38 additions & 0 deletions render/fetch.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,38 @@
package render

import (
"fmt"
"image"
_ "image/gif"
_ "image/jpeg"
_ "image/png"
"net/http"
"strings"
"sync"
)

var FileCache = make(map[string]image.Image)
var mu sync.RWMutex

func LoadImageFromURL(ImageURL string) (image.Image, error) {
ImageURL = strings.TrimSpace(ImageURL)
mu.RLock()
img, exists := FileCache[ImageURL]
mu.RUnlock()
if exists {
return img, nil
}
resp, err := http.Get(ImageURL)
if err != nil {
return nil, fmt.Errorf("failed to fetch image from URL %s: %v", ImageURL, err)
}
defer resp.Body.Close()
img, _, err = image.Decode(resp.Body)
if err != nil {
return nil, fmt.Errorf("failed to decode image from URL %s: %v", ImageURL, err)
}
mu.Lock()
FileCache[ImageURL] = img
mu.Unlock()
return img, nil
}
23 changes: 23 additions & 0 deletions render/file.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
package render

import (
"bytes"
"fmt"
"os"
)

func Write(filename string, buf *bytes.Buffer) error {
if buf == nil {
return fmt.Errorf("buffer cannot be nil")
}
file, err := os.Create(filename)
if err != nil {
return fmt.Errorf("failed to create file: %v", err)
}
defer file.Close()
_, err = file.Write(buf.Bytes())
if err != nil {
return fmt.Errorf("failed to write data to file: %v", err)
}
return nil
}
54 changes: 54 additions & 0 deletions render/gifs.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,54 @@
package render

import (
"bytes"
"image"
"image/color"
"image/gif"
)

func GIF(urls []string, delay int) (*bytes.Buffer, error) {
var images []*image.Paletted
var delays []int
for _, url := range urls {
img, err := LoadImageFromURL(url)
if err != nil {
return nil, err
}
palette := Palette(img)
palettedImage := image.NewPaletted(img.Bounds(), palette)
for y := 0; y < palettedImage.Bounds().Dy(); y++ {
for x := 0; x < palettedImage.Bounds().Dx(); x++ {
palettedImage.Set(x, y, img.At(x, y))
}
}
images = append(images, palettedImage)
delays = append(delays, delay)
}
var buf bytes.Buffer
err := gif.EncodeAll(&buf, &gif.GIF{
Image: images,
Delay: delays,
})
if err != nil {
return nil, err
}
return &buf, nil
}

func Palette(frame image.Image) color.Palette {
colorSet := make(map[color.Color]struct{})
for y := 0; y < frame.Bounds().Dy(); y++ {
for x := 0; x < frame.Bounds().Dx(); x++ {
colorSet[frame.At(x, y)] = struct{}{}
}
}
var colors []color.Color
for c := range colorSet {
colors = append(colors, c)
}
if len(colors) > 256 {
colors = colors[:256]
}
return colors
}
25 changes: 25 additions & 0 deletions test/module_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
package main

import (
"log"
"testing"

"github.com/fluffy-melli/krapo"
"github.com/fluffy-melli/krapo/RDR_CMP"
"github.com/fluffy-melli/krapo/render"
)

func Test(t *testing.T) {
urls, err := RDR_CMP.GetImagesURL("", krapo.Time())
if err != nil {
log.Fatalln(err)
}
gif, err := render.GIF(urls, 5)
if err != nil {
log.Fatalln(err)
}
err = render.Write("./test.gif", gif)
if err != nil {
log.Fatalln(err)
}
}
Binary file added test/test.gif
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.

0 comments on commit c3bcd40

Please sign in to comment.