Solved

Convert image to byte array

Posted on 2014-12-03
8
189 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
Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

 
LVL 33

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

Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

Title # Comments Views Activity
strCopies  challenge 17 83
advertisement module in core php 4 147
Turning python script into an applet 12 104
Why isn't object file created? 6 42
This is about my first experience with programming Arduino.
Whether you’re a college noob or a soon-to-be pro, these tips are sure to help you in your journey to becoming a programming ninja and stand out from the crowd.
The goal of this video is to provide viewers with basic examples to understand and use structures 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.

867 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

17 Experts available now in Live!

Get 1:1 Help Now