-
Notifications
You must be signed in to change notification settings - Fork 1
/
main.go
149 lines (122 loc) · 3.77 KB
/
main.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
package main
import (
"encoding/json"
"fmt"
"image"
"net/http"
"strconv"
"golang.org/x/image/tiff"
"github.com/disintegration/imaging"
"github.com/gorilla/mux"
"github.com/rs/cors"
)
type ErrorResponse struct {
Error string `json:"error"`
}
func findMinDimensions(images []image.Image) (int, int) {
minWidth, minHeight := images[0].Bounds().Dx(), images[0].Bounds().Dy()
for _, img := range images {
width, height := img.Bounds().Dx(), img.Bounds().Dy()
if width < minWidth {
minWidth = width
}
if height < minHeight {
minHeight = height
}
}
return minWidth, minHeight
}
func interlaceImages(images []image.Image, stripHeight int) (image.Image, error) {
width := images[0].Bounds().Dx()
height := images[0].Bounds().Dy()
interlaced := image.NewNRGBA(image.Rect(0, 0, width, height))
for y := 0; y < height; y++ {
imgIndex := (y / stripHeight) % len(images)
for x := 0; x < width; x++ {
interlaced.Set(x, y, images[imgIndex].At(x, y))
}
}
return interlaced, nil
}
func writeJSONError(w http.ResponseWriter, message string, status int) {
w.Header().Set("Content-Type", "application/json")
w.WriteHeader(status)
json.NewEncoder(w).Encode(ErrorResponse{Error: message})
}
func uploadHandler(w http.ResponseWriter, r *http.Request) {
err := r.ParseMultipartForm(32 << 20)
if err != nil {
writeJSONError(w, "Unable to parse form", http.StatusBadRequest)
return
}
// Parse LPI from form data (optional, defaults to 50)
lpiStr := r.FormValue("lpi")
lpi, err := strconv.ParseFloat(lpiStr, 64)
if err != nil || lpi <= 0 {
lpi = 50
}
dpiStr := r.FormValue("dpi")
dpi, err := strconv.ParseFloat(dpiStr, 64)
if err != nil || dpi <= 0 {
dpi = 96
}
stripWidthStr := r.FormValue("stripWidth")
stripWidth, err := strconv.ParseFloat(stripWidthStr, 64)
if err != nil || stripWidth <= 0 {
stripWidth = dpi / lpi
}
fmt.Printf("Calculated strip width: %.2f pixels per strip\n", stripWidth)
files := r.MultipartForm.File["images"]
if len(files) == 0 {
writeJSONError(w, "No images uploaded", http.StatusBadRequest)
return
}
var loadedImages []image.Image
for _, fileHeader := range files {
file, err := fileHeader.Open()
if err != nil {
fmt.Println(err)
writeJSONError(w, "Unable to open uploaded file", http.StatusInternalServerError)
return
}
defer file.Close()
img, err := imaging.Decode(file)
if err != nil {
fmt.Println(err)
writeJSONError(w, "Failed to decode image", http.StatusBadRequest)
return
}
loadedImages = append(loadedImages, img)
}
minWidth, minHeight := findMinDimensions(loadedImages)
fmt.Printf("Minimum dimensions: %d x %d\n", minWidth, minHeight)
var resizedImages []image.Image
for _, img := range loadedImages {
resizedImg := imaging.Resize(img, minWidth, minHeight, imaging.Lanczos)
resizedImages = append(resizedImages, resizedImg)
}
interlacedImg, err := interlaceImages(resizedImages, int(stripWidth))
if err != nil {
fmt.Println(err)
writeJSONError(w, "Failed to interlace images", http.StatusInternalServerError)
return
}
// **New Step: Scale the interlaced image to double its dimensions**
doubleWidth := interlacedImg.Bounds().Dx() * 2
doubleHeight := interlacedImg.Bounds().Dy() * 2
scaledImg := imaging.Resize(interlacedImg, doubleWidth, doubleHeight, imaging.Lanczos)
fmt.Printf("Scaled dimensions: %d x %d\n", doubleWidth, doubleHeight)
w.Header().Set("Content-Type", "image/tiff")
if err := tiff.Encode(w, scaledImg, nil); err != nil {
fmt.Println(err)
writeJSONError(w, "Failed to encode interlaced image", http.StatusInternalServerError)
return
}
}
func main() {
router := mux.NewRouter()
router.Use(cors.Default().Handler)
router.HandleFunc("/upload", uploadHandler).Methods("POST")
fmt.Println("Server started at :8081")
http.ListenAndServe(":8081", router)
}