diff --git a/README.md b/README.md index 7aaeff6..2b09f30 100644 --- a/README.md +++ b/README.md @@ -1,2 +1,45 @@ # Bitmap-Image-Fatma-Ebrahim -a small Go program that can read a bitmap image file and extract its dimensions (height and width). +This repository contains an implementation of a small Go program that can read a bitmap image file and extract its dimensions (height and width). + +## Features: +- Reads bitmap image and extracts its dimensions +- Accept the image file path as a command-line argument + + +## How to Use: + +### Step 1: Install the library using `go get` + + ```bash + go get github.com/codescalersinternships/Bitmap-Image-Fatma-Ebrahim + ``` + +This command fetches the library and adds it to your project's `go.mod` file. + +### Step 2: Import and use the library in your code + + After running `go get`, you can import the library into your project and use the function as follows: + ``` + package main + +import ( + "fmt" + + bitmap "github.com/codescalersinternships/Bitmap-Image-Fatma-Ebrahim/pkg" +) + +func main() { + res,err := bitmap.GetDim("sample.bmp") + if err != nil { + fmt.Println(err) + return + } + fmt.Printf("Bitmap image width: %d, height: %d\n", res.Width, res.Height) + +} + ``` + +Also, you can specify the image file path via command-line argument as follows: +``` +go run main.go sample.bmp +``` \ No newline at end of file diff --git a/cmd/main.go b/cmd/main.go new file mode 100644 index 0000000..5bfd9ab --- /dev/null +++ b/cmd/main.go @@ -0,0 +1,17 @@ +package main + +import ( + "fmt" + + bitmap "github.com/codescalersinternships/Bitmap-Image-Fatma-Ebrahim/pkg" +) + +func main() { + res,err := bitmap.GetDim() + if err != nil { + fmt.Println(err) + return + } + fmt.Printf("Bitmap image width: %d, height: %d\n", res.Width, res.Height) + +} \ No newline at end of file diff --git a/go.mod b/go.mod new file mode 100644 index 0000000..030fd8a --- /dev/null +++ b/go.mod @@ -0,0 +1,3 @@ +module github.com/codescalersinternships/Bitmap-Image-Fatma-Ebrahim + +go 1.25.1 diff --git a/pkg/bitmap.go b/pkg/bitmap.go new file mode 100644 index 0000000..1d681e6 --- /dev/null +++ b/pkg/bitmap.go @@ -0,0 +1,52 @@ +package bitmap + +import ( + "encoding/binary" + "flag" + "fmt" + "os" +) + +type bitmap struct { + Width int + Height int +} + + +func GetDim(paths ...string) (bitmap, error) { + var path string + if len(paths) == 0 { + path = handleCLArgs() + } else{ + path = paths[0] + } + + img, err := os.ReadFile(path) + bitmapImage := bitmap{} + if err != nil { + return bitmapImage, err + } + + headerOffset :=14 + headerSize := 12 + header:= img [headerOffset: headerOffset + headerSize] + + widthOffset := 4 + size:= 4 + heightOffset := widthOffset + size + + width:= binary.LittleEndian.Uint16(header[widthOffset:widthOffset+size]) + height := binary.LittleEndian.Uint16(header[heightOffset:heightOffset+size]) + bitmapImage.Width = int(width) + bitmapImage.Height = int(height) + + fmt.Printf("Bitmap image width: %d, height: %d\n", bitmapImage.Width, bitmapImage.Height) + + return bitmapImage, nil +} + +func handleCLArgs() string { + flag.Parse() + path := flag.Arg(0) + return path +} diff --git a/sample.bmp b/sample.bmp new file mode 100644 index 0000000..0db1a57 Binary files /dev/null and b/sample.bmp differ