Accueil > > > CONVERSION DE FICHIER EN FICHIER BMP
CONVERSION DE FICHIER EN FICHIER BMP
Information sur la source
Description
Dans la même optique que ma source précédente, voici un programme pour faire des fichiers bmp en utilisant un fichier quelconque. Il prend en argument le nom du fichier à convertir et le nom du fichier de sotie (facultatif).
Source
- #include <stdio.h>
- #include <stdlib.h>
- #include <string.h>
- #include <math.h>
-
- int main(int argc, char *argv[])
- {
- /* BMP header */
- char BM[2] = "BM"; //Magic number
- int FileSize; //Size of the BMP file
- short Creator1 = 0; //Unused
- short Creator2 = 0; //Unused
- int DataStart = 54; //Offset where the pixel array (bitmap data) can be found
- /*DIB header */
- int DIB = 40; //Number of bytes in the DIB header (from this point)
- int Width;
- int Height;
- short ColorPlanes = 1;
- short BitsPerPixel = 24;
- int Compression = 0; //BI_RGB, no pixel array compression used
- int DataSize; //Size of the raw data in the pixel array (including padding)
- int HorizontalResolution = 0;
- int VerticalResolution = 0;
- int ColorPalette = 0; //Number of colors in the palette
- int ImportantColors = 0; //0 means all colors are important
- /*Data start */
-
- char RawName[FILENAME_MAX], BmpName[FILENAME_MAX] = "bmp.bmp";
- if(argc > 1)
- {
- strcpy(RawName, argv[1]);
- if(argc > 2)
- strcpy(BmpName, argv[2]);
- }
- else
- {
- printf("bmp [RawFile]\n");
- return -1;
- }
-
- FILE *raw = fopen(RawName, "rb+");
- if(raw == NULL)
- {
- printf("File \"%s\" not found.\n", RawName);
- return -1;
- }
- FILE *bmp = fopen(BmpName, "wb");
- if(bmp == NULL)
- {
- printf("Can not create \"%s\".\n", BmpName);
- return -1;
- }
- fseek(raw, 0, SEEK_END);
- DataSize = ftell(raw); //donnees
- int tmp = DataSize;
- while(1)
- {
- if(tmp%3 == 0 && tmp%4 == 0 && (float)sqrt(tmp/3)-(int)sqrt(tmp/3) == 0 && (int)sqrt(tmp/3)%4 == 0) //Pour avoir un nombre de données multiple de 3 (RGB), aucun padding (multiple de 4), ainsi qu'un carre.
- break;
- else
- tmp++;
- }
- printf("Bits de plus : %d\n", tmp-DataSize);
- Width = sqrt(tmp/3);
- Height = sqrt(tmp/3);
-
- DataSize = tmp;
- FileSize = 54 + DataSize;//header + donnees
- printf("File Size = %d bits (%.0f kb)\n", FileSize, (float)FileSize/1024);
-
- fwrite(BM, 1, 2, bmp);
- fwrite(&FileSize, 4, 1, bmp);
- fwrite(&Creator1, 2, 1, bmp);
- fwrite(&Creator2, 2, 1, bmp);
- fwrite(&DataStart, 4, 1, bmp);
- fwrite(&DIB, 4, 1, bmp);
- fwrite(&Width, 4, 1, bmp);
- fwrite(&Height, 4, 1, bmp);
- fwrite(&ColorPlanes, 2, 1, bmp);
- fwrite(&BitsPerPixel, 2, 1, bmp);
- fwrite(&Compression, 4, 1, bmp);
- fwrite(&DataSize, 4, 1, bmp);
- fwrite(&HorizontalResolution, 4, 1, bmp);
- fwrite(&VerticalResolution, 4, 1, bmp);
- fwrite(&ColorPalette, 4, 1, bmp);
- fwrite(&ImportantColors, 4, 1, bmp);
-
- printf("Creating %s...\n", BmpName);
- char *p = calloc(DataSize,sizeof(char)); //calloc pour avoir des 0 (noir) pour les pixels supplementaires
- if(p == NULL)
- {
- printf("Dynamic memory allocation failed.\n");
- return -1;
- }
- fseek(raw, 0, SEEK_SET);
- fread(p, 1, DataSize, raw);
- fwrite(p, 1, DataSize, bmp);
-
- printf("%s created.\n", BmpName);
- free(p);
-
- fclose(raw);
- fclose(bmp);
- }
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <math.h>
int main(int argc, char *argv[])
{
/* BMP header */
char BM[2] = "BM"; //Magic number
int FileSize; //Size of the BMP file
short Creator1 = 0; //Unused
short Creator2 = 0; //Unused
int DataStart = 54; //Offset where the pixel array (bitmap data) can be found
/*DIB header */
int DIB = 40; //Number of bytes in the DIB header (from this point)
int Width;
int Height;
short ColorPlanes = 1;
short BitsPerPixel = 24;
int Compression = 0; //BI_RGB, no pixel array compression used
int DataSize; //Size of the raw data in the pixel array (including padding)
int HorizontalResolution = 0;
int VerticalResolution = 0;
int ColorPalette = 0; //Number of colors in the palette
int ImportantColors = 0; //0 means all colors are important
/*Data start */
char RawName[FILENAME_MAX], BmpName[FILENAME_MAX] = "bmp.bmp";
if(argc > 1)
{
strcpy(RawName, argv[1]);
if(argc > 2)
strcpy(BmpName, argv[2]);
}
else
{
printf("bmp [RawFile]\n");
return -1;
}
FILE *raw = fopen(RawName, "rb+");
if(raw == NULL)
{
printf("File \"%s\" not found.\n", RawName);
return -1;
}
FILE *bmp = fopen(BmpName, "wb");
if(bmp == NULL)
{
printf("Can not create \"%s\".\n", BmpName);
return -1;
}
fseek(raw, 0, SEEK_END);
DataSize = ftell(raw); //donnees
int tmp = DataSize;
while(1)
{
if(tmp%3 == 0 && tmp%4 == 0 && (float)sqrt(tmp/3)-(int)sqrt(tmp/3) == 0 && (int)sqrt(tmp/3)%4 == 0) //Pour avoir un nombre de données multiple de 3 (RGB), aucun padding (multiple de 4), ainsi qu'un carre.
break;
else
tmp++;
}
printf("Bits de plus : %d\n", tmp-DataSize);
Width = sqrt(tmp/3);
Height = sqrt(tmp/3);
DataSize = tmp;
FileSize = 54 + DataSize;//header + donnees
printf("File Size = %d bits (%.0f kb)\n", FileSize, (float)FileSize/1024);
fwrite(BM, 1, 2, bmp);
fwrite(&FileSize, 4, 1, bmp);
fwrite(&Creator1, 2, 1, bmp);
fwrite(&Creator2, 2, 1, bmp);
fwrite(&DataStart, 4, 1, bmp);
fwrite(&DIB, 4, 1, bmp);
fwrite(&Width, 4, 1, bmp);
fwrite(&Height, 4, 1, bmp);
fwrite(&ColorPlanes, 2, 1, bmp);
fwrite(&BitsPerPixel, 2, 1, bmp);
fwrite(&Compression, 4, 1, bmp);
fwrite(&DataSize, 4, 1, bmp);
fwrite(&HorizontalResolution, 4, 1, bmp);
fwrite(&VerticalResolution, 4, 1, bmp);
fwrite(&ColorPalette, 4, 1, bmp);
fwrite(&ImportantColors, 4, 1, bmp);
printf("Creating %s...\n", BmpName);
char *p = calloc(DataSize,sizeof(char)); //calloc pour avoir des 0 (noir) pour les pixels supplementaires
if(p == NULL)
{
printf("Dynamic memory allocation failed.\n");
return -1;
}
fseek(raw, 0, SEEK_SET);
fread(p, 1, DataSize, raw);
fwrite(p, 1, DataSize, bmp);
printf("%s created.\n", BmpName);
free(p);
fclose(raw);
fclose(bmp);
}
Conclusion
Les fichiers bmp sont étonnamment plus difficiles à gérer pour ce type d'opération qu'un wav, et donc ma source n'est pas des plus optimale, mais le tout fonctionne. Si jamais le fichier ne répond pas aux attentes fixées par le programme (aucun padding ainsi qu'une hauteur et largeur égale), il rajoute des octets jusqu'à ce que cette condition soie remplie. Les informations sur l'header du format bmp sont disponibles sur Wikipedia.
Sources du même auteur
Sources de la même categorie
Commentaires et avis
Discussions en rapport avec ce code source dans le forum
Conversion d un float [ par Seth ]
Comment arrondir un float vers le nombre le plus proche.Exemple : (float)2.8 -> 3 ! (float)2.3 -> 2
Conversion de types de données... [ par nullspace ]
J'ai jamais réussis à trouver une bonne solution pour convertir un type de variable dans une autre variable ayant un autre type.Par exemple...si j'ai
Demande source en C conversion IEEE754 AU SECOUR!!!!!! [ par Benny54 ]
Si vous avez quoi que ce soit sur la conversion IEEE754 en langage C envoyez le moi je suis au bord du GOUFFRE!!!!!
source en c conversion Urgent [ par fleur ]
je suis super débutante. Je cherche en C un code pour convertir un décimal en hexadécimal.Je vous remercie d'avance pour votre aide...j'en ai vraiment
Conversion de type [ par Dlofret ]
Bonjour, J'aimerais une façon simple d'afficher une donnée de type float dans un format string. Comment dois-jem m'y prendre ??
Conversion int -> char * [ par Xentor ]
Bonjour tout le monde, et merci de lire mon message parce que je suis un vrai débutant !Je voudrais savoir comment convertir un entier en tableau de c
Conversion de donnée [ par Johjo ]
Salut tout le monde, je cherche à convertir une valeur char en valeur float et inversement de float en char. L'equivalent de Val et Str en basic.Merci
pb de conversion de chaine de caractéres [ par Thanatos ]
Bonjour,Je désirerais faire un programme sur les fonctions logiques de base : un utilisateur rentre une fonction logique et le programme lui sort la t
Conversion Float to String [ par PierreP ]
Bonjour à tous !je suis en train de me prendre la tête pour créer une fonction de conversion d'un réel en une chaine de caractère (problème du débutan
Conversion de chaine en entier [ par JMGR ]
Comment peut - on convertir une chaine qui contient par exemple "25" en entier ?Exemple :char texte[256]="25";int nombre;nombre = texte; //Erreur...<
|
Derniers Blogs
ETENDRE LE TEAM WEB ACCESS DE TFS 2012 - STEP 0ETENDRE LE TEAM WEB ACCESS DE TFS 2012 - STEP 0 par Philess
L'extensibilité du Team Web Access
Le Web Access (site d'équipe) de Team Foundation Server a été complètement réécrit dans la version 2012 avec pas moins de 400.000 lignes de JavaScript. Ce nouveau modèle a été pensé pour offrir de grandes...
Cliquez pour lire la suite de l'article par Philess SIMULER FACILEMENT L'ENVOI DE MAILSIMULER FACILEMENT L'ENVOI DE MAIL par JeremyJeanson
il m'a été demandé, à plusieurs reprises, comment je faisais pour simuler l'envoi de mail lors de mes démos de Workflow Foundation. Ma solution est plutôt simple : j'utilise la configuration par défaut du SmtpClient et j'oriente les mails vers un dossier ...
Cliquez pour lire la suite de l'article par JeremyJeanson VOTEZ POUR LE TOP 10 DES INFLUENCEURS SHAREPOINT FRANCOPHONES !VOTEZ POUR LE TOP 10 DES INFLUENCEURS SHAREPOINT FRANCOPHONES ! par Patrick Guimonet
Si ce n'est déjà fait (comme plus de 600 personnes déjà), il est encore temps de voter pour le concours TOP 10 des influenceurs SharePoint francophones ! Il est organisé par harmon.ie et accessible ici : http://harmon.ie/top-...
Cliquez pour lire la suite de l'article par Patrick Guimonet [CONF'SHAREPOINT] DERNIER RAPPEL ! :-)[CONF'SHAREPOINT] DERNIER RAPPEL ! :-) par Patrick Guimonet
La Conf'SharePoint en chiffres c'est : 3 jours de SharePoint ! 4 parcours et 60 sessions 17 partenaires représentant toutes les fac...
Cliquez pour lire la suite de l'article par Patrick Guimonet
Forum
PB PACMAN C++PB PACMAN C++ par garfield95
Cliquez pour lire la suite par garfield95
Logiciels
Easy-Planning (4.5.0.11)EASY-PLANNING (4.5.0.11)Easy-Planning permet de créer des plannings sous la représentation de diagrammes et est adapté a... Cliquez pour télécharger Easy-Planning CVEasy (3.1.0.51)CVEASY (3.1.0.51)PHMSD-CVEasy est un logiciel d'aide à la rédaction de CV d'une simplicité déconcertante.
PHMSD-C... Cliquez pour télécharger CVEasy LettresFaciles 2011 (8.6.0.31)LETTRESFACILES 2011 (8.6.0.31)LettresFaciles est un logiciel facilitant la création et la rédaction de lettres types.
Son inte... Cliquez pour télécharger LettresFaciles 2011 sDEVIS-FACTURES vlPRO (8.4.2.62)SDEVIS-FACTURES VLPRO (8.4.2.62)sDEVIS-FACTURES vlPRO a été mis au point pour les particuliers, créateurs, entrepreneurs, artisa... Cliquez pour télécharger sDEVIS-FACTURES vlPRO Devis-Factures PHMSD (2.1.0.11)DEVIS-FACTURES PHMSD (2.1.0.11)Configuration minimale
Nécessite Windows™ 2000, XP, Windows 7, 8, Vista (Service Pack à... Cliquez pour télécharger Devis-Factures PHMSD
|