binary to decimal question 
Author Message
 binary to decimal question

Hi,
trying to read a binary image data and create a file
showing decimal values of each pixel. the following
does not quite do it. Meaning. tried an image and the
values were not right.How to skip header of anYEver
bytes?. Got the function call from usenet.
Mk

#pragma warning(disable: 4786)
#include <algorithm>
#include <fstream>
#include <iostream>
#include <istream>
#include <iterator>
#include <ostream>
#include <string>
using namespace std;

std::ostream& xlate(std::istream& is, std::ostream&
os,
                    const char *delim = "")
{
    std::copy(std::istream_iterator<char>(is),
              std::istream_iterator<char>(),
              std::ostream_iterator<int>(os, delim));

    return os;

Quote:
}

void main(void)
{
      std::ifstream file("c:\\image.jpg");
    if(!file)
        {
            std::cout<<"No Such File"<<std::endl;
            return;
        }
      std::ofstream fp;
      fp.open ("c:\\file.txt", ios::out);
      if (!fp)
            {
            cout<<"Failed to create file"<<endl;
            return;
            }
      xlate(file,fp,",");
      file.close();
      fp.close();

Quote:
}

--
Sent by mike333_n from yahoo  in  area com
This is a spam protected message. Please answer with reference header.
Posted via http://www.*-*-*.com/


Sat, 30 Apr 2005 23:43:36 GMT  
 binary to decimal question

on comp.lang.c:

Quote:
> Hi,
> trying to read a binary image data and create a file
> showing decimal values of each pixel. the following
> does not quite do it. Meaning. tried an image and the
> values were not right.How to skip header of anYEver
> bytes?. Got the function call from usenet.
> Mk
> #pragma warning(disable: 4786)
> #include <algorithm>
> #include <fstream>
> #include <iostream>
> #include <istream>
> #include <iterator>
> #include <ostream>
> #include <string>
> using namespace std;

(Snip)

Please Keep Our Newsgroup Tidy - C++ code belongs in comp.lang.c++.

--

| Kingpriest of "The Flying Lemon Tree" G++ FR FW+ M- #108 D+ ADA N+++|
| http://www.helsinki.fi/~palaste       W++ B OP+                     |
\----------------------------------------- Finland rules! ------------/
"I will never display my bum in public again."
   - Homer Simpson



Sun, 01 May 2005 00:02:45 GMT  
 binary to decimal question


Quote:
> Hi,
> trying to read a binary image data and create a file
> showing decimal values of each pixel. the following
> does not quite do it. Meaning. tried an image and the
> values were not right.How to skip header of anYEver
> bytes?. Got the function call from usenet.
> Mk

don't know what anYEver means. can't decipher it. and, how do you get the
function call from UseNet? are you doing SOAP over nntp?

Quote:
> void main(void)

there is no such thing in standard C or C++.

i don't know what you are trying to do but maybe you can use the code
below as a starting point.

#include <stdio.h>
#include <stdlib.h>

void usage(void)
{
        fputs("Usage: trbinhex input_file\n", stderr);

Quote:
}

int main(int argc, char *argv[])
{
        int status = EXIT_FAILURE;

        if(argc != 2)
        {
                usage();
        }
        else
        {
                FILE *fin = fopen(argv[1], "rb");
                if(fin == NULL)
                {
                        perror(argv[1]);
                }
                else
                {
                        int c;
                        size_t count = 0;
                        while((c = fgetc(fin)) != EOF)
                        {
                                fprintf(stdout, "%2.2X ", (unsigned) c);
                                ++count;
                                if((count % 16) == 0)
                                        fputc('\n', stdout);
                        }
                        fputc('\n', stdout);
                        fclose(fin);
                        status = EXIT_SUCCESS;
                }
        }
        return status;

Quote:
}

--
A. Sinan Unur

Remove dashes for address



Sun, 01 May 2005 00:43:00 GMT  
 binary to decimal question



Quote:
> Hi,
> trying to read a binary image data and create a file
> showing decimal values of each pixel. the following
> does not quite do it. Meaning. tried an image and the
> values were not right.How to skip header of anYEver
> bytes?. Got the function call from usenet.
> Mk

You certainly got that function call from somewhere, but what exactly have
you been looking for? If I get you correctly then you want to output the
decimal color values for each pixel of an image. The code below just opens a
file, reads in every byte as a character and writes its decimal value to a
file. That's it, but that's obviously not what you want.

