Advertisement
hjh

Untitled

hjh
Jun 13th, 2013
239
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 8.71 KB | None | 0 0
  1. #include <iostream>
  2. #include <cstdlib>
  3.  
  4. //class structure for bmpFile
  5.  
  6. class BmpFile {
  7. public:
  8. // properties...
  9. int bmpIdentifier; //BM identifier
  10. long bmpFilesize; //File size
  11. unsigned short int bmpres1,bmpres2;
  12. long bmpPixoff;
  13. long bmpiSize;
  14. long bmpWidth; //BMP width
  15. long bmpHeight; //BMP height
  16. unsigned short int bmpPlanes;
  17. unsigned short int bmpBitsPixel;
  18. long bmpCompression; //if BMP is compressed
  19. long bmpImageSize;
  20. long bmpXscale;
  21. long bmpYScale;
  22. long bmpColor; //BMP color depth
  23. long bmpImpCol;
  24. long bmpTotalStuffablechar; //ASCII stuffable character in bitmap
  25.  
  26. // methods...
  27. BmpFile(char *filename); //constructor #
  28. ~BmpFile(); //destructor
  29. int isFileExist(char *filename); //checking file existence
  30. int getDimension(char *filename, long *width, long *height); //get height & width
  31. int readHeader(char *filename); //get the header info #
  32. int hide(char *bmpfile, char *txtfile, char *output); //hidding job *
  33. int unhide(char *bmpfile,char *txtfile); //unhidding job *
  34. int checkFilesForHiding(char *bmpfile, char *txtfile); //gets file ready
  35. int printFileInfo();
  36. };
  37.  
  38.  
  39. // checks for existence of file, returns 0 if ok, returns -1 on error
  40. int BmpFile::isFileExist(char *filename)
  41. {
  42. FILE *bmpFilename;
  43. bmpFilename = fopen(filename,"rb");
  44.  
  45. if(bmpFilename == NULL)
  46. {
  47. throw("File doesn't exits or wrong Filename.\n");
  48. return (-1);
  49. }
  50.  
  51. fclose(bmpFilename);
  52. return 0;
  53. }
  54.  
  55. // read header of file and other stuff, important method.
  56. int BmpFile::readHeader(char *filename)
  57. {
  58. char m1,m2;
  59. unsigned long width, height, fileSize;
  60.  
  61. if(isFileExist(filename) == 0)
  62. {
  63. FILE *bmpFilename = fopen(filename,"rb");
  64.  
  65. // reading first two part of file.
  66. fread((char *)&m1,1,1,bmpFilename);
  67. fread((char *)&m2,1,1,bmpFilename);
  68.  
  69. if(m1 != 'B' || m2 != 'M') //B 42 & M 4D
  70. {
  71. throw ("error: not a valid bitmap file");
  72. fclose(bmpFilename);
  73. return -1;
  74. }
  75.  
  76. bmpIdentifier = m1 * 100 + m2;
  77.  
  78. fread((long *)&bmpFilesize,4,1,bmpFilename);
  79.  
  80. fread((unsigned short int *)&bmpres1,2,1,bmpFilename);
  81.  
  82. fread((unsigned short int *)&bmpres2,2,1,bmpFilename);
  83.  
  84. fread((long *)&bmpPixoff,4,1,bmpFilename);
  85.  
  86. fread((long *)&bmpiSize,4,1,bmpFilename);
  87.  
  88. fread((long *)&bmpWidth,4,1,bmpFilename);
  89.  
  90. fread((long *)&bmpHeight,4,1,bmpFilename);
  91.  
  92. fread((unsigned short int *)&bmpPlanes,2,1,bmpFilename);
  93.  
  94. fread((unsigned short int *)&(bmpBitsPixel),2,1,bmpFilename);
  95.  
  96. fread((long *)&bmpCompression,4,1,bmpFilename);
  97.  
  98. fread((long *)&bmpImageSize,4,1,bmpFilename);
  99.  
  100. fread((long *)&bmpXscale,4,1,bmpFilename);
  101.  
  102. fread((long *)&bmpYScale,4,1,bmpFilename);
  103.  
  104. fread((long *)&bmpColor,4,1,bmpFilename);
  105.  
  106. fread((long *)&bmpImpCol,4,1,bmpFilename);
  107.  
  108. fclose(bmpFilename);
  109.  
  110. width = bmpWidth;
  111. height = bmpHeight;
  112. fileSize = width * height * 3;
  113. bmpTotalStuffablechar = (fileSize/8)-54;
  114.  
  115. return 0;
  116. }
  117. return -1;
  118. }
  119.  
  120. //0 if ok , -1 on err
  121. int BmpFile::getDimension(char *filename, long *width, long *height)
  122. {
  123. if(isFileExist(filename) == 0)
  124. {
  125. readHeader(filename);
  126. *width = bmpWidth;
  127. *height = bmpHeight;
  128. return 0;
  129. }
  130. return -1;
  131. }
  132.  
  133. // class contructor
  134. BmpFile::BmpFile(char *filename)
  135. {
  136. if(isFileExist(filename) == 0)
  137. {
  138. readHeader(filename);
  139. }
  140. }
  141.  
  142. // checking for file existence and compability.
  143. int BmpFile::checkFilesForHiding(char *bmpfile, char *txtfile)
  144. {
  145. FILE *bfile, *tfile;
  146. long l = 0;
  147.  
  148. // check and Initialize files...
  149. bfile = fopen(bmpfile,"rb");
  150.  
  151. if(isFileExist(bmpfile) == -1)
  152. {
  153. throw("BMP file doesn't exist");
  154. return -1;
  155. }
  156.  
  157. tfile = fopen(txtfile,"rb");
  158. if(isFileExist(txtfile)== -1)
  159. {
  160. throw("txt file doesn't exist");
  161. return -1;
  162. }
  163.  
  164. while( ! feof(tfile) ) // read the length of txt file...
  165. {
  166. fgetc(tfile);
  167. l++;
  168. }
  169.  
  170. readHeader(bmpfile);
  171.  
  172. if(l >= bmpTotalStuffablechar)
  173. {
  174. throw "length of text file too big , select bigger bmp file";
  175. return -1;
  176. }
  177.  
  178. fclose(bfile);
  179. fclose(tfile);
  180. return 0;
  181. }
  182.  
  183. // encoding implementation...
  184. int BmpFile::hide(char *bmpfile, char *txtfile, char *output)
  185. {
  186.  
  187. FILE *bfile, *tfile, *outfile;
  188. unsigned char header[54];
  189. char bmpbuffer, txtbuffer;
  190. int i;
  191. char txtTerminatorIndicator = '*';
  192.  
  193. // check and Initialize bmp & txt files...
  194. if(checkFilesForHiding(bmpfile, txtfile) == -1)
  195. {
  196. throw "error!, initialization failed...";
  197. return -1;
  198. }
  199.  
  200. bfile = fopen(bmpfile,"rb");
  201. tfile = fopen(txtfile,"rb");
  202. outfile = fopen(output,"w+b");
  203.  
  204. fread(header,54,1,bfile); //read BMP header
  205. fwrite(header,54,1,outfile); //write BMP header
  206. bmpFilesize = bmpFilesize - 54;
  207.  
  208. // main hiding/encoding process
  209. while(!feof(tfile))
  210. {
  211. txtbuffer = fgetc(tfile);
  212. for(i = 0; i<8; i++)
  213. {
  214. bmpbuffer = fgetc(bfile);
  215. bmpbuffer &= 0xFE; //FE, to make sure LSB is always zero
  216. bmpbuffer |= (char)((txtbuffer >> i) & 1);
  217. fputc(bmpbuffer,outfile);
  218.  
  219. bmpTotalStuffablechar--;
  220. bmpFilesize--;
  221. }
  222. }
  223.  
  224. // stuffing txt terminator indicator.
  225. for(i = 0; i < 8; i++)
  226. {
  227. bmpbuffer = fgetc(bfile);
  228. bmpbuffer &= 0xFE;
  229. bmpbuffer |= (char)((txtTerminatorIndicator >> i) & 1);
  230. fputc(bmpbuffer,outfile);
  231. bmpTotalStuffablechar--;
  232. bmpFilesize--;
  233. }
  234.  
  235. // write remaing bmp bytes into the new bmp file.
  236. if(bmpFilesize != 0)
  237. {
  238. while( ! feof(bfile) )
  239. {
  240. fputc(fgetc(bfile), outfile);
  241. }
  242. }
  243.  
  244. // close all file handlers
  245. fclose(bfile);
  246. fclose(tfile);
  247. fclose(outfile);
  248.  
  249. return 0;
  250. }
  251.  
  252. // encoding implementation...
  253. int BmpFile::unhide (char *bmpfile, char *txtfile)
  254. {
  255. if(readHeader(bmpfile) == -1)
  256. return -1;
  257.  
  258. FILE *bfile, *tfile;
  259. char ch, bmpBuffer[8];
  260. int i;
  261. bfile = fopen(bmpfile,"rb");
  262. tfile = fopen(txtfile,"w+b");
  263.  
  264. fseek(bfile, 54, SEEK_SET); //skip the BMP header part
  265. ch = 0;
  266.  
  267. while(!feof(bfile))
  268. {
  269. //read the last bit from BMP file
  270. ch = 0;
  271. for(i=0; i<=7; i++)
  272. {
  273. bmpBuffer[i] = fgetc(bfile);
  274. }
  275.  
  276. for(i=7; i>=0; i--)
  277. {
  278. ch += (bmpBuffer[i] & 1);
  279. if(i != 0)
  280. ch <<= 1;
  281. }
  282.  
  283. if(ch == EOF || ch == '*')
  284. {
  285. break;
  286. }
  287. else
  288. {
  289. fputc(ch,tfile);
  290. }
  291. }
  292.  
  293. fclose(bfile);
  294. fclose(tfile);
  295.  
  296. return 0;
  297. }
  298.  
  299. // destructor implementation
  300. BmpFile::~BmpFile ()
  301. {}
  302.  
  303. // bmp header information
  304. int BmpFile::printFileInfo()
  305. {
  306. //readHeader(filename)
  307. std::cout<<"identifier : "<<bmpIdentifier<<std::endl;
  308. std::cout<<"filesize : "<<bmpFilesize<<std::endl;
  309. std::cout<<"res1 : "<<bmpres1<<std::endl;
  310. std::cout<<"res2 : "<<bmpres2<<std::endl;
  311. std::cout<<"pixoff : "<<bmpPixoff<<std::endl;
  312. std::cout<<"isize : "<<bmpiSize<<std::endl;
  313. std::cout<<"Width : "<<bmpWidth<<std::endl;
  314. std::cout<<"Height : "<<bmpHeight<<std::endl;
  315. std::cout<<"Plane : "<<bmpPlanes<<std::endl;
  316. std::cout<<"BitPixel : "<<bmpBitsPixel<<std::endl;
  317. std::cout<<"compression : "<<bmpCompression<<std::endl;
  318. std::cout<<"imageSize : "<<bmpImageSize<<std::endl;
  319. std::cout<<"Xscale : "<<bmpXscale<<std::endl;
  320. std::cout<<"YScale : "<<bmpYScale<<std::endl;
  321. std::cout<<"color : "<<bmpColor<<std::endl;
  322. std::cout<<"ImpCol : "<<bmpImpCol<<std::endl;
  323. std::cout<<"---------------------------------------"<<std::endl;
  324. std::cout<<"Width : " << bmpWidth << std::endl;
  325. std::cout<<"Height : " << bmpHeight << std::endl;
  326. std::cout<<"comprss : "<< bmpCompression<<std::endl;
  327. std::cout<<"FileSize : " << bmpFilesize <<std::endl;
  328. std::cout<<"Stuffable ASCII : " << bmpTotalStuffablechar << std::endl;
  329.  
  330. return 0;
  331. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement