Add color calibration functionality and cleanup a bit

This commit is contained in:
zuckerberg
2023-09-03 22:36:34 -06:00
parent ec941eceb8
commit eff01cd141
4 changed files with 85 additions and 548 deletions

View File

@@ -69,6 +69,40 @@ func ConvertToEInkImage(src image.Image, colorSpace ColorSpace) []byte {
return einkImage
}
func min(a, b int) int {
if a < b {
return a
}
return b
}
func GenerateCalibrationImage(width, height int, colorSpace ColorSpace) []byte {
var einkImage []byte
colorCount := len(colorSpace)
// calculate number of rows and columns (sideLength)
sideLengthFloat := math.Sqrt(float64(colorCount))
sideLength := int(sideLengthFloat)
if float64(sideLength) != sideLengthFloat {
sideLength = sideLength + 1
}
rowSpacing := height / sideLength
columnSpacing := width / sideLength
for y := 0; y < height; y++ {
for x := 0; x < width; x++ {
row := y / rowSpacing
column := x / columnSpacing
colorIndex := min(colorCount-1, row*sideLength+column)
einkImage = append(einkImage, colorSpace[colorIndex].Code)
}
}
return packBytesIntoNibbles(einkImage)
}
func packBytesIntoNibbles(input []byte) []byte {
// input length must divisible by 2

View File

@@ -63,6 +63,22 @@ type ImageProperties struct {
ColorSpace ColorSpace `json:"color_space"`
}
func calibrationImage(w http.ResponseWriter, r *http.Request) {
var imageProps ImageProperties
err := json.NewDecoder(r.Body).Decode(&imageProps)
if err != nil {
http.Error(w, err.Error(), http.StatusBadRequest)
return
}
data := GenerateCalibrationImage(imageProps.Width, imageProps.Height, imageProps.ColorSpace)
fmt.Printf("Bytes to send: %+v\n", len(data))
w.Header().Set("Content-Length", strconv.Itoa(len(data)))
w.Write(data)
}
func fetchImage(w http.ResponseWriter, r *http.Request) {
var imageProps ImageProperties
err := json.NewDecoder(r.Body).Decode(&imageProps)
@@ -120,8 +136,9 @@ func main() {
router.Get("/{name}", requestHandler)
router.Group(func(r chi.Router) {
r.Use(basicAuth)
// r.Use(basicAuth)
r.Post("/fetchImage", fetchImage)
r.Post("/calibrationImage", calibrationImage)
})
// Start the HTTP server on port 8080 and log any errors