How to Manipulate the LSB of Each Byte in a 24-Bit BMP Image?

  • Thread starter Thread starter hadi amiri 4
  • Start date Start date
  • Tags Tags
    Picture
Click For Summary

Discussion Overview

The discussion revolves around manipulating the least significant bit (LSB) of each byte in a 24-bit BMP image file. Participants explore how to properly handle BMP file structures, including headers and pixel data, and the appropriate methods for reading and modifying the file in a programming context.

Discussion Character

  • Technical explanation
  • Homework-related
  • Debate/contested

Main Points Raised

  • One participant inquires about treating a BMP file as an array of integers and whether to define a structure for the different parts of the BMP file, such as headers.
  • Another participant emphasizes that a BMP file is a binary file and cautions against using an array of integers due to potential misalignment with the data structure.
  • Concerns are raised about the complexity of existing code examples found online, with a request for clarification on how to open and read BMP files in a program.
  • Some participants suggest that a basic understanding of programming is necessary to manipulate BMP files effectively.
  • A participant shares a code snippet for reading BMP headers and pixel data, noting that it is untested and may lead to questions.
  • Another participant expresses gratitude for the code and indicates a willingness to study it before asking further questions.
  • One participant presents a separate code example for reading BMP file headers and pixel data, questioning the correctness of their implementation.

Areas of Agreement / Disagreement

There is no consensus on the best approach to manipulate BMP files, with participants expressing varying levels of understanding and different methods for handling the file structure. Some participants agree on the necessity of treating the file as binary, while others highlight the complexity of the task for beginners.

Contextual Notes

Participants mention the need for knowledge of file structures and programming concepts, indicating that the discussion may involve unresolved technical details and assumptions about the participants' programming backgrounds.

hadi amiri 4
Messages
98
Reaction score
1
Hello every body :
i am very new to programming ,i am trying to write a programme that will work on the LSB of each byte in a 24 bit picture.
now my question is how to treat a .bmp file ,i mean can i consider it as an array of integers with leth (for example)800x600.
and start to changing the LSB of bytes after a specific element (0x36 where the image datas strat) or i should define some structure for keeping the different parts of the bmp like header and...

or i should treat it as a binary file or a text file?
 
Technology news on Phys.org
A .bmp file is a binary file, not a text file. And you don't want an array of integers, since they won't align with the data in your file, which is 24 bits/pixel, probably with 8 bits each for red, green, and blue. The int data type in C is typically 32 bits, but might be 16 bits for an older compiler.
 
I know the internal structure of .bmp and i know that it is made of headers which contains information about the .bmp like width,height ,bitdepth.
but my problem is that how to open it into my program .i have seen lots of codes on the internet that are named for example "bitmaploader" or thing like this which are concerned with complicated structures and use of I/O functions which are far from the contents of what i have learned .my knowledge of programming is very little and i got confused when i go through them can anyone give me some make it more clear for me .
 
Sorry, these are not things that can be done without some knowledge. If you don't understand code that you see, you probably need to learn more before trying.

You have to treat the file as binary, open it, read part of the content into header structure, then the rest into allocated memory.
 
in page http://en.wikipedia.org/wiki/BMP_file_format" at the bottom ther is a table which draws the contents of the a 2x2 bmp pictures
the are in 0x :42 4D 46 00 00 00 00 00 36 00 ...00 FF 00 00 00
so they are separate numbers stored in bytes,and if i (for example) want to modify the LSB of pixels
i start to modifying from the 0x36th byte is it legal?
 
Last edited by a moderator:
Borek said:
Sorry, these are not things that can be done without some knowledge. If you don't understand code that you see, you probably need to learn more before trying.

You have to treat the file as binary, open it, read part of the content into header structure, then the rest into allocated memory.

Dear Borek
Can you translate the last sentence into C language i would be very thankful:blushing:
 
This is your lucky day, I found something almost ready on my disk. This can be my unlucky day, as I guess posting it I risk of being bombarded with tons of questions about the code. So here goes disclaimer - this is part of an old project, part that I have not tested now. Basically I am posting it 'as is' - I can't guarantee it will work nor I I can't guarantee I will answer any questions you may ask.

Code:
#include "stdafx.h"

int main(int argc, char* argv[])
{
	FILE *f;
	BITMAPINFOHEADER bmphdr;
	BITMAPFILEHEADER bmpfhdr;
	char *pBMP,*pch;
	int iBW;

// open file
	if ((f = fopen("name.bmp","rb")) == NULL) return -1;
// read headers
	fread(&bmpfhdr,sizeof(bmpfhdr),1,f);
	fread(&bmphdr,sizeof(bmphdr),1,f);
// width of the picture as stored in memory - note, it works only for RGB pictures
	iBW = (3*bmphdr.biWidth + 3) & (0xFFFFFFFF ^ 0x3);

// allocate memory for image data
	pBMP = new char[iBW*bmphdr.biHeight];
// read image data
	fread(pBMP,sizeof(char),iBW*bmphdr.biHeight,f);

// calculate pointer to point at coordinates (100,100)
	int x = 100, y = 100; 
	pch = pBMP + 3*x + (bmphdr.biHeight-y-1)*iBW;

	fclose(f);

	delete pBMP;

	return 0;
}

To save file to disk write same data in the same order to file.

Note that you may expect problems with signed/unsigned char when trying to modify picture in memory, I don't remember what were compiler settings at the time.
 
Thank you Borek for this piece of code.i will go to implement it to what i want.and don't worry i will not bombard you with questions until i read it (the codes)carefully and check my courses stuff.but be ready !
 
  • #10
Code:
what is wrong with this code?

typedef unsigned long DWORD;
typedef unsigned short WORD;


typedef struct {
    int Signature;
	DWORD Size;
	DWORD Reserved;
	DWORD BitsOffset;
} BITMAP_FILEHEADER;


typedef struct {
	DWORD HeaderSize;
	DWORD Width;
	DWORD Height;
	WORD Planes;
	WORD BitCount;
	DWORD Compression;
	DWORD SizeImage;
	DWORD PelsPerMeterX;
	DWORD PelsPerMeterY;
	DWORD ClrUsed;
	DWORD ClrImportant;

} BITMAP_HEADER;

#include <stdio.h>
main()
{
    FILE * fp;
    BITMAP_FILEHEADER bmpheader;
    BITMAP_HEADER bmpinfheader;
    int raw_data_size;
    char *pixel;

    //openning file
    fp=fopen("c:\\pic.bmp","rb");

    /*if(fp==NULL){
        printf("error in opennig file");
        exit(1);
    }*/



    fread(&bmpheader,sizeof(bmpheader),1,fp);//reads the header
    fread(&bmpinfheader,sizeof(bmpinfheader),1,fp);//reads the info header

    raw_data_size=bmpheader.SizeImage;//stores the size of raw bitmaps

    pixel=new char [raw_data_size];//create an array for holding the pixels

    fread(pixel,sizeof(char),raw_data_size,fp);//reads them from memory to aray

    printf("%x",bmpheader.Signature);//prints 424D

    fclose(fp);
    return 0;
}
 

Similar threads

  • · Replies 8 ·
Replies
8
Views
3K
  • · Replies 4 ·
Replies
4
Views
2K
  • · Replies 3 ·
Replies
3
Views
5K
  • · Replies 33 ·
2
Replies
33
Views
5K
Replies
1
Views
2K
  • · Replies 4 ·
Replies
4
Views
3K
Replies
1
Views
14K
  • · Replies 6 ·
Replies
6
Views
3K
  • · Replies 4 ·
Replies
4
Views
16K
  • · Replies 3 ·
Replies
3
Views
4K