load jpg as bytearray?

hi, i want to load a jpg file as bytearray. but i really dont know how. i know freeimage has a memorystream load, but must the file be loaded allready?
thanks

do you want to load a file raw into memory, or memory into a freeImage structure?

if it’s the first - that’s not so hard – I post this snippet, taken from

http://www.cplusplus.com/reference/clib-…-fread.html

  
  
/* fread example: read a complete file */  
#include <stdio.h>  
#include <stdlib.h>  
  
.......  
  
  FILE * pFile;  
  long lSize;  
  char * buffer;  
  size_t result;  
  
  pFile = fopen ( "test.jpg" , "rb" );   // rb = read binary, **very** important for jpgs / binary files  
  if (pFile==NULL) {fputs ("File error",stderr); exit (1);}  
  
  // obtain file size:  
  fseek (pFile , 0 , SEEK_END);  
  lSize = ftell (pFile);  
  rewind (pFile);  
  
  // allocate memory to contain the whole file:  
  buffer = (char*) malloc (sizeof(char)*lSize);  
  if (buffer == NULL) {fputs ("Memory error",stderr); exit (2);}  
  
  // copy the file into the buffer:  
  result = fread (buffer,1,lSize,pFile);  
  if (result != lSize) {fputs ("Reading error",stderr); exit (3);}  
  
  /* the whole file is now loaded in the memory buffer "buffer" */  
  
  // close the file  
  fclose (pFile);  
....  
  

hope that helps!
best,
zach

zach.
thanks a lot. in fact i was just looking at cplusplus reference in this moment and trying it.
ill try and ask again if i find any trouble.

as for memory in to a freeimage structure ive found that freeimage has read from memory stream fucntions. so, ill try that way

i’ve found two problems so far. the exit () function is not defined, so i replaced with abort().
the program breaks here

if (pFile==NULL) {fputs ("File error ",stderr); abort();}

im loading this file

pFile = fopen ( “bg.jpg” , “rb” );

the file is in the data dir

try replacing exit with std::exit()

and

pFile = fopen ( “bg.jpg” , “rb” );

with

string fileName = ofToDataPath(“bg.jpg”);
pFile = fopen ( fileName.c_str(), “rb” );

(ofToDataPath appends bg.jpg with the right prefix to get to the data directory)

hope that helps!
best,
zach

it works, but now another problems. i send the buffer contents to the console. i was especting signed char data (-128 to 127) but it showr a mix of characters and numbers max 377 i think) are those unicode index? how do i get the signed chars or meaby hex values?

if you output them to the console, they will come out now as chars, which are characters.

however, you can print them like:

printf( “%i \n”, (int)buffer[0]);

and you will see them as ints…

take care!
zach