Skip to content

Commit

Permalink
fomat
Browse files Browse the repository at this point in the history
  • Loading branch information
scott-mescudi committed Dec 10, 2024
1 parent 3d864f9 commit 3606cbb
Show file tree
Hide file tree
Showing 6 changed files with 64 additions and 67 deletions.
14 changes: 7 additions & 7 deletions global.go
Original file line number Diff line number Diff line change
Expand Up @@ -13,11 +13,11 @@ import (
// that can be embedded in the given image, based on the specified bit depth.
// Returns 0 if the bit depth exceeds 7, as higher depths are unsupported.
func GetImageCapacity(coverImage image.Image, bitDepth uint8) int {
if bitDepth > 7 {
return 0
}
return ((coverImage.Bounds().Max.X * coverImage.Bounds().Max.Y * 3) / 8) * (int(bitDepth) + 1)
if bitDepth > 7 {
return 0
}

return ((coverImage.Bounds().Max.X * coverImage.Bounds().Max.Y * 3) / 8) * (int(bitDepth) + 1)
}

// takes in path to imaeg and returns image.image
Expand All @@ -27,7 +27,7 @@ func Decodeimage(path string) (image.Image, error) {
if err != nil {
return nil, err
}

switch ext {
case ".jpg":
return jpeg.Decode(file)
Expand All @@ -38,4 +38,4 @@ func Decodeimage(path string) (image.Image, error) {
}

return nil, fmt.Errorf("invalid image format")
}
}
100 changes: 49 additions & 51 deletions pkg/image.go
Original file line number Diff line number Diff line change
Expand Up @@ -56,62 +56,60 @@ func ExtractRGBChannelsFromImage(img image.Image) []RgbChannel {
return pixels
}


type split struct {
start, end int
start, end int
}

func splitTask(n int, imgy int) []split {
if n <= 0 || imgy <= 0 {
return nil
}

sizes := make([]split, n)
pp := imgy / n
remainder := imgy % n

start := 0
for i := 0; i < n; i++ {
extra := 0
if i < remainder {
extra = 1
}
sizes[i] = split{start: start, end: start + pp + extra}
start = sizes[i].end
}

return sizes
}
if n <= 0 || imgy <= 0 {
return nil
}

sizes := make([]split, n)
pp := imgy / n
remainder := imgy % n

start := 0
for i := 0; i < n; i++ {
extra := 0
if i < remainder {
extra = 1
}
sizes[i] = split{start: start, end: start + pp + extra}
start = sizes[i].end
}

return sizes
}

// use this wth numGoroutines := runtime.NumCPU()
func ExtractRGBChannelsFromImageWithConCurrency(img image.Image, numGoroutines int) []RgbChannel {
bounds := img.Bounds()
totalPixels := bounds.Dx() * bounds.Dy()
pixels := make([]RgbChannel, totalPixels)

splits := splitTask(numGoroutines, bounds.Max.Y)

var wg sync.WaitGroup
for _, s := range splits {
wg.Add(1)
go func(start, end int) {
defer wg.Done()
idx := start * bounds.Dx()
for y := start; y < end; y++ {
for x := bounds.Min.X; x < bounds.Max.X; x++ {
r, g, b, _ := img.At(x, y).RGBA()
if r > 255 || g > 255 || b > 255 {
pixels[idx] = RgbChannel{R: r >> 8, G: g >> 8, B: b >> 8}
} else {
pixels[idx] = RgbChannel{R: r, G: g, B: b}
}
idx++
}
}
}(s.start, s.end)
}

wg.Wait()
return pixels
}
bounds := img.Bounds()
totalPixels := bounds.Dx() * bounds.Dy()
pixels := make([]RgbChannel, totalPixels)

splits := splitTask(numGoroutines, bounds.Max.Y)

var wg sync.WaitGroup
for _, s := range splits {
wg.Add(1)
go func(start, end int) {
defer wg.Done()
idx := start * bounds.Dx()
for y := start; y < end; y++ {
for x := bounds.Min.X; x < bounds.Max.X; x++ {
r, g, b, _ := img.At(x, y).RGBA()
if r > 255 || g > 255 || b > 255 {
pixels[idx] = RgbChannel{R: r >> 8, G: g >> 8, B: b >> 8}
} else {
pixels[idx] = RgbChannel{R: r, G: g, B: b}
}
idx++
}
}
}(s.start, s.end)
}

wg.Wait()
return pixels
}
3 changes: 1 addition & 2 deletions pkg/image_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -180,7 +180,6 @@ func TestExtractRGBChannelsFromImageWithConCurrency_EmptyImage(t *testing.T) {
assert.Empty(t, result)
}


func BenchmarkExtractRGBChannelsFromImageWithConCurrency(b *testing.B) {
// Generate a sample image for testing
width, height := 10000, 10000 // Modify as needed
Expand Down Expand Up @@ -208,4 +207,4 @@ func generateTestImage(width, height int) image.Image {
}
}
return img
}
}
2 changes: 1 addition & 1 deletion pkg/rgb.go
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,7 @@ func EmbedIntoRGBchannelsWithDepth(RGBchannels []RgbChannel, data []byte, depth
return nil, fmt.Errorf("bit depth exeeds 7")
}

if (len(data)*8)+32 > len(RGBchannels)* 3 * (int(depth)+1) {
if (len(data)*8)+32 > len(RGBchannels)*3*(int(depth)+1) {
return nil, fmt.Errorf("data is too big")
}

Expand Down
10 changes: 5 additions & 5 deletions pkg/utils.go
Original file line number Diff line number Diff line change
Expand Up @@ -26,12 +26,12 @@ func Int32ToBinary(num int32) []int {
}

func GetlenOfData(data []byte) (int, error) {
if len(data) < 4 {
return 0, fmt.Errorf("insufficient data: expected at least 4 bytes")
}
if len(data) < 4 {
return 0, fmt.Errorf("insufficient data: expected at least 4 bytes")
}

n := int(data[0])<<24 | int(data[1])<<16 | int(data[2])<<8 | int(data[3])
return n, nil
n := int(data[0])<<24 | int(data[1])<<16 | int(data[2])<<8 | int(data[3])
return n, nil
}

func FlipBit(num uint32, position uint8) uint32 {
Expand Down
2 changes: 1 addition & 1 deletion variables.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,5 +7,5 @@ var (
const (
MinBitDepth uint8 = 0
MaxBitDepth uint8 = 7
Default int = 1
Default int = 1
)

0 comments on commit 3606cbb

Please sign in to comment.