Advertisement
Guest User

Untitled

a guest
Jan 15th, 2014
230
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 2.70 KB | None | 0 0
  1. #include <iostream>
  2. #include <string.h>
  3. #include <fstream>
  4. #include <string>
  5.  
  6. using namespace std;
  7.  
  8. char convertToHex(char c);
  9.  
  10. int main(int argc, char *argv[]){
  11.    
  12.     if (argc != 4){
  13.         cout << endl;
  14.         cout << "*********** JpgFromText ***********" << endl << endl;
  15.         cout << "You haven't provided all the parameters." << endl;
  16.         cout << "The correct format is:" << endl << endl;
  17.         cout << "\tjpgfromtext.exe [-f/-r] <input txt file> <output jpg file>" << endl << endl;
  18.         cout << "-f - foreward direction" << endl;
  19.         cout << "-r - reverse direction" << endl;
  20.         return 1;
  21.     }
  22.  
  23.     if (strcmp(argv[1], "-f") != 0 && strcmp(argv[1], "-r") != 0){
  24.         cout << "Invalid options parameter: options are -f or -r." << endl;
  25.         return 1;
  26.     }
  27.  
  28.     ifstream is(argv[2]);
  29.     ofstream os(argv[3], ofstream::binary);
  30.  
  31.     char bout = 0x00;
  32.     char boutprev = bout;
  33.     bool control = true;
  34.  
  35.     if (strcmp(argv[1], "-f") == 0){
  36.         while (is.good()) {
  37.             char b = is.get();
  38.             b = convertToHex(b);
  39.  
  40.             if (b != (char)0xff){
  41.                 if (control) {
  42.                     bout = bout | b;
  43.                     control = false;
  44.                 }
  45.                 else {
  46.                     bout = bout << 4;
  47.                     bout = bout | b;
  48.                     os.put(bout);
  49.                     if (bout == 0xd9 && boutprev == 0xff) break;
  50.                     boutprev = bout;
  51.                     bout = 0x00;
  52.                     control = true;
  53.                 }
  54.             }
  55.         }
  56.     } else {
  57.         string data;
  58.         while (is.good()) {
  59.             data += is.get();
  60.         }
  61.         string reversed;
  62.         string::iterator iter = data.end();
  63.         iter -= 3;
  64.         for (; iter != data.begin(); iter -= 2){
  65.             string::iterator temp;
  66.             temp = iter;
  67.             reversed += *temp;
  68.             temp++;
  69.             reversed += *(temp);
  70.         }
  71.        
  72.         for(string::iterator iter = reversed.begin(); iter!=reversed.end(); iter++) {
  73.             char b = *iter;
  74.  
  75.             b = convertToHex(b);
  76.             if (b != (char)0xff){
  77.                 if (control) {
  78.                     bout = bout | b;
  79.                     control = false;
  80.                 }
  81.                 else {
  82.                     bout = bout << 4;
  83.                     bout = bout | b;
  84.                     os.put(bout);
  85.                     if (bout == (char)0xd9 && boutprev == (char)0xff){
  86.                         is.close();
  87.                         os.close();
  88.                         return 0;
  89.                     }
  90.                     boutprev = bout;
  91.                     bout = 0x00;
  92.                     control = true;
  93.                 }
  94.             }
  95.         }
  96.  
  97.  
  98.     }
  99.    
  100.     os.close();
  101.     is.close();
  102.     return 0;
  103. }
  104.  
  105. char convertToHex(char c){
  106.     char b;
  107.     switch (c){
  108.     case '0':b = 0x00;
  109.         break;
  110.     case '1':b = 0x01;
  111.         break;
  112.     case '2':b = 0x02;
  113.         break;
  114.     case '3':b = 0x03;
  115.         break;
  116.     case '4':b = 0x04;
  117.         break;
  118.     case '5':b = 0x05;
  119.         break;
  120.     case '6':b = 0x06;
  121.         break;
  122.     case '7':b = 0x07;
  123.         break;
  124.     case '8':b = 0x08;
  125.         break;
  126.     case '9':b = 0x09;
  127.         break;
  128.     case 'a':b = 0x0a;
  129.         break;
  130.     case 'b':b = 0x0b;
  131.         break;
  132.     case 'c':b = 0x0c;
  133.         break;
  134.     case 'd':b = 0x0d;
  135.         break;
  136.     case 'e':b = 0x0e;
  137.         break;
  138.     case 'f':b = 0x0f;
  139.         break;
  140.     default: b = 0xff;
  141.     }
  142.  
  143.     return b;
  144. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement