Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <stdio.h>
- #include <stdlib.h>
- struct BitMap {
- short int Signature;
- int FileSize;
- short int Reserved1;
- short int Reserved2;
- int Offset;
- int HeaderSize;
- int Width;
- int Height;
- short int Planes;
- short int BitsPerPixel;
- int Compression;
- int ImageSize;
- int XPixelsPerMeter;
- int YPixelsPerMeter;
- int ColorsInColorTable;
- int ImportantColorCount;
- char *rest;
- char *ColorTable;
- char *GAP1;
- struct Pix*PixelArray;
- };
- struct Pix {
- char R;
- char G;
- char B;
- };
- /*void PixOne (char r0, char g0, char b0, struct Pix p){
- p.R=r0;
- p.G=g0;
- p.B=b0;
- };*/
- struct Pix getPix (int x, int y, struct BitMap bm)
- {
- //struct Pix p;
- return bm.PixelArray [(bm.Height - y - 1)*bm.Width*3 + x];
- }
- void setPix (int x, int y, struct BitMap bm, struct Pix p)
- {
- y = bm.Height - y - 1;
- bm.PixelArray [y*bm.Width*3 + x].R = p.R;
- (bm.PixelArray [y*bm.Width*3 + x]).G = p.G;
- (bm.PixelArray [y*bm.Width*3 + x]).B = p.B;
- }
- struct BitMap readBitMap(FILE *BMPFile) {
- struct BitMap x;
- fread(&x.Signature,2,1,BMPFile);
- fread(&x.FileSize,4,1,BMPFile);
- fread(&x.Reserved1,2,1,BMPFile);
- fread(&x.Reserved2,2,1,BMPFile);
- fread(&x.Offset,4,1,BMPFile);
- fread(&x.HeaderSize,4,1,BMPFile);
- fread(&x.Width,4,1,BMPFile);
- fread(&x.Height,4,1,BMPFile);
- fread(&x.Planes,2,1,BMPFile);
- fread(&x.BitsPerPixel,2,1,BMPFile);
- fread(&x.Compression,4,1,BMPFile);
- fread(&x.ImageSize,4,1,BMPFile);
- fread(&x.XPixelsPerMeter,4,1,BMPFile);
- fread(&x.YPixelsPerMeter,4,1,BMPFile);
- fread(&x.ColorsInColorTable,4,1,BMPFile);
- fread(&x.ImportantColorCount,4,1,BMPFile);
- x.rest = (char*) malloc(x.HeaderSize-40);
- //printf("%d%n",x.ImageSize);
- //printf("%d%n",(x.Width*3+2)*x.Height);
- fread(x.rest,x.HeaderSize-40,1,BMPFile);
- x.ColorTable = (char*) malloc(x.ColorsInColorTable*4);
- fread(x.ColorTable,x.ColorsInColorTable*4,1,BMPFile);
- x.GAP1 = (char*) malloc(x.Offset-x.HeaderSize-14-x.ColorsInColorTable*4);
- fread(x.GAP1,x.Offset-x.HeaderSize-14-x.ColorsInColorTable*4,1,BMPFile);
- int JunkBytes = 0;
- if ((4-(x.Width*3)%4) !=4)
- JunkBytes = 4-(x.Width*3)%4;
- fseek(BMPFile, x.Offset, SEEK_SET);
- x.PixelArray = (struct Pix*) malloc(x.Width*x.Height*3);
- int i;
- for (i = 0; i < x.Height; i++){
- fread(x.PixelArray+3*x.Width*i,3,x.Width,BMPFile);
- fseek(BMPFile,JunkBytes,SEEK_CUR);
- }
- return x;
- }
- void writeBitMap(FILE *BMPFile,struct BitMap bm) {
- fwrite(&bm.Signature,2,1,BMPFile);
- fwrite(&bm.FileSize,4,1,BMPFile);
- fwrite(&bm.Reserved1,2,1,BMPFile);
- fwrite(&bm.Reserved2,2,1,BMPFile);
- fwrite(&bm.Offset,4,1,BMPFile);
- fwrite(&bm.HeaderSize,4,1,BMPFile);
- fwrite(&bm.Width,4,1,BMPFile);
- fwrite(&bm.Height,4,1,BMPFile);
- fwrite(&bm.Planes,2,1,BMPFile);
- fwrite(&bm.BitsPerPixel,2,1,BMPFile);
- fwrite(&bm.Compression,4,1,BMPFile);
- fwrite(&bm.ImageSize,4,1,BMPFile);
- fwrite(&bm.XPixelsPerMeter,4,1,BMPFile);
- fwrite(&bm.YPixelsPerMeter,4,1,BMPFile);
- fwrite(&bm.ColorsInColorTable,4,1,BMPFile);
- fwrite(&bm.ImportantColorCount,4,1,BMPFile);
- fwrite(bm.rest,bm.HeaderSize-40,1,BMPFile);
- fwrite(bm.ColorTable,bm.ColorsInColorTable*4,1,BMPFile);
- fwrite(bm.GAP1,bm.Offset-bm.HeaderSize-14-bm.ColorsInColorTable*4,1,BMPFile);
- int JunkBytes = 0;
- if ((4-(bm.Width*3)%4) !=4)
- JunkBytes = 4-(bm.Width*3)%4;
- fseek(BMPFile, bm.Offset, SEEK_SET);
- int i;
- for (i = 0; i < bm.Height; i++){
- fwrite(bm.PixelArray+3*bm.Width*i,3,bm.Width,BMPFile);
- int x=0;
- int j=0;
- for (j = 0; j<JunkBytes;j++)
- fwrite(&x,1,1,BMPFile);
- }
- }
- int main(void) {
- if (sizeof(int)!=4) {
- printf ("error");
- }
- else {
- struct BitMap x;
- FILE *f;
- FILE *f1;
- f = fopen("mapa.bmp","rb");
- if(f!= NULL) {
- x = readBitMap(f);
- fclose(f);
- }
- else {
- printf ("bad file");
- }
- struct Pix p;
- p = getPix (6,6,x);
- setPix(0,13,x,p);
- f1 = fopen("mapa.bmp","wb");
- writeBitMap(f1,x);
- fclose(f1);
- }
- return 0;
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement