Reading JPEG and GIF

A place to discuss the implementation and style of computer programs.

Moderators: phlip, Moderators General, Prelates

captaineddie
Posts: 15
Joined: Wed Jan 30, 2008 3:30 pm UTC
Location: Canada, Ontario
Contact:

Reading JPEG and GIF

Postby captaineddie » Fri Jul 02, 2010 1:19 pm UTC

What I need to achieve: Reading a JPEG and GIF file and then saving it, pixel by pixel, into a plain text document, each pixel is represented by a RGB value.

512 512 (width and height)
0 0 0 (pixel 1)
0 1 0 (pixel 2)
134 523 134 (pixel 3)
...

Is there an easy library to assist me with this? I'm working with C here.
Last edited by captaineddie on Fri Jul 02, 2010 2:46 pm UTC, edited 1 time in total.

User avatar
Cosmologicon
Posts: 1806
Joined: Sat Nov 25, 2006 9:47 am UTC
Location: Cambridge MA USA
Contact:

Re: Reading JPEG and GIF

Postby Cosmologicon » Fri Jul 02, 2010 2:33 pm UTC

This is extremely close to the PPM image format. I would start by converting the image files to PPM, which can be done using lots of programs, like Gimp and ImageMagick. Depending on exactly what you need, that might be enough, and in any event it'll be easy from there.

captaineddie
Posts: 15
Joined: Wed Jan 30, 2008 3:30 pm UTC
Location: Canada, Ontario
Contact:

Re: Reading JPEG and GIF

Postby captaineddie » Fri Jul 02, 2010 2:46 pm UTC

Shoot, forgot to mention, that I'm doing this in C....

User avatar
Cosmologicon
Posts: 1806
Joined: Sat Nov 25, 2006 9:47 am UTC
Location: Cambridge MA USA
Contact:

Re: Reading JPEG and GIF

Postby Cosmologicon » Fri Jul 02, 2010 2:55 pm UTC

Okay, the ImageMagick library has a C API called MagickWand [link]. I haven't used it, but I'm sure it can handle this.

User avatar
PM 2Ring
Posts: 3713
Joined: Mon Jan 26, 2009 3:19 pm UTC
Location: Sydney, Australia

Re: Reading JPEG and GIF

Postby PM 2Ring » Sat Jul 03, 2010 3:24 pm UTC

Does it have to be in C? If so, any modern graphics library, like GTK, should be able to read JPEG, GIF, etc for you. I guess you could use the official JPEG library, but that way lies madness. :)

Personally, I'd use existing programs, probably a NetPBM program to do the conversion to plain ppm & sed to massage it exactly into the desired format.

I'm curious. Why do you want to do this? Graphics files in ASCII are huge & unwieldy beasts. :)

makc
Posts: 181
Joined: Mon Nov 02, 2009 12:26 pm UTC

Re: Reading JPEG and GIF

Postby makc » Sun Jul 18, 2010 4:27 pm UTC

if you're on windows, there's built-in GDI+ library.

here's a func:

Code: Select all

#include <atlbase.h> // CComBSTR (also helps gdiplus.h to compile :)
#include <gdiplus.h> // (add gdiplus.lib)
#define STRIDE_3ALIGN4(W) (((3*((W)+1))>>2)*4)

   unsigned char *loadBitmap (char *fname) {
   // Create initial Bitmap object
   CComBSTR bstrfname (fname);
   Gdiplus::Bitmap *pBitmap = Gdiplus::Bitmap::FromFile (bstrfname);
   if (pBitmap == NULL) {
      return NULL;
   }

   // Get bitmap data in RGB-24 format (do we really need to initialize bd here ?)
   Gdiplus::BitmapData bd; bd.Scan0 = NULL; bd.PixelFormat = PixelFormat24bppRGB;
   bd.Width = pBitmap->GetWidth (); bd.Height = pBitmap->GetHeight (); bd.Stride = STRIDE_3ALIGN4 (bd.Width);
   Gdiplus::Rect rect (0, 0, (INT)bd.Width, (INT)bd.Height);
   Gdiplus::Status res = pBitmap->LockBits (&rect, Gdiplus::ImageLockModeRead, bd.PixelFormat, &bd);
   if ((res != Gdiplus::Ok) || (bd.PixelFormat != PixelFormat24bppRGB)) {
      return NULL;
   }

   // construct valid BITMAPINFOHEADER in pBuffer and append bd data there
   unsigned char *pBuffer;
   try {
      pBuffer = new unsigned char [sizeof (BITMAPINFOHEADER) + STRIDE_3ALIGN4 (bd.Width) * bd.Height];
   } catch (...) {
      // swallow any allocation errors here to return NULL
   }

   if (pBuffer == NULL) {
      return NULL;
   }

   PBITMAPINFOHEADER pbih = (PBITMAPINFOHEADER) pBuffer; ZeroMemory (pbih, sizeof (BITMAPINFOHEADER));
   pbih->biBitCount = 24; pbih->biCompression = BI_RGB; pbih->biHeight = bd.Height; pbih->biWidth = bd.Width;
   pbih->biPlanes = 1; pbih->biSize = sizeof (BITMAPINFOHEADER); pbih->biSizeImage = STRIDE_3ALIGN4 (pbih->biWidth) * pbih->biHeight;
   pbih->biXPelsPerMeter = pbih->biYPelsPerMeter = 0; // TODO: aspect ???

   memcpy ((LPBYTE)pbih + (WORD)(pbih->biSize), bd.Scan0, pbih->biSizeImage);

   // Release GDI+ stuff
   pBitmap->UnlockBits (&bd); delete pBitmap;

   return pBuffer;
}

