How to pass image file as pointer and reference as std::vector<char> ?


#include <string>
#include <sstream>
#include <fstream>
#include <vector>
#include <cstring>
#include <cstdlib>
#include <stdio.h>
#include <ostream>
#include <time.h>
#include <algorithm>
#include <iterator>
#include <iostream>
#include <iterator>
#include <stdio.h>
#include <stdlib.h>

using namespace std;


void use_vector()
{

std::ifstream source("fruit.bmp", std::ios::binary);
std::vector<char> vec((std::istreambuf_iterator<char>(source)),
std::istreambuf_iterator<char>());

for(unsigned int i = 54; i <vec.size(); i++)
vec[i] = 255-vec[i];

ofstream fout("data.dat", ios::out | ios::binary);
fout.write((char*)&vec[0], vec.size() * sizeof(char));
fout.close();

}
int main(int argc, char* argv[])
{

if (argc >= 4) {
// Tell the user how to run the program
std::cerr << " " << argv[0] << " Your BMP file is inverted" << std::endl;

use_vector();//Done

return 1;

}


// Print the user's name:
std::cout << argv[0] << " hello, " << argv[1] << "Zesan" << std::endl;


return 0;

}

Now I want to use std::vector<char>*vec and std::vector<char>&vec in above block code instead of std::vector<char>vec, I tried a lot but not getting any desired solution, please help me out.
Note that vec[i] = 255-vec[i]; may be out side the range of values that char can hold.
Consider using unsigned char to represent the bytes in the file.

Something like this, perhaps:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
#include <iostream>
#include <string>
#include <fstream>
#include <vector>
#include <iterator>

std::vector<unsigned char> get_bytes( const std::string& in_file_name )
{
    std::ifstream file( in_file_name, std::ios::binary ) ;
    using iterator = std::istream_iterator<unsigned char> ;
    return { iterator(file), iterator{} } ;
}

void put_bytes( const std::vector<unsigned char>& bytes, const std::string& out_file_name )
{
    std::ofstream file( out_file_name, std::ios::binary ) ;
    for( unsigned char c : bytes ) file.put(c) ;
}

std::vector<unsigned char>& convert( std::vector<unsigned char>& bytes )
{
    // static_assert( std::numeric_limits<unsigned char>::max() > 254, "out of range" ) ;
    for( unsigned char& c : bytes ) c = 255 - c ;
    return bytes ;
}

int main( int argc, char* argv[] )
{
    if( argc != 3 )
    {
        std::cerr << "usage: " << argv[0] << " <input file> <output file>\n" ;
        return 1 ;
    }

    const std::string in_file_name = argv[1] ;
    const std::string out_file_name = argv[2] ;

    auto vec = get_bytes(in_file_name) ;
    put_bytes( convert(vec), out_file_name ) ;
    std::cout << "input file '" << in_file_name << " was converted. output file '"
              << out_file_name << "'\n" ;
}
Thanks for your help, but I am confused why my compiler is showing many errors ?
What are your errors?

Are you compiling for one of the more current standards (C++11, C++14)?

I am running Qt 3.0.1 and g++-4.8, GNU C++ compiler 4.8.4-2ubuntu1~14.04.3, is something wrong with that ?
That doesn't tell me what standard you're using. To compile the code given by JLBorges you need to have your compiler set to use the C++11 or higher standard. It appears that you're probably compiling as a C++98 standard.

I am sorry to bother you guys again, I am very much thankful to get the code, it is instructed from the University like :

Pass the file as reference as std:vector<char> to the function and invert it,
Pass the file as Pointer to std:vector<char> to the function and invert it,
Pass the file as character array the function and invert it.

They only diifer from each other in passing way.

Q-1) Where it was done using std:vector<unsigned char> , code is above, so I am confused is that assignment wrong or it is possible according to requirement ?

Q-2) Using command line argument they are saying to call the application like below :

ImageConvert InputImage.bmp ResultImage.bmp USE_REFERENCE // Code I got above.
ImageConvert InputImage.bmp ResultImage.bmp USE_POINTER //Trying
ImageConvert InputImage.bmp ResultImage.bmp USE_VECTOR // Done by me
ImageConvert InputImage.bmp ResultImage.bmp USE_CHRACTERARRAY //Trying
Last edited on
> is that assignment wrong or it is possible according to requirement ?

Technically, the assignment is wrong; the value of 255-vec[i] may not fit into a char


> it is possible according to requirement ?

Yes; narrowing by itself is not an error. Because char is an octet and because the representation is a two's complement representation on every platform, we can get away with using a vector of char

This would be typical for mainstream implementations:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
#include <iostream>
#include <limits>

int main()
{
    using limits = std::numeric_limits<char> ;

    std::cout << std::boolalpha << "properties of the type char:\n"
              << "    signed integer type: " << limits::is_signed << '\n'
              << "  minimum integer value: " << int( limits::min() ) << '\n'
              << "  maximum integer value: " << int( limits::max() ) << "\n\n" ;

    for( unsigned char byte : { 20, 200 } ) // say, the values of two bytes in the bmp file
    {
        char ch = byte ;
        std::cout << "\ninteger value of byte is " << int(byte) << '\n'
                  << "  integer value of ch is " << int(ch) << '\n'
                  << "  integer value of 255-byte is " << (255-byte) << '\n'
                  << "  integer value of 255-ch is " << (255-ch) << '\n' ;

        ch = 255 - ch ;
        std::cout << "  integer value of 255-ch narrowed to char is " << int(ch)
                  << "\n---------------------------------\n" ;
    }
}

properties of the type char:
    signed integer type: true
  minimum integer value: -128
  maximum integer value: 127


integer value of byte is 20
  integer value of ch is 20
  integer value of 255-byte is 235
  integer value of 255-ch is 235
  integer value of 255-ch narrowed to char is -21
---------------------------------

integer value of byte is 200
  integer value of ch is -56
  integer value of 255-byte is 55
  integer value of 255-ch is 311
  integer value of 255-ch narrowed to char is 55
---------------------------------


properties of the type char:
    signed integer type: true
  minimum integer value: -128
  maximum integer value: 127


integer value of byte is 20
  integer value of ch is 20
  integer value of 255-byte is 235
  integer value of 255-ch is 235
  integer value of 255-ch narrowed to char is -21
---------------------------------

integer value of byte is 200
  integer value of ch is -56
  integer value of 255-byte is 55
  integer value of 255-ch is 311
  integer value of 255-ch narrowed to char is 55
---------------------------------

http://coliru.stacked-crooked.com/a/5c3038dffe605265
http://rextester.com/FBCKIE79925
Thank you for your nice feedback.
Topic archived. No new replies allowed.