Your task is actually a little more difficult because you need to know the
file format of the image file you want to read. Thus decide on a file format
and get a format description that tells you about the header and where the
actual pixel information is stored. Furthermore you need to know how it is
stored - RGB values (which color depth?) and so on. After getting this
information you should check out the read function of streams for binary
files. BTW skipping a specified number of bytes is done using the seekg
function.

Chris

Quote:
> #pragma warning(disable: 4786)
> #include <algorithm>
> #include <fstream>
> #include <iostream>
> #include <istream>
> #include <iterator>
> #include <ostream>
> #include <string>
> using namespace std;

> std::ostream& xlate(std::istream& is, std::ostream&
> os,
>                     const char *delim = "")
> {
>     std::copy(std::istream_iterator<char>(is),
>               std::istream_iterator<char>(),
>               std::ostream_iterator<int>(os, delim));

>     return os;
> }

> void main(void)

That should be int main()

Quote:
> {
>       std::ifstream file("c:\\image.jpg");
>     if(!file)
>         {
>             std::cout<<"No Such File"<<std::endl;
>             return;

main returns an int!

Quote:
>         }
>       std::ofstream fp;
>       fp.open ("c:\\file.txt", ios::out);
>       if (!fp)
>             {
>             cout<<"Failed to create file"<<endl;
>             return;

main returns an int!
Quote:
>             }
>       xlate(file,fp,",");
>       file.close();
>       fp.close();
> }

main returns an int!


Sun, 01 May 2005 01:07:47 GMT  
 binary to decimal question
Quote:

> Hi,
> trying to read a binary image data and create a file
> showing decimal values of each pixel. the following
> does not quite do it. Meaning. tried an image and the
> values were not right.How to skip header of anYEver
> bytes?. Got the function call from usenet.
> Mk

> #pragma warning(disable: 4786)
> #include <algorithm>
> #include <fstream>
> #include <iostream>
> #include <istream>
> #include <iterator>
> #include <ostream>
> #include <string>
> using namespace std;

> std::ostream& xlate(std::istream& is, std::ostream&
> os,
>                     const char *delim = "")
> {
>     std::copy(std::istream_iterator<char>(is),
>               std::istream_iterator<char>(),
>               std::ostream_iterator<int>(os, delim));

>     return os;
> }

> void main(void)

This must be:  int main(void).
The main() function returns int.  Always.

- Show quoted text -

Quote:
> {
>       std::ifstream file("c:\\image.jpg");
>     if(!file)
>         {
>             std::cout<<"No Such File"<<std::endl;
>             return;
>         }
>       std::ofstream fp;
>       fp.open ("c:\\file.txt", ios::out);
>       if (!fp)
>             {
>             cout<<"Failed to create file"<<endl;
>             return;
>             }
>       xlate(file,fp,",");
>       file.close();
>       fp.close();
> }

I believe that using the xlate function is creating more hassles.
Why not just try the tried & true method of reading in data,
translating the data and outputting the data?

ifstream  image_file("my_file", ios_base::in | ios_base::binary);
ofstream  analysis_file("image_analysis.txt");

// Skip over header
const unsigned int  HEADER_SIZE = ???;
unsigned char * buffer = new unsigned char [HEADER_SIZE];
image_file.read(buffer, HEADER_SIZE);
delete [] buffer;

// Process the image
unsigned char byte;
while (image_file.read(&byte, 1))
{
  analysis_file << (unsigned int) byte << ' ';

Quote:
}

Think in terms of pixels.  Read the image header and determine the
number of bytes per pixels; or pixels per byte.

Start simple and read one pixel, print out the value of the pixel.
Once that works, place it into a while loop as above.  For example,
some pixels contain RGB information so you might want to print out:
   Red: rrrr   Green: gggg  Blue: bbbbb

Once this program works, you could expand it by calling a translation
function instead of the output statement.

-- Thomas Matthews



Sun, 01 May 2005 03:13:54 GMT  
 
 [ 5 post ] 

 Relevant Pages 

1. Convert from binary to decimal, hex to decimal etc

2. Binary to Decimal function

3. Display Binary Equivalent To Decimal Value

4. Hexadecimal to binary, decimal in c#

5. Binary to decimal

6. Decimal, Hex, and Binary

7. decimal to binary

8. decimal to binary

9. conversion decimal<->binary

10. how to convert numbers in decimal to binary?

11. Binary to Decimal Function

12. Binary to decimal conversion.

 

 
Powered by phpBB® Forum Software