Salut à tous ! N'ayant pas pus résoudre mon prblème seul, je m'en réfère à vous !
Je m'explique, je cherche à écrire un fichier BMP à l'ancienne, c'est à dire sans aucune librairie.
Je suis presque au bout de mon programme mais lorsque je lui demande de dessiner un pixel, il dessine tout les ligne horizontale. A quoi est-ce du ?
Voici mon code :
#include <iostream.h>
#include <stdlib.h>
#include <windows.h>
#include <stdio.h>
#include <math.h>
// Structs
typedef struct{
unsigned char Red;
unsigned char Green;
unsigned char Blue;
}Color;
typedef struct
{
Color* ImgArray;
char* Filename;
int iWidth;
int iHeight;
FILE* FS;
}Picture;
void InitBmpFile(FILE *FS,unsigned int iWidth,unsigned int iHeight);
Picture BuildPicture(char* Filename,unsigned int iWidth,unsigned int iHeight)
{
Picture TmpPic;
TmpPic.Filename = Filename;
TmpPic.FS = fopen(Filename,"wb");
TmpPic.iWidth = iWidth;
TmpPic.iHeight = iHeight;
TmpPic.ImgArray = (Color*)malloc((sizeof(Color)*iWidth) + (sizeof(Color)*iHeight));
InitBmpFile(TmpPic.FS,iWidth,iHeight);
return TmpPic;
}
void InitBmpFile(FILE *FS,unsigned int iWidth,unsigned int iHeight)
{
const char *BMFormat = "BM";
unsigned int BMPHeaderLenght = (iWidth*iHeight*3) + 54;
unsigned int ReservedBit = 0;
unsigned int DataOffset = 54;
unsigned int DataHeaderLenght = 40;
unsigned short Planes = 1;
unsigned short BPP = 24;
unsigned int Compression = 0;
unsigned int ImageSize = 0;//(iWidth*iHeight*3);
unsigned int ResX = 0;
unsigned int Resy = 0;
unsigned int ColorUsed =0;
unsigned int ColorImportant = 0;
fwrite(BMFormat,1,2,FS);
fwrite(&BMPHeaderLenght,4,1,FS);
fwrite(&ReservedBit,4,1,FS);
fwrite(&DataOffset,4,1,FS);
fwrite(&DataHeaderLenght,4,1,FS);
fwrite(&iWidth,4,1,FS);
fwrite(&iHeight,4,1,FS);
fwrite(&Planes,2,1,FS);
fwrite(&BPP,2,1,FS);
fwrite(&Compression,4,1,FS);
fwrite(&ImageSize,4,1,FS);
fwrite(&ResX,4,1,FS);
fwrite(&Resy,4,1,FS);
fwrite(&ColorUsed,4,1,FS);
fwrite(&ColorImportant,4,1,FS);
}
void Save(Picture Pic)
{
for(int Y=Pic.iHeight;Y>0;Y--)
{
for(int X=0;X<Pic.iWidth;X++)
{
fwrite(&(Pic.ImgArray[X,Y].Blue),1,1,Pic.FS);
fwrite(&(Pic.ImgArray[X,Y].Green),1,1,Pic.FS);
fwrite(&(Pic.ImgArray[X,Y].Red),1,1,Pic.FS);
}
}
}
void WritePixel(Picture Pic,int X,int Y,Color PixelColor)
{
Pic.ImgArray[X,Y] = PixelColor;
}
Color GetColor(unsigned char Red,unsigned unsigned Green,unsigned unsigned Blue)
{
Color TmpColor;
TmpColor.Red = Red;
TmpColor.Green = Green;
TmpColor.Blue = Blue;
return TmpColor;
}
void Close(Picture Pic)
{
fclose(Pic.FS);
}
Pour l'utiliser :
#include "stdafx.h"
#include "BitmapWriterLib.h"
int main(int argc, char* argv[])
{
Picture MyPic= BuildPicture("\\\\galileo\\gaudin.m\\Mes documents\\TestFile.bmp",800,600);
for(int X=0;X<MyPic.iWidth;X++)
{
for(int Y=0;Y<MyPic.iHeight;Y++)
{
WritePixel(MyPic,X,Y,GetColor(255,255,255));
}
}
WritePixel(MyPic,400,300,GetColor(255,0,0));
Save(MyPic);
Close(MyPic);
return 0;
}
MLerci d'avance !
Tchou !
-=Ar$£nik=-