begin process at 2012 02 08 23:16:01
  Trouver un code source :
 
dans
 
Accueil > 

Code

 > 

Graphique

 > ASCII ART : BMP TO HTM - TRANSFORME UNE IMAGE EN FICHIER HTM

ASCII ART : BMP TO HTM - TRANSFORME UNE IMAGE EN FICHIER HTM


 Information sur la source

Note :
8,83 / 10 - par 6 personnes
8,83 / 10

  • 1

  • 2

  • 3

  • 4

  • 5

  • 6

  • 7

  • 8

  • 9

  • 10
Catégorie :Graphique Niveau :Débutant Date de création :29/07/2004 Date de mise à jour :30/07/2004 16:54:57 Vu / téléchargé :4 994 / 271

Auteur : Maegis

Ecrire un message privé
Commentaire sur cette source (17)
Ajouter un commentaire et/ou une note

 Description

Cliquez pour voir la capture en taille normale
Ce code est dans le même genre que mon précedent code Bitmap24 to Text
Il permet de convertir un fichier BMP 24 bit en un fichier htm en gérant les couleurs
Les pixels sont remplacés par une alternance de 0 et de 1 dans le fichier htm
On peut aussi spécifier la taille de la police et spécifier la "résolution" en caractères du fichier htm qui sera généré : une fonction de scale de l'image est incluse.

