Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <sys/stat.h>
- #include <iostream>
- #include <fstream>
- #include <stdio.h>
- using namespace std;
- // Developed by: M. Robertson
- //Code to derive the Least Significant Bit from a 16bit based file.
- // An example of this code is as follows:
- //sokalli2 thaipos: if you have say an array of 8 bytes and a variable bits then
- // for (int i=0; i<8; ++i) bits = (bits << 1) | (bytes[i] & 1);
- int main ()
- {
- int dataStart = 40; //This is the 'start' of the data of our file.
- // If your header length is 44 bytes, this should be 44,
- // due to index start of 0.
- //It would also be wise to open up a hex editor to find the
- // specific spot the data starts. For this file, despite
- // being a .wav, it was at the 40th byte.
- int dataEnd = 3000000; //Put this to a particularly high number
- //if you don't feel like finding out the filesize.
- int byteLength = 0;
- char bits;
- //Make SURE this file is placed wherever the code is being executed from.
- fstream myFile("dinosaur_noise.wav", ios::in | ios::out | ios::binary);
- ofstream saveFile("dinoNoise_LSB");
- char ch;
- cout << "Least Significant Bit extractor for 16bit based files.\n";
- //TODO: need to put in an if statement to make sure the file exists.
- /*/Uncomment to grab the header...
- for(int i = 0; i < dataStart; i++){
- myFile.seekg(i);
- myFile.get(ch);
- saveFile << ch;
- }//*/
- //This loop goes through the entire file / dataSet as designated above.
- for(int i = dataStart; i < dataEnd; i++){
- myFile.seekg(i);//This forces the index of the currently viewed byte
- myFile.get(ch); //This pulls the currently viewed byte into the ch variable
- //We check at our current index to see if we're the 16th bit in.
- // However, we must subtract our dataStart to compensate that we may not
- // be starting at 0, and +1 due to the nature of C++ starting its file stream
- // at index 0.
- if(((i-dataStart)+1)%16 == 0){
- cout << i << endl;
- //The byte doesn't necessarily need to be reset,
- // but is good practice to prevent strange data occurances.
- bits = 0x00;
- byteLength++; //Keep a count for how many times we delve in to this.
- // This will count the number of bytes total in the program.
- for(int j = 15; j >= 0; j--){
- j--; //Comment this line, change to %8, and j = 7 to grab for 8bit.
- myFile.seekg(i-j);
- myFile.get(ch);
- //cout << ch << "--"; //Some debug; don't show unless
- //cout << (ch&0x01) << endl; //you're only pulling a couple of bytes.
- //saveFile << ch; //Also for debugging purposes.
- //The magic bitwise:
- // bits << 1 : Every time a bit is added, move all the bits over 1.
- // ch & 1 : Grab the last bit from the char byte.
- // | (or) after bit shift and grabbing the last bit adds in a single bit
- // to the end of the byte total.
- bits = (bits << 1) | (ch & 1);
- }
- //Save our totaled byte to our file.
- saveFile << bits;
- }
- //cout << (ch);
- //This is to allow a larger number for the for loop than the file.
- // If we reach the end of the file, simply break out.
- if(myFile.eof()){
- cout << ch;
- break;
- }
- }
- cout << "\nSuccess. The File is " << byteLength << " bytes long.";
- saveFile.close();
- myFile.close();
- char anything;
- cin >> anything;
- return 0;
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement