-2

My supervisor developed a simulator (Its a collection of codes) that reads data from a file and converts them to a signal (for example optical signal, etc.), and then the simulator saves this signal to a .sgn file.

He asked me to read a .jpg image in VS 2019 and convert it to a signal of type byte and then save the signal to .sgn file. However, when I save the signal and change its extension to .jpg (in order to make sure that the signal contains the image data) it cannot be open.

I compared the information of the original image and my signal and I see some extra garbage in the signal as shown in pictures.

  • Original image

enter image description here

  • Resulted image(Signal image)

enter image description here

My questions are (thank you so much in advance):

  1. In order to solve this issue, should I read the image header file separately?

  2. Am I reading the image file correctly? (the simulator is huge, so I can not put all the codes)

  3. Do you have any other idea about where the problem is? like the buffer or something else.

    std::ifstream inFile;
    inFile.open("1.jpg");
    
    std::byte out;     // type of output signal
    
    int length = sizeof(std::byte);
    char * memblock = new char[length];
    
    for (int i = 0; i < process; i++) {   //this line is related to the circular buffer
    
    inFile.read(memblock, length);
    std::byte * byte_values = (std::byte*)memblock;
    out = *byte_values;
    outputSignals[0]->bufferPut(out);    // related to saving the output signal
    }
    delete[] memblock;
    
Kalana
  • 4,683
  • 6
  • 22
  • 46
  • it is probably a compression or header problem, can you post an example of the result image ? – phoenixstudio Jan 01 '20 at 15:09
  • You just seem to do a straight byte-by-byte copy of the file, without any actual processing of the image. Just like normal copying of the file. And as such, you need to copy the *whole* file (i.e. make sure that `processing` is equal to the byte-size of the input file). If you're supposed to be doing some processing of the actual image inside the JPEG file, then use a library to read and uncompress the image, and process only the image data (not other meta-data in the file, or the compressed image data which is useless). – Some programmer dude Jan 01 '20 at 15:10
  • 2
    ***when I save the signal and change its extension to .jpg (in order to make sure that the signal contains the image data) it cannot be open.*** Just giving data a `.jpg` extension does not make it a valid jpeg file. You need to use a jpeg library to read and write jpeg files. – drescherjm Jan 01 '20 at 15:11
  • `Am I reading the image file correctly?` no you don't read in the file correctly, whenever you read in nontextual data, you have set the input stream to `binary`, otherwise, you might not read everything. The same goes for writing. – t.niese Jan 01 '20 at 15:13
  • Hi @phoenixstudio, I have already uploaded the resulted image (signal image) at https://i.stack.imgur.com/kE8bI.jpg – Zeinab Rahmani Jan 01 '20 at 15:16

1 Answers1

2

jpeg and similar files contain binary data, hence you should open the file in binary mode for the data to be read correctly.

Simple Example

EvilTeach
  • 26,577
  • 21
  • 79
  • 136