Note :
Sur le screen, l'image générée parait terme (en particulier le rouge), mais c'est dû à la compression en jpeg (ayant un fond noir et les caractères étant petits, il y a de fortes variations sur de petits intervalles et la compression jpeg altere l'image originale)
Le fichier htm généré est fidèle au niveau des couleurs

Source

  • #include <windows.h>
  • #include <stdio.h>
  • #include "resource.h"
  • #define PARTDEC(a) (a-(double)(int)a)
  • BOOL CALLBACK DlgProc(HWND,UINT,WPARAM,LPARAM);
  • struct PIXEL
  • {
  • BYTE b;
  • BYTE g;
  • BYTE r;
  • };
  • /*--------------------------------*/
  • //Change la resolution d'une image
  • /*--------------------------------*/
  • void Scale(const PIXEL* src,DWORD srcwidth,DWORD srcheight,PIXEL* data,DWORD datawidth,DWORD dataheight,BOOL moyenne_etirement)
  • {
  • DWORD i,j,k,l;
  • PIXEL* save_data;
  • double xratio,yratio;
  • double precx = 0.0,precy = 0.0;
  • double currentx,currenty;
  • double coeff; //somme des coeffs pour la moyenne
  • double temp;
  • double somme_r,somme_g,somme_b;
  • xratio = (double)srcwidth/(double)datawidth;
  • yratio = (double)srcheight/(double)dataheight;
  • save_data = data;
  • currenty = 0.0;
  • for(j=0;j<dataheight;j++) //Pour chaque pixel du but
  • {
  • currentx = 0.0;
  • currenty += yratio;
  • for(i=0;i<datawidth;i++)
  • {
  • currentx += xratio;
  • coeff = 0.0;
  • somme_r = 0.0;
  • somme_g = 0.0;
  • somme_b = 0.0;
  • //Calcul de la moyenne
  • for(l=0;l<(unsigned int)yratio+1;l++) //Pour chaque pixel de la source contenu dans le pixel du but
  • {
  • for(k=0;k<(unsigned int)xratio+1;k++)
  • {
  • //Coeff en x
  • if (k==0)
  • temp = 1.0-PARTDEC(precx);
  • else if (k==(unsigned int)xratio)
  • temp = xratio-(double)((int)xratio-1)-(1.0-PARTDEC(precx));
  • else
  • temp = 1;
  • //Coeff en y
  • if (l==0)
  • temp = temp*(1.0-PARTDEC(precy));
  • else if (l==(unsigned int)yratio)
  • temp = temp*(yratio-(double)((int)yratio-1)-(1.0-PARTDEC(precy)));
  • if ((int)precx+k<srcwidth && (int)precy+l<srcheight) //si on est dans les limites
  • {
  • somme_r += src[(int)precx+k+((int)precy+l)*srcwidth].r*temp;
  • somme_g += src[(int)precx+k+((int)precy+l)*srcwidth].g*temp;
  • somme_b += src[(int)precx+k+((int)precy+l)*srcwidth].b*temp;
  • coeff += temp;
  • }
  • if (moyenne_etirement) //Moyenne lors de l'etirement si souhaité
  • {
  • if (xratio<1. && (int)currentx-(int)precx==1 && PARTDEC(currentx) != 0.)
  • {
  • temp = PARTDEC(currentx);
  • somme_r += src[(int)precx+1 + (int)precy*srcwidth].r*temp;
  • somme_g += src[(int)precx+1 + (int)precy*srcwidth].g*temp;
  • somme_b += src[(int)precx+1 + (int)precy*srcwidth].b*temp;
  • coeff += temp;
  • temp = -1.0;
  • }
  • if (yratio<1. && (int)currenty-(int)precy==1 && PARTDEC(currenty) != 0.)
  • {
  • if (temp == -1.0) //on compte le quatrieme carré
  • {
  • temp = PARTDEC(currenty)*PARTDEC(currentx);
  • somme_r += src[(int)precx+1 + ((int)(precy)+1)*srcwidth].r*temp;
  • somme_g += src[(int)precx+1 + ((int)(precy)+1)*srcwidth].g*temp;
  • somme_b += src[(int)precx+1 + ((int)(precy)+1)*srcwidth].b*temp;
  • coeff += temp;
  • }
  • temp = PARTDEC(currenty);
  • somme_r += src[(int)precx + ((int)(precy)+1)*srcwidth].r*temp;
  • somme_g += src[(int)precx + ((int)(precy)+1)*srcwidth].g*temp;
  • somme_b += src[(int)precx + ((int)(precy)+1)*srcwidth].b*temp;
  • coeff += temp;
  • }
  • }
  • }
  • }
  • data->r = (BYTE)(somme_r/coeff);
  • data->g = (BYTE)(somme_g/coeff);
  • data->b = (BYTE)(somme_b/coeff);
  • data++;
  • precx = currentx;
  • }
  • precy = currenty;
  • precx = 0.0;
  • }
  • data = save_data;
  • }
  • /*---------------------*/
  • //Ouvre un bmp 24 bits
  • /*---------------------*/
  • BOOL GetBmp24(char* chemin,PIXEL **data,DWORD& width,DWORD& height)
  • {
  • BITMAPFILEHEADER fileheader;
  • BITMAPINFOHEADER infoheader;
  • HANDLE fichier;
  • DWORD dummy,i,j;
  • DWORD size;
  • DWORD bourrage = 0;
  • PIXEL temp;
  • BYTE *buffer = NULL;
  • fichier = CreateFile(chemin,
  • GENERIC_READ,
  • FILE_SHARE_READ,
  • NULL,
  • OPEN_EXISTING,
  • FILE_ATTRIBUTE_NORMAL,
  • NULL);
  • if (fichier == INVALID_HANDLE_VALUE)
  • return FALSE;
  • ReadFile(fichier,&fileheader,14,&dummy,NULL);
  • ReadFile(fichier,&infoheader,40,&dummy,NULL);
  • width = infoheader.biWidth;
  • height = infoheader.biHeight;
  • while ((3*width+bourrage) % 4 != 0) //gestion du bourrage
  • bourrage++;
  • if (fileheader.bfType != 0x4D42 || infoheader.biBitCount != 24
  • || infoheader.biCompression != 0)
  • {
  • CloseHandle(fichier);
  • return FALSE;
  • }
  • size = width*height;
  • buffer = new BYTE[size*3+bourrage*height];
  • if (buffer == NULL)
  • return FALSE;
  • SetFilePointer(fichier,fileheader.bfOffBits,NULL,FILE_BEGIN);
  • ReadFile(fichier,buffer,size*3+bourrage*height,&dummy,NULL);
  • (*data) = new PIXEL[size];
  • if ((*data) == NULL)
  • {
  • delete[] buffer;
  • return FALSE;
  • }
  • for(i=0;i<height;i++)
  • {
  • memcpy((*data)+(i*width),buffer+i*(3*width+bourrage),width*3);
  • }
  • //remet dans le bon sens
  • for(i=0;i<(height/2);i++)
  • {
  • for(j=0;j<width;j++)
  • {
  • temp = (*data)[i*width+j];
  • (*data)[i*width+j] = (*data)[(height-1-i)*width+j];
  • (*data)[(height-1-i)*width+j] = temp;
  • }
  • }
  • CloseHandle(fichier);
  • delete[] buffer;
  • return TRUE;
  • }
  • /*------------------------------*/
  • //Enregistre les données en Htm
  • /*------------------------------*/
  • BOOL SaveHtm(char *chemin,const PIXEL *data,DWORD width,DWORD height,char size)
  • {
  • HANDLE fichier;
  • char* buffer = NULL;
  • char temp[30];
  • DWORD i,j;
  • char valeur = '0';
  • DWORD cur = 0;
  • PIXEL prev = {0,0,0};
  • BOOL red = FALSE;
  • buffer = new char[80+2*height+28*height*width];
  • if (buffer == NULL)
  • return FALSE;
  • sprintf(buffer,"<HTML><HEAD><BODY bgColor=#000000><PRE><FONT size=%d>",size);
  • cur += 53;
  • if (data[0].r == 0 && data[0].g == 0 && data[0].b == 0)
  • {
  • sprintf(temp,
  • "<FONT color=#%.2x%.2x%.2x>",
  • data[0].r,
  • data[0].g,
  • data[0].b );
  • memcpy(buffer+cur,temp,20);
  • cur += 20;
  • }
  • for(i=0;i<height;i++)
  • {
  • for(j=0;j<width;j++)
  • {
  • if (prev.r == data[i*width+j].r && prev.g == data[i*width+j].g && prev.b == data[i*width+j].b)
  • {
  • memcpy(buffer+cur,&valeur,1);
  • cur++;
  • red = TRUE;
  • }else
  • {
  • if (red == TRUE) //si la couleur du suivant n'est pas la meme que le precedent
  • {
  • memcpy(buffer+cur,"</FONT>",7);
  • cur += 7;
  • red = FALSE;
  • }
  • prev.r = data[i*width+j].r;
  • prev.g = data[i*width+j].g;
  • prev.b = data[i*width+j].b;
  • sprintf(temp,
  • "<FONT color=#%.2x%.2x%.2x>%c",
  • prev.r,
  • prev.g,
  • prev.b,
  • valeur);
  • memcpy(buffer+cur,temp,21);
  • cur += 21;
  • }
  • if (valeur =='0')
  • valeur = '1';
  • else
  • valeur = '0';
  • }
  • if(i==height-1)
  • {
  • memcpy(buffer+cur,"</FONT>",7);
  • cur+=7;
  • }
  • memcpy(buffer+cur,"\n",1);
  • cur += 1;
  • }
  • memcpy(buffer+cur,"</PRE></FONT></BODY></HTML>",27);
  • cur +=27;
  • fichier = CreateFile(chemin,
  • GENERIC_WRITE,
  • FILE_SHARE_READ,
  • NULL,
  • CREATE_ALWAYS,
  • FILE_ATTRIBUTE_NORMAL,
  • NULL);
  • if (fichier != INVALID_HANDLE_VALUE)
  • {
  • WriteFile(fichier,buffer,cur,&i,NULL);
  • CloseHandle(fichier);
  • delete buffer;
  • return TRUE;
  • }
  • delete[] buffer;
  • return FALSE;
  • }
  • /*--------------*/
  • //Fonction Main
  • /*--------------*/
  • int APIENTRY WinMain(HINSTANCE hInstance,
  • HINSTANCE hPrevInstance,
  • LPSTR lpCmdLine,
  • int nShowCmd)
  • {
  • HWND hWndDlg;
  • MSG msg;
  • hWndDlg = CreateDialog(hInstance,MAKEINTRESOURCE(IDD_DIALOG),NULL,&DlgProc);//Crée la boite de
  • //dialogue
  • ShowWindow(hWndDlg,SW_SHOW); //Affiche la bdd
  • while(GetMessage(&msg,NULL,0,0))
  • {
  • TranslateMessage(&msg); //Boucle de messages
  • DispatchMessage(&msg);
  • }
  • return 0;
  • }
  • /*----------------------------------*/
  • //Traitement des messages de la bdd
  • /*----------------------------------*/
  • BOOL CALLBACK DlgProc(HWND hWnd,UINT uMsg,WPARAM wParam,LPARAM lParam)
  • {
  • int bouton; //en cas de WM_COMMAND
  • switch(uMsg)
  • {
  • case WM_SYSCOMMAND : //si on clique sur la croix
  • if (wParam == SC_CLOSE) //on ferme la fenetre
  • PostQuitMessage(0);
  • return TRUE;
  • case WM_INITDIALOG : //a la création de la boite de dialogue
  • SetDlgItemText(hWnd,IDC_FICHIERSOURCE,"Aucun fichier selectionné");
  • SetDlgItemText(hWnd,IDC_FICHIERDEST,"Aucun fichier selectionné");
  • SetDlgItemInt(hWnd,IDC_TEXTSIZE,2,FALSE);
  • return TRUE;
  • case WM_COMMAND :
  • bouton = LOWORD(wParam); //Le bouton envoyant le message
  • switch(bouton)
  • {
  • case IDC_QUIT :
  • PostQuitMessage(0);
  • return TRUE;
  • case IDC_SOURCE :
  • char cheminsource[_MAX_FNAME]; //va contenir le chemin du fichier source
  • OPENFILENAME ofn; //structure pour la bdd ouvrir
  • memset(&ofn,0,sizeof(OPENFILENAME)); //on la set à 0
  • ofn.lStructSize = sizeof(OPENFILENAME);
  • ofn.hwndOwner = hWnd; //Handle de la fenetre parente ou NULL
  • ofn.hInstance = NULL;
  • ofn.lpstrFilter = "Fichier BMP 24bits (*.bmp)\0*.bmp\0";
  • ofn.lpstrCustomFilter = NULL;
  • ofn.nMaxCustFilter = NULL;
  • ofn.nFilterIndex = 0; //index du filtre par defaut
  • ofn.lpstrFile = cheminsource; //votre buffer de fichier
  • ofn.nMaxFile = _MAX_FNAME;
  • ofn.lpstrFileTitle = NULL; //buffer contenant le nom du fichier.ext
  • ofn.lpstrInitialDir = NULL; //dir initial;
  • ofn.lpstrTitle = "Selectionnez le bitmap"; //titre de la BDG
  • ofn.Flags = OFN_FILEMUSTEXIST; //Selectionner seulement un fichier qui existe
  • ofn.lCustData = NULL;
  • ofn.lpfnHook = NULL;
  • ofn.lpTemplateName = NULL;
  • cheminsource[0] = 0;
  • if(GetOpenFileName(&ofn) != 0) //Si pas de probleme
  • {
  • SetDlgItemText(hWnd,IDC_FICHIERSOURCE,cheminsource); //mettre le nom du
  • //fichier dans la edit box
  • }
  • return TRUE;
  • case IDC_DEST :
  • char chemindest[_MAX_FNAME]; //va contenir le chemin du fichier destination
  • char *point;
  • GetDlgItemText(hWnd,IDC_FICHIERSOURCE,chemindest,_MAX_FNAME);
  • point = strstr(chemindest,".");
  • if (point != NULL)
  • strcpy(point,".htm\0");
  • else
  • ZeroMemory(chemindest,_MAX_FNAME);
  • OPENFILENAME ofn2; //structure pour la bdg ouvrir
  • memset(&ofn2,0,sizeof(OPENFILENAME)); //on la set à 0
  • ofn2.lStructSize = sizeof(OPENFILENAME);
  • ofn2.hwndOwner = hWnd; //Handle de la fenetre parente ou NULL
  • ofn2.hInstance = NULL;
  • ofn2.lpstrFilter = "Fichier htm (*.htm)\0*.htm\0";
  • ofn2.lpstrCustomFilter = NULL;
  • ofn2.nMaxCustFilter = NULL;
  • ofn2.nFilterIndex = 0; //index du filtre par defaut
  • ofn2.lpstrFile = chemindest; //votre buffer de fichier
  • ofn2.nMaxFile = _MAX_FNAME;
  • ofn2.lpstrFileTitle = NULL; //buffer contenant le nom du fichier.ext
  • ofn2.lpstrInitialDir = NULL; //dir initial;
  • ofn2.lpstrTitle = "Selectionnez le fichier destination"; //titre de la BDD
  • ofn2.Flags = OFN_PATHMUSTEXIST;
  • ofn2.lCustData = NULL;
  • ofn2.lpfnHook = NULL;
  • ofn2.lpTemplateName = NULL;
  • if(GetSaveFileName(&ofn2) != 0) //Si pas de probleme
  • SetDlgItemText(hWnd,IDC_FICHIERDEST,chemindest); //mettre le nom du
  • //fichier dans la edit box
  • return TRUE;
  • case IDC_PROCESS:
  • char pathdest[_MAX_FNAME]; //chemin du fichier de dest
  • char pathsrc[_MAX_FNAME];
  • DWORD height,width;
  • PIXEL *data = NULL;
  • PIXEL *scaleddata;
  • int xresol,yresol;
  • GetDlgItemText(hWnd,IDC_FICHIERDEST,pathdest,_MAX_FNAME); //obtient les paths
  • GetDlgItemText(hWnd,IDC_FICHIERSOURCE,pathsrc,_MAX_FNAME);
  • if (strcmp(pathsrc,"Aucun fichier selectionné") == 0) //si pas fichier src
  • {
  • MessageBox(hWnd,"Selectionne d'abord le fichier source!","Erreur fichier",MB_OK | MB_ICONEXCLAMATION);
  • return TRUE;
  • }
  • if (strcmp(pathdest,"Aucun fichier selectionné") == 0) //si pas fichier dest
  • {
  • MessageBox(hWnd,"Selectionne d'abord le fichier de destination!","Erreur fichier",MB_OK | MB_ICONEXCLAMATION);
  • return TRUE;
  • }
  • xresol = GetDlgItemInt(hWnd,IDC_X,NULL,FALSE);
  • yresol = GetDlgItemInt(hWnd,IDC_Y,NULL,FALSE);
  • if(GetBmp24(pathsrc,&data,width,height))
  • {
  • if (xresol != 0 && yresol !=0) //Gestion du scale
  • {
  • scaleddata = new PIXEL[xresol*yresol];
  • if (scaleddata != NULL)
  • {
  • Scale(data,width,height,scaleddata,xresol,yresol,FALSE);
  • delete[] data;
  • data = scaleddata;
  • width = xresol;
  • height = yresol;
  • }
  • }
  • if(SaveHtm(pathdest,data,width,height,GetDlgItemInt(hWnd,IDC_TEXTSIZE,NULL,FALSE)))
  • MessageBox(hWnd,"Opération réussie","Bitmap24ToHtm",MB_OK | MB_ICONEXCLAMATION);
  • else
  • MessageBox(hWnd,"Erreur à l'écriture du fichier de destination","Bitmap24ToHtm",MB_OK | MB_ICONEXCLAMATION);
  • delete[] data;
  • }
  • else
  • MessageBox(hWnd,"Erreur a l'ouverture du fichier source\nVerifiez que le fichier soit bien un bitmap 24bits","Bitmap24ToHtm",MB_OK | MB_ICONEXCLAMATION);
  • return TRUE;
  • }
  • if (HIWORD(wParam) == EN_KILLFOCUS && LOWORD(wParam) == IDC_TEXTSIZE)
  • {
  • int size = GetDlgItemInt(hWnd,IDC_TEXTSIZE,NULL,FALSE);
  • if (size<1 || size >7)
  • SetDlgItemInt(hWnd,IDC_TEXTSIZE,2,FALSE);
  • return TRUE;
  • }
  • return FALSE;
  • }
  • return FALSE;
  • }
#include <windows.h>
#include <stdio.h>
#include "resource.h"

#define PARTDEC(a) (a-(double)(int)a)

BOOL CALLBACK DlgProc(HWND,UINT,WPARAM,LPARAM);


struct PIXEL
{
	BYTE b;
	BYTE g;
	BYTE r;
};


/*--------------------------------*/
//Change la resolution d'une image
/*--------------------------------*/
void Scale(const PIXEL* src,DWORD srcwidth,DWORD srcheight,PIXEL* data,DWORD datawidth,DWORD dataheight,BOOL moyenne_etirement)
{
	DWORD	i,j,k,l;
	PIXEL*	save_data; 
	double	xratio,yratio;
	double	precx = 0.0,precy = 0.0;
	double	currentx,currenty;
	double	coeff;						//somme des coeffs pour la moyenne
	double	temp;
	double	somme_r,somme_g,somme_b;

	xratio = (double)srcwidth/(double)datawidth;
	yratio = (double)srcheight/(double)dataheight;
	save_data = data;
	currenty = 0.0;

	for(j=0;j<dataheight;j++)			//Pour chaque pixel du but
	{
		currentx = 0.0;
		currenty += yratio;
		for(i=0;i<datawidth;i++)
		{
			currentx += xratio;
			coeff = 0.0;
			somme_r = 0.0;
			somme_g = 0.0;
			somme_b = 0.0;
		
			//Calcul de la moyenne
			for(l=0;l<(unsigned int)yratio+1;l++)		//Pour chaque pixel de la source contenu dans le pixel du but
			{
				for(k=0;k<(unsigned int)xratio+1;k++)
				{
				//Coeff en x
					if (k==0)
						temp = 1.0-PARTDEC(precx);
					else if (k==(unsigned int)xratio)
						temp = xratio-(double)((int)xratio-1)-(1.0-PARTDEC(precx));
					else
						temp = 1;
				//Coeff en y
					if (l==0)
						temp = temp*(1.0-PARTDEC(precy));
					else if (l==(unsigned int)yratio)
						temp = temp*(yratio-(double)((int)yratio-1)-(1.0-PARTDEC(precy)));

					if ((int)precx+k<srcwidth && (int)precy+l<srcheight)		//si on est dans les limites
					{
						somme_r += src[(int)precx+k+((int)precy+l)*srcwidth].r*temp;
						somme_g += src[(int)precx+k+((int)precy+l)*srcwidth].g*temp;
						somme_b += src[(int)precx+k+((int)precy+l)*srcwidth].b*temp;
						coeff += temp;
					}

					if (moyenne_etirement)  //Moyenne lors de l'etirement si souhaité
					{
						if (xratio<1. && (int)currentx-(int)precx==1 && PARTDEC(currentx) != 0.)
						{
							temp = PARTDEC(currentx);
							somme_r += src[(int)precx+1 + (int)precy*srcwidth].r*temp;
							somme_g += src[(int)precx+1 + (int)precy*srcwidth].g*temp;
							somme_b += src[(int)precx+1 + (int)precy*srcwidth].b*temp;
							coeff += temp;
							temp = -1.0;
						}
						if (yratio<1. && (int)currenty-(int)precy==1 && PARTDEC(currenty) != 0.)
						{
							if (temp == -1.0)		//on compte le quatrieme carré
							{
								temp = PARTDEC(currenty)*PARTDEC(currentx);
								somme_r += src[(int)precx+1 + ((int)(precy)+1)*srcwidth].r*temp;
								somme_g += src[(int)precx+1 + ((int)(precy)+1)*srcwidth].g*temp;
								somme_b += src[(int)precx+1 + ((int)(precy)+1)*srcwidth].b*temp;
								coeff += temp;
							}
							temp = PARTDEC(currenty);
							somme_r += src[(int)precx + ((int)(precy)+1)*srcwidth].r*temp;
							somme_g += src[(int)precx + ((int)(precy)+1)*srcwidth].g*temp;
							somme_b += src[(int)precx + ((int)(precy)+1)*srcwidth].b*temp;
							coeff += temp;
						}
					}
				}
			}
			data->r = (BYTE)(somme_r/coeff);
			data->g = (BYTE)(somme_g/coeff);
			data->b = (BYTE)(somme_b/coeff);
			data++;
			precx = currentx;
		}
		precy = currenty;
		precx = 0.0;
	}
	data = save_data;
}

/*---------------------*/
//Ouvre un bmp 24 bits
/*---------------------*/
BOOL GetBmp24(char* chemin,PIXEL **data,DWORD& width,DWORD& height)
{
	BITMAPFILEHEADER	fileheader;
	BITMAPINFOHEADER	infoheader;
	HANDLE				fichier;
	DWORD				dummy,i,j;
	DWORD				size;
	DWORD				bourrage	= 0;
	PIXEL				temp;
	BYTE				*buffer		= NULL;

	fichier = CreateFile(chemin,
						 GENERIC_READ,
						 FILE_SHARE_READ,
						 NULL,
						 OPEN_EXISTING,
						 FILE_ATTRIBUTE_NORMAL,
						 NULL);
	if (fichier == INVALID_HANDLE_VALUE)
		return FALSE;

	ReadFile(fichier,&fileheader,14,&dummy,NULL);
	ReadFile(fichier,&infoheader,40,&dummy,NULL);
	width = infoheader.biWidth;
	height = infoheader.biHeight;

	while ((3*width+bourrage) % 4 != 0)		//gestion du bourrage
		bourrage++;

	if (fileheader.bfType != 0x4D42 || infoheader.biBitCount != 24 
		|| infoheader.biCompression != 0)
	{
		CloseHandle(fichier);
		return FALSE;
	}
	size = width*height;

	buffer = new BYTE[size*3+bourrage*height];
	if (buffer == NULL)
		return FALSE;

	SetFilePointer(fichier,fileheader.bfOffBits,NULL,FILE_BEGIN);

	ReadFile(fichier,buffer,size*3+bourrage*height,&dummy,NULL);

	(*data) = new PIXEL[size];
	if ((*data) == NULL)
	{
		delete[] buffer;
		return FALSE;
	}

	for(i=0;i<height;i++)
	{
		memcpy((*data)+(i*width),buffer+i*(3*width+bourrage),width*3);
	}

	//remet dans le bon sens
	for(i=0;i<(height/2);i++)
	{
		for(j=0;j<width;j++)
		{
			temp  = (*data)[i*width+j];
			(*data)[i*width+j] = (*data)[(height-1-i)*width+j];
			(*data)[(height-1-i)*width+j] = temp;
		}
	}
	

	CloseHandle(fichier);
	delete[] buffer;
	return TRUE;
}

/*------------------------------*/
//Enregistre les données en Htm
/*------------------------------*/
BOOL SaveHtm(char *chemin,const PIXEL *data,DWORD width,DWORD height,char size)
{
	HANDLE	fichier;
	char*	buffer		= NULL;
	char	temp[30];
	DWORD	i,j;
	char	valeur		= '0';
	DWORD	cur			= 0;
	PIXEL	prev		= {0,0,0};
	BOOL	red			= FALSE;

	buffer = new char[80+2*height+28*height*width];
	if (buffer == NULL)
		return FALSE;

	sprintf(buffer,"<HTML><HEAD><BODY bgColor=#000000><PRE><FONT size=%d>",size);
	cur += 53;

	if (data[0].r == 0 && data[0].g == 0 && data[0].b == 0)
	{
			sprintf(temp,
					"<FONT color=#%.2x%.2x%.2x>",
					data[0].r,
					data[0].g,
					data[0].b	);

			memcpy(buffer+cur,temp,20);
			cur += 20;
	}

	for(i=0;i<height;i++)
	{
		for(j=0;j<width;j++)
		{
			if (prev.r == data[i*width+j].r && prev.g == data[i*width+j].g && prev.b == data[i*width+j].b)
			{
				memcpy(buffer+cur,&valeur,1);
				cur++;
				red = TRUE;
			}else
			{
				if (red == TRUE)		//si la couleur du suivant n'est pas la meme que le precedent
				{
					memcpy(buffer+cur,"</FONT>",7);
					cur += 7;
					red = FALSE;
				}

				prev.r = data[i*width+j].r;
				prev.g = data[i*width+j].g;
				prev.b = data[i*width+j].b;

				sprintf(temp,
						"<FONT color=#%.2x%.2x%.2x>%c",
						prev.r,
						prev.g,
						prev.b,
						valeur);
						
				memcpy(buffer+cur,temp,21);
				cur += 21;
			}

			if (valeur =='0')
				valeur = '1';
			else
				valeur = '0';
		}
		if(i==height-1)
		{
			memcpy(buffer+cur,"</FONT>",7);
			cur+=7;
		}
		memcpy(buffer+cur,"\n",1);
		cur += 1;
	}
	memcpy(buffer+cur,"</PRE></FONT></BODY></HTML>",27);
	cur +=27;

	fichier = CreateFile(chemin,
						 GENERIC_WRITE,
						 FILE_SHARE_READ,
						 NULL,
						 CREATE_ALWAYS,
						 FILE_ATTRIBUTE_NORMAL,
						 NULL);

	if (fichier != INVALID_HANDLE_VALUE)
	{
		WriteFile(fichier,buffer,cur,&i,NULL);
		CloseHandle(fichier);
		delete buffer;
		return TRUE;
	}
	delete[] buffer;
	return FALSE;
}

/*--------------*/
//Fonction Main
/*--------------*/
int APIENTRY WinMain(HINSTANCE hInstance,
					 HINSTANCE hPrevInstance,
					 LPSTR lpCmdLine,
					 int nShowCmd)
{
	HWND hWndDlg;
	MSG msg;
	
	hWndDlg = CreateDialog(hInstance,MAKEINTRESOURCE(IDD_DIALOG),NULL,&DlgProc);//Crée la boite de
																				//dialogue
	ShowWindow(hWndDlg,SW_SHOW);	//Affiche la bdd

	while(GetMessage(&msg,NULL,0,0))
	{
		TranslateMessage(&msg);				//Boucle de messages
		DispatchMessage(&msg);
	}
	return 0;
}

/*----------------------------------*/
//Traitement des messages de la bdd
/*----------------------------------*/
BOOL CALLBACK DlgProc(HWND hWnd,UINT uMsg,WPARAM wParam,LPARAM lParam)
{
	int bouton;					//en cas de WM_COMMAND

	switch(uMsg)
	{
	case WM_SYSCOMMAND :				//si on clique sur la croix
		if (wParam == SC_CLOSE)			//on ferme la fenetre
			PostQuitMessage(0);
		return TRUE;

	case WM_INITDIALOG :			//a la création de la boite de dialogue
		SetDlgItemText(hWnd,IDC_FICHIERSOURCE,"Aucun fichier selectionné");
		SetDlgItemText(hWnd,IDC_FICHIERDEST,"Aucun fichier selectionné");
		SetDlgItemInt(hWnd,IDC_TEXTSIZE,2,FALSE);
		return TRUE;

	case WM_COMMAND :

		bouton = LOWORD(wParam);		//Le bouton envoyant le message

		switch(bouton)		
		{
		case IDC_QUIT :				
			PostQuitMessage(0);
			return TRUE;

		case IDC_SOURCE :
			char cheminsource[_MAX_FNAME];	//va contenir le chemin du fichier source
			OPENFILENAME ofn;				//structure pour la bdd ouvrir

			memset(&ofn,0,sizeof(OPENFILENAME));		//on la set à 0
			ofn.lStructSize = sizeof(OPENFILENAME);
			ofn.hwndOwner = hWnd;				//Handle de la fenetre parente ou NULL
			ofn.hInstance = NULL;
			ofn.lpstrFilter = "Fichier BMP 24bits (*.bmp)\0*.bmp\0";
			ofn.lpstrCustomFilter = NULL;
			ofn.nMaxCustFilter = NULL;
			ofn.nFilterIndex = 0;				//index du filtre par defaut
			ofn.lpstrFile = cheminsource;		//votre buffer de fichier
			ofn.nMaxFile = _MAX_FNAME;
			ofn.lpstrFileTitle = NULL;		//buffer contenant le nom du fichier.ext
			ofn.lpstrInitialDir = NULL;		//dir initial;
			ofn.lpstrTitle = "Selectionnez le bitmap";	//titre de la BDG
			ofn.Flags = OFN_FILEMUSTEXIST;	//Selectionner seulement un fichier qui existe 
			ofn.lCustData = NULL;
			ofn.lpfnHook = NULL;
			ofn.lpTemplateName = NULL;
			cheminsource[0] = 0;

			if(GetOpenFileName(&ofn) != 0)		//Si pas de probleme
			{
				SetDlgItemText(hWnd,IDC_FICHIERSOURCE,cheminsource);	//mettre le nom du 
																//fichier dans la edit box
			}
			return TRUE;

		case IDC_DEST :			
			char chemindest[_MAX_FNAME];	//va contenir le chemin du fichier destination
			char *point;
	
			GetDlgItemText(hWnd,IDC_FICHIERSOURCE,chemindest,_MAX_FNAME);
			point = strstr(chemindest,".");
			if (point != NULL)
				strcpy(point,".htm\0");
			else
				ZeroMemory(chemindest,_MAX_FNAME);


			OPENFILENAME ofn2;				//structure pour la bdg ouvrir

			memset(&ofn2,0,sizeof(OPENFILENAME));		//on la set à 0
			ofn2.lStructSize = sizeof(OPENFILENAME);
			ofn2.hwndOwner = hWnd;				//Handle de la fenetre parente ou NULL
			ofn2.hInstance = NULL;
			ofn2.lpstrFilter = "Fichier htm (*.htm)\0*.htm\0";
			ofn2.lpstrCustomFilter = NULL;
			ofn2.nMaxCustFilter = NULL;
			ofn2.nFilterIndex = 0;				//index du filtre par defaut
			ofn2.lpstrFile = chemindest;		//votre buffer de fichier
			ofn2.nMaxFile = _MAX_FNAME;
			ofn2.lpstrFileTitle = NULL;	//buffer contenant le nom du fichier.ext
			ofn2.lpstrInitialDir = NULL;		//dir initial;
			ofn2.lpstrTitle = "Selectionnez le fichier destination";	//titre de la BDD
			ofn2.Flags = OFN_PATHMUSTEXIST;
			ofn2.lCustData = NULL;
			ofn2.lpfnHook = NULL;
			ofn2.lpTemplateName = NULL;

			if(GetSaveFileName(&ofn2) != 0)		//Si pas de probleme
				SetDlgItemText(hWnd,IDC_FICHIERDEST,chemindest);	//mettre le nom du 
																	//fichier dans la edit box
			return TRUE;

		case IDC_PROCESS:
			char pathdest[_MAX_FNAME];	//chemin du fichier de dest
			char pathsrc[_MAX_FNAME];
			DWORD height,width;
			PIXEL *data = NULL;
			PIXEL *scaleddata;
			int xresol,yresol;

			GetDlgItemText(hWnd,IDC_FICHIERDEST,pathdest,_MAX_FNAME);		//obtient les paths
			GetDlgItemText(hWnd,IDC_FICHIERSOURCE,pathsrc,_MAX_FNAME);

			if (strcmp(pathsrc,"Aucun fichier selectionné") == 0)		//si pas fichier src
			{
				MessageBox(hWnd,"Selectionne d'abord le fichier source!","Erreur fichier",MB_OK | MB_ICONEXCLAMATION);
				return TRUE;
			}

			if (strcmp(pathdest,"Aucun fichier selectionné") == 0)		//si pas fichier dest
			{
				MessageBox(hWnd,"Selectionne d'abord le fichier de destination!","Erreur fichier",MB_OK | MB_ICONEXCLAMATION);
				return TRUE;
			}

			xresol = GetDlgItemInt(hWnd,IDC_X,NULL,FALSE);
			yresol = GetDlgItemInt(hWnd,IDC_Y,NULL,FALSE);

			if(GetBmp24(pathsrc,&data,width,height))
			{
				if (xresol != 0 && yresol !=0)		//Gestion du scale
				{
					scaleddata = new PIXEL[xresol*yresol];
					if (scaleddata != NULL)
					{
						Scale(data,width,height,scaleddata,xresol,yresol,FALSE);
						delete[] data;
						data	= scaleddata;
						width	= xresol;
						height	= yresol;
					}
				}

				if(SaveHtm(pathdest,data,width,height,GetDlgItemInt(hWnd,IDC_TEXTSIZE,NULL,FALSE)))
					MessageBox(hWnd,"Opération réussie","Bitmap24ToHtm",MB_OK | MB_ICONEXCLAMATION);
				else
					MessageBox(hWnd,"Erreur à l'écriture du fichier de destination","Bitmap24ToHtm",MB_OK | MB_ICONEXCLAMATION);
		
				delete[] data;
			}
			else
				MessageBox(hWnd,"Erreur a l'ouverture du fichier source\nVerifiez que le fichier soit bien un bitmap 24bits","Bitmap24ToHtm",MB_OK | MB_ICONEXCLAMATION);

			return TRUE;	
		}
		if (HIWORD(wParam) == EN_KILLFOCUS && LOWORD(wParam) == IDC_TEXTSIZE)
		{
			int size = GetDlgItemInt(hWnd,IDC_TEXTSIZE,NULL,FALSE);
			if (size<1 || size >7)
				SetDlgItemInt(hWnd,IDC_TEXTSIZE,2,FALSE);	
			return TRUE;
		}
		return FALSE;
	}
	return FALSE;
}

 Conclusion

Attention, veuillez générer des fichiers htm de faible résolution.
Si le fichier bmp contient beaucoup de couleurs différentes, le fichier htm sera très vite conséquent

De plus si le fichier est trop gros,s'il y a trop de changement de couleur, il y aura des risques de plantage du browser (notament IE)
Evitez donc de dépasser le 100X100 en résolution.

 Fichier Zip

Les Membres Club peuvent télécharger directement un fichier contenu dans le zip sans télécharger le zip en entier !

Télécharger le zip


 Historique

30 juillet 2004 01:29:05 :
Rajout du zip oublié :(
30 juillet 2004 16:54:58 :
Enlevage du <string.h> inutile

 Sources du même auteur

Source avec Zip Source avec une capture DETECTION DE CONTOURS
Source avec Zip ETIRER/RÉTRÉCIR UNE IMAGE BMP SANS STRETCHBLT
Source avec Zip Source avec une capture BITMAP24 TO TEXT - TRANSFORME UNE IMAGE EN TEXTE VC++
Source avec Zip RESOLUTION D'EQUATIONS DE DEGRÉ 4 OU INFERIEUR
Source avec Zip Source avec une capture DEBUT DE MOTEUR D'AFFICHAGE OPENGL AVEC RENDU DE SKYBOX

 Sources de la même categorie

Source avec Zip APPLICATION DE DESSIN DE QUELQUES FIGURES par laguchori
Source avec Zip Source avec une capture HDR EXPOSURE FUSION par mecrosoft
Source avec Zip Source avec une capture IRC CLIENT MULTISERVEUR EN MFC (TXIRC) par TeniX
Source avec Zip ENTETE DU FICHIER BMP (BIPMAP) par k.Lutchi
Source avec Zip Source avec une capture XCOUPE : COUPE 2D par pop70

Commentaires et avis

Commentaire de jerrol le 30/07/2004 00:42:43

Salut !
Le programme a l'air interessant, d'après la capture.
Par contre, il manque le fichier "resource.h".
@+

Commentaire de Inekman le 30/07/2004 00:55:21

C'est trop bien codé même si je cale pas grand chose au C++ mais je trouve l'indentation trop belle et tout.

Ca fait plaisir aux noeils :-P

Inekman.

Commentaire de Maegis le 30/07/2004 01:26:42

Arf j'ai oublié de mettre le zip, je le rajoute !!
jerol>>>Télécharge le zip!

Commentaire de BruNews le 30/07/2004 14:26:15 administrateur CS

Salut Maegis,
pourquoi tu mets:
#include <stdio.h>
#include <string.h>

sur VC++ windows.h devrait suffire.

Commentaire de Maegis le 30/07/2004 16:04:39

Ben en fait j'ai repris une veille source qui trainait au fond de mon PC, copier/coller, virage de 2-3 trucs et rajout des morceaux qui manquaient. :)
Le string.h est inutile en effet mais par contre le stdio.h est necessaire car j'utilise sprintf.

Commentaire de BruNews le 30/07/2004 16:11:15 administrateur CS

ah oui, je n'avais pas vu les 'sprintf'.

Commentaire de Kirua le 30/07/2004 22:51:05

je te conseille d'au contraire créer un tableau de x lignes * y colonnes pour une image de x * y pixels. chaque case a comme couleur de fond la couleur du pixel correspondant. à partir de là, il suffit de définir la largeur et la hauteur du tableau (dont les bords sont de 0 bien sur) pour agrandir/rétrécir l'image.

autre chose: écrire une classe de chargement de BMP n'est vrmnt pas difficile, et ça te permettrait de faire un code portable. t'en as pour une soirée, et t'es qd même plus satisfait à partir du moment où tu as dû comprendre le format BMP, c'est jamais perdu.

Commentaire de Maegis le 31/07/2004 01:27:02

Ouais, ouais, une classe pour la gestion des BMP je pensait le faire, c'est vrai que ça peut toujours servir.

Par contre je comprend pas ce que tu veux dire juste au dessus, tu veux dire qu'il serait mieux de travailler avec un tableau 2d ???
Et puis que veux tu dire par définir la largeur et la hauteur du tableau : si ton tableau est de x lignes, y colonnes, il est de x lignes y colonnes
Enfin je pige le sens de la phrase quoi

Commentaire de Kirua le 31/07/2004 12:25:05

les caractères "plus grand" et "plus petit" vont sauter parce que le site est mal fait de ce pt de vue mais regarde, un truc du style: (ds la balise table, x et y sont à remplacer bien sûr)

<table cellpadding="0" cellspacing="0" border="0" width="x" height="y">

<tr>
<td bgcolor="#abcdef"></td>
<td bgcolor="#abcdef"></td>
<td bgcolor="#abcdef"></td>
</tr>
<tr>
<td bgcolor="#abcdef"></td>
<td bgcolor="#abcdef"></td>
<td bgcolor="#abcdef"></td>
</tr>
<tr>
<td bgcolor="#abcdef"></td>
<td bgcolor="#abcdef"></td>
<td bgcolor="#abcdef"></td>
</tr>

</table>


chaque cellule du tableau a une couleur de fond propre (ici j'ai mis la même chose partout parce que c copié collé) et cette couleur est la couleur du pixel correspondant (3 * 3 cellules donc image originale: 3 * 3 pixels). tu comprends?

Commentaire de Kirua le 31/07/2004 12:25:46

hey, les balises html sont restées :):):):) avant ça ne passait pas, chouette :)

