Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
45 changes: 44 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
@@ -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
```
17 changes: 17 additions & 0 deletions cmd/main.go
Original file line number Diff line number Diff line change
@@ -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)

}
3 changes: 3 additions & 0 deletions go.mod
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
module github.com/codescalersinternships/Bitmap-Image-Fatma-Ebrahim

go 1.25.1
52 changes: 52 additions & 0 deletions pkg/bitmap.go
Original file line number Diff line number Diff line change
@@ -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
}
Binary file added sample.bmp
Binary file not shown.