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
TECHDAYS PARIS 2010 : SHAREPOINT 2010 POUR LES DéVELOPPEURSTECHDAYS PARIS 2010 : SHAREPOINT 2010 POUR LES DéVELOPPEURS par ROMELARD Fabrice
Animé par: Laurent Cotton Le développement dans SharePoint 2010 passe par plusieurs axes qui seront évoqués dans cette session, mais plus particulièrement les développements simples lié au besoin Business Business Connectivity Services Ce BCS es...
Cliquez pour lire la suite de l'article par ROMELARD Fabrice TECHDAYS PARIS 2010 : PLEINIèRE DERNIER JOURTECHDAYS PARIS 2010 : PLEINIèRE DERNIER JOUR par ROMELARD Fabrice
Cette session est la dernière pleinière de ces 3 jours de TechDays Paris 2010. Généralement, cette troisième journée est plus axée sur l'avenir vu par Microsoft. Après un retour sur l'avenir vu par la Science Fiction ou par ...
Cliquez pour lire la suite de l'article par ROMELARD Fabrice UNE JOLIE-HORLOGE ET PAS QU'UN PEU !UNE JOLIE-HORLOGE ET PAS QU'UN PEU ! par neodante
Pour les possesseurs d'iPhone, ça y est Bijin Tokei - qui se traduit littéralement en Français par " Jolie Horloge " - est arrivé et GRATUITEMENT s'il vous plaît ! Après la version Tokyo, Hokkaido, night club, racing, Gal, "pour les mademoiselles'", . voi...
Cliquez pour lire la suite de l'article par neodante TECHDAYS PARIS 2010 : CONNECTEZ VOS DONNéES à SHAREPOINT 2010 AVEC LES BUSINESS CONNECTIVITY SERVICESTECHDAYS PARIS 2010 : CONNECTEZ VOS DONNéES à SHAREPOINT 2010 AVEC LES BUSINESS CONNECTIVITY SERVICES par ROMELARD Fabrice
Animé par: Gaetan Bouveret et Julien Chomarat Business Connectivity Services (BCS) est dans SharePoint 2010 la version 2 de Business Data Catalog (BDC dans SharePoint 2007). Il s'agit de la solution permettant de visualiser des données provenan...
Cliquez pour lire la suite de l'article par ROMELARD Fabrice [DIVERS] SUIVRE VOS SéRIES PRéFéRéS SUR LA TOILE[DIVERS] SUIVRE VOS SéRIES PRéFéRéS SUR LA TOILE par orion
Comme de nombreux geek, je suis un grand amateur de série TV et je rate régulièrement des épisodes de mes séries préférés. Une solution s'offre à vous avec ce merveilleux site : Tv Gorge - www.tvgorge.com Moteur de recherche à l'appui, vous pouvez ...
Cliquez pour lire la suite de l'article par orion
Forum
RE : WIN APIRE : WIN API par racpp
Cliquez pour lire la suite par racpp
Logiciels
DB-MAIN (9.1.0)DB-MAIN (9.1.0)DB-MAIN is a data-modeling and data-architecture tool. It is designed to help developers and anal... Cliquez pour télécharger DB-MAIN Xilisoft DPG Convertisseur (5.1.37.0120)XILISOFT DPG CONVERTISSEUR (5.1.37.0120)Xilisoft DPG Convertisseur offre aux fans de Nintendo DS une bonne solution leur permettant de dé... Cliquez pour télécharger Xilisoft DPG Convertisseur GraphicsGale (2.01.01)GRAPHICSGALE (2.01.01)GraphicsGale est un logiciel de PixelArt avec de nombreuse fonctionnalités permettant de réalisé ... Cliquez pour télécharger GraphicsGale Architecte 3D (Platinum 2010)ARCHITECTE 3D (PLATINUM 2010)Architecte 3D Platinium vous permet de concevoir facilement les plans votre future maison, de l'é... Cliquez pour télécharger Architecte 3D TeamViewer 5 (TeamViewer 5)TEAMVIEWER 5 (TEAMVIEWER 5)Dépanner un ami,expliquer une manipulation devient un jeu d'enfant.
Prise en main d'un autre ord... Cliquez pour télécharger TeamViewer 5
|