Solved

Convert image to byte array

Posted on 2014-12-03
8
173 Views
Last Modified: 2015-05-28
I would like some code or worst case a utility that can take an image, .jpeg, .bmp, .... and convert it to it's hexidecimal representation in an array. Code wise just straight c. NO C##, VB, etc.

Thank You
0
Comment
Question by:atomicgs12
8 Comments
 
LVL 24

Expert Comment

by:chaau
ID: 40479877
If C++ is an option and it is a Windows environment then have a look at CImage class. Requires MFC.

Otherwise, have a look at GOCR library. The library itself is a OCR library. You do not have to use it, however there are two files there pnm.h and pnm.c that can handle many of known image files. have a look at readpnm() function there. I have used this library for OCR purposes in one of my projects. You can just use the pnm files, I guess, check its license.
0
 

Author Comment

by:atomicgs12
ID: 40480910
Thanks but only C, I tried to cover all the languages not to use guess I have to make my list longer.
No C#, No VB, No C++, No Python, No Pascal, No Fortron, No HTML, No Silverlight, No...
0
 

Author Comment

by:atomicgs12
ID: 40480961
Looking at GOCR file pnm.c I don't see a ' readpnm() ' I do see a 'readpgm()' which I'm assuming your referring to. Even in that function I don't see an explanation as to to the input variables. I'm assuming 'char *name' is the name of the picture file, put I don't know what 'pix *p' and 'int vvv' are referring to.
0
How to run any project with ease

Manage projects of all sizes how you want. Great for personal to-do lists, project milestones, team priorities and launch plans.
- Combine task lists, docs, spreadsheets, and chat in one
- View and edit from mobile/offline
- Cut down on emails

 
LVL 32

Accepted Solution

by:
sarabande earned 500 total points
ID: 40481391
you may use the following code:

int binfile2hexstring1(const char * inputfile, char hexbuf[], int sizbuf)
{
     char hexdigits[] = "0123456789ABCDEF";
     struct stat fs = { 0 }; 
     FILE * file;
     char * buf;
     int nread;
     char ac[3];
     int n;
     char * p, * q;

     if (stat(inputfile, &fs) != 0 || fs.st_size == 0)
          return errno;   // file does not exist or is empty
      if (sizbuf < 2*fs.st_size)
           return -1;  // hex buffer to small
      file = fopen(inputfile, "r");
      buf =  (char*)calloc(fs.st_size);
      if (file == NULL ||buf == NULL)
            return errno;
     if ((nread = fread(&buf[0], 1, fs.st_size, file)) < (int)fs.st_size)
          return errno;  // invalid read
     fclose(file);
     p = &hexbuf[0];
     q = &buf[0];
     for (n = 0; n < (int)fs.st_size; ++n)    
     {
       *p++ = hexdigits[((*q)&0xf0)>>4];  // convert high half-byte
       *p++ = hexdigits[((*q)&0x0f)];         // convert low half-byte
         q++;
     }     
     return 0;
}

Open in new window


the code is pure c "derived" from an older question where we not only discussed conversion to hex but also differences in speed using c or c++ solutions.
 
see http://www.experts-exchange.com/Programming/Languages/CPP/Q_28457320.html for more.

Sara
0
 

Expert Comment

by:William Sherif
ID: 40510293
I would use SDL https://www.libsdl.org/.

Load the image using IMG_Load (or if you're using a png, use the SDL_Image 2.0 library).

After you load the image, you can inspect the contents of the surface using the pixels member of the SDL_Surface object (see https://wiki.libsdl.org/SDL_Surface).

If you really want a hold-your-hand start up tutorial, check out my udemy course https://www.udemy.com/creating-games-from-scratch-using-sdl-2/
0
 

Author Comment

by:atomicgs12
ID: 40707609
Need to try the suggested solution before selecting a correct answer(s).
Thank you all
0

Featured Post

Highfive + Dolby Voice = No More Audio Complaints!

Poor audio quality is one of the top reasons people don’t use video conferencing. Get the crispest, clearest audio powered by Dolby Voice in every meeting. Highfive and Dolby Voice deliver the best video conferencing and audio experience for every meeting and every room.

Join & Write a Comment

Suggested Solutions

Title # Comments Views Activity
scoresClump  challenge 31 108
scoreUp challenge 14 48
changePi Challenge 15 75
SUM 2 INTEGER ARRAYS INTO 1 10 58
A short article about a problem I had getting the GPS LocationListener working.
A short article about problems I had with the new location API and permissions in Marshmallow
The goal of this video is to provide viewers with basic examples to understand and use conditional statements in the C programming language.
The goal of this video is to provide viewers with basic examples to understand and use switch statements in the C programming language.

705 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

19 Experts available now in Live!

Get 1:1 Help Now