also, you need to init/deinit GDI+ once per app:

Code: Select all

   
   Gdiplus::GdiplusStartupInput gdiplusStartupInput; ULONG_PTR gdiplusToken;
   Gdiplus::GdiplusStartup(&gdiplusToken, &gdiplusStartupInput, NULL);
   ...
   Gdiplus::GdiplusShutdown(gdiplusToken);

User avatar
hotaru
Posts: 1045
Joined: Fri Apr 13, 2007 6:54 pm UTC

Re: Reading JPEG and GIF

Postby hotaru » Sun Jul 18, 2010 6:22 pm UTC

makc wrote:if you're on windows, there's built-in GDI+ library.

this GDI+ library?

Code: Select all

factorial product enumFromTo 1
isPrime n 
factorial (1) `mod== 1

User avatar
You, sir, name?
Posts: 6983
Joined: Sun Apr 22, 2007 10:07 am UTC
Location: Chako Paul City
Contact:

Re: Reading JPEG and GIF

Postby You, sir, name? » Sun Jul 18, 2010 6:49 pm UTC

I'd like to add to the choir that says "don't reinvent the wheel."
I edit my posts a lot and sometimes the words wrong order words appear in sentences get messed up.

User avatar
Sc4Freak
Posts: 673
Joined: Thu Jul 12, 2007 4:50 am UTC
Location: Redmond, Washington

Re: Reading JPEG and GIF

Postby Sc4Freak » Sun Jul 18, 2010 10:59 pm UTC

hotaru wrote:
makc wrote:if you're on windows, there's built-in GDI+ library.

this GDI+ library?

Vulnerabilities in graphics libraries aren't exactly unusual or uncommon. Graphics libraries deal with lots of user data, operating on lots of raw data. Googling for "tiff library vulnerability" will return a whole lot of results detailing various security vulnerabilities in Ubuntu, for example.

User avatar
hotaru
Posts: 1045
Joined: Fri Apr 13, 2007 6:54 pm UTC

Re: Reading JPEG and GIF

Postby hotaru » Mon Jul 19, 2010 12:41 am UTC

Sc4Freak wrote:
hotaru wrote:this GDI+ library?

Vulnerabilities in graphics libraries aren't exactly unusual or uncommon. Graphics libraries deal with lots of user data, operating on lots of raw data. Googling for "tiff library vulnerability" will return a whole lot of results detailing various security vulnerabilities in Ubuntu, for example.

yeah, but remote code execution vulnerabilities aren't that common. and i know of at least two in GDI+ that haven't been fixed.

Code: Select all

factorial product enumFromTo 1
isPrime n 
factorial (1) `mod== 1

tuseroni
Posts: 124
Joined: Fri Jun 25, 2010 10:41 pm UTC

Re: Reading JPEG and GIF

Postby tuseroni » Wed Jul 21, 2010 3:31 am UTC

ive found imagick works, i made a c program which does just that (except it outputs the pixels as highlighted blocks for bb code to make big ass images...which...didnt quite work as planned...had to do with the board filtering certain runs of characters or something) using imagick (i think...its been a while..)
Image


Return to “Coding”

Who is online

Users browsing this forum: No registered users and 2 guests