Commentaire de BruNews le 31/07/2004 12:29:18 administrateur CS

Kirua pris en flag de medisance indue...

Commentaire de LordBob le 31/07/2004 13:34:09

j'avais deja vu un programme comme celui ci... mais le resultat et toujours aussi bien :) !!!

Commentaire de Kirua le 31/07/2004 13:50:02

avant les balises ne passaient pas, et reconnais qu'il y a encore qq bugs gênant sur le réseau. ça ne m'empêche pas de venir tous les jours sur bcp de codes...

Commentaire de BruNews le 31/07/2004 13:54:17 administrateur CS

evident, je plaisantais.

Commentaire de Maegis le 31/07/2004 15:25:06

Kirua>>> Ben en fait la couleur de fond je m'en fous
Ce que je voulais faire c'est une image en 0 et en 1 comme je l'avais vu sur un site.
C'est vrai que si l'on veut rendre l'image plus fidelement, c'est mieux de faire comme tu dis

Commentaire de _Thy_ le 12/09/2004 20:46:04

Tu devrais (si c autorisé) utiliser le format png pour ton pb de grab aux couleurs dégradées.

Commentaire de andros le 17/09/2004 00:03:33

je voulais dire  : j'ai essayé avec une photo de moi c nickel

 Ajouter un commentaire




Nos sponsors


Sondage...

CalendriCode

Février 2012
LMMJVSD
  12345
6789101112
13141516171819
20212223242526
272829    

Consulter la suite du CalendriCode

Photothèque

 
Développement réalisé par Nicolas SOREL (Nix) avec l'aide de : Cyril DURAND et Emmanuel (EBArtSoft), Merci à Vincent pour ses précieux conseils.
CodeS-SourceS.com© Toute reproduction même partielle est interdite sauf accord écrit du Webmaster
CodeS-SourceS.com© est une marque déposée tous droits réservés

Google Coop CodeS-SourceS Google Coop CodeS-SourceS
Temps d'éxécution de la page : 0,530 sec (4)

Nous contacter | Annoncer sur CodeS-SourceS | Mentions légales