Accueil > > > 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
Description
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.
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
Sources de la même categorie
Commentaires et avis
|
Derniers Blogs
WORKFLOW FOUNDATION 3 A UN PIED DANS LA TOMBEWORKFLOW FOUNDATION 3 A UN PIED DANS LA TOMBE par JeremyJeanson
Depuis déjà un an, je conseille vivement les utilisateurs de Workflow Foundation 3 à migrer vers la version 4. L'information qui va suivre ne devrait donc pas trop prendre au dépourvu les personnes qui l'ont sagement suivi. Je profite de ce poste pour fai...
Cliquez pour lire la suite de l'article par JeremyJeanson TECHDAYS PARIS 2012 : NOUVELLES TENDANCES DU POSTE DE TRAVAIL - BRING YOUR OWN PCTECHDAYS PARIS 2012 : NOUVELLES TENDANCES DU POSTE DE TRAVAIL - BRING YOUR OWN PC par ROMELARD Fabrice
Speakers: Thierry Rapatout, Antoine Petit et Xavier Trebbia Cette session entre dans le cadre des RDV Décideurs des TechDays 2012, elle est liée à la consumérisation de l'IT et la mise en place du "DeskTop as a Service" dans de plus en ...
Cliquez pour lire la suite de l'article par ROMELARD Fabrice TECHDAYS PARIS 2012 : SYSTEM CENTER SERVICE MANAGER 2012 VUE D'ENSEMBLETECHDAYS PARIS 2012 : SYSTEM CENTER SERVICE MANAGER 2012 VUE D'ENSEMBLE par ROMELARD Fabrice
Speakers: Julien Marechal, Gautier Confiant, Sébastien MEYER La session débute par le positionnement de la solution System Center par rapport aux concepts d'organisation ITIL. Le portail du catalogue de se...
Cliquez pour lire la suite de l'article par ROMELARD Fabrice TECHDAYS PARIS 2012 : PLEINIèRE SECOND JOURTECHDAYS PARIS 2012 : PLEINIèRE SECOND JOUR par ROMELARD Fabrice
Après une première journée dédiée aux développeurs, cette seconde journée est dédiée au monde des entreprises et de ses applications. Ainsi, cette pleinière est dédiée à faire un 360 de l'évolution des applications Business aux demandes ac...
Cliquez pour lire la suite de l'article par ROMELARD Fabrice TECHDAYS PARIS 2012 : RETOUR D'EXPéRIENCE SUR LA MISE EN PLACE D'UN CLOUD PRIVéTECHDAYS PARIS 2012 : RETOUR D'EXPéRIENCE SUR LA MISE EN PLACE D'UN CLOUD PRIVé par ROMELARD Fabrice
Speaker : Guillaume Rochette Cette session est dédiée à fournir le retour sur la mise en place d'un cloud privé (IaaS) par Osiatis pour son compte ou celui de ses clients. Ce projet s'est déroulé sur 4 mois et a permis de faire évoluer...
Cliquez pour lire la suite de l'article par ROMELARD Fabrice
Logiciels
Academy System (17.2.1.0)ACADEMY SYSTEM (17.2.1.0)Logiciel de gestion des établissements.
- élèves/étudiants (inscription, dossier, absence...)
-... Cliquez pour télécharger Academy System Easy-Planning (1.0.0.1)EASY-PLANNING (1.0.0.1)Basé sur les mêmes principes que MyPlanning, Easy-Planning permet de créer des plannings sous la ... Cliquez pour télécharger Easy-Planning COLLECTOR PLUS (3.00B)COLLECTOR PLUS (3.00B)COLLECTOR PLUS version 3.00B est un logiciel utilisant une base de données alimentée par :
- L... Cliquez pour télécharger COLLECTOR PLUS PONAMEDIA PREMIUM - HELLLOOO FLASH DEMO (V7.4)PONAMEDIA PREMIUM - HELLLOOO FLASH DEMO (V7.4)PONAMEDIA TV DEVIENS HELLLOOO FLASH
LA TV SUR VOTRE ORDINATEUR.
Toute une plateforme Multi... Cliquez pour télécharger PONAMEDIA PREMIUM - HELLLOOO FLASH DEMO LettresFaciles 2011 (8.0.0.1)LETTRESFACILES 2011 (8.0.0.1)LettresFaciles est un logiciel facilitant la création et la rédaction de lettres types.
Son inte... Cliquez pour télécharger LettresFaciles 2011
|