tags:

views:

118

answers:

2

i want to read the raw image data of a jpeg so i can manipulate it without loosing any quality when doing so.
i took a look at the LibJpeg.Net library http://bitmiracle.com/libjpeg .
but there is a lot of code and couldnt find anything about reading/writing raw blocks.
mainly i want to implement lossless editing of a jpeg image and was wondering how to do it?
http://en.wikipedia.org/wiki/Jpeg#Lossless_editing
thanks

Update:

basically what i want to do is to access the jpeg block data as some kind of array so i can read the data.
and then i want to create a new jpeg and access some kind of block data array that i can fill with rows from other images.
the tasks seems easy but the problem is that the documentation of libJpeg.net is not that clear.
i have reached this state of code now.

var cinfo = new BitMiracle.LibJpeg.Classic.jpeg_decompress_struct();
cinfo.jpeg_stdio_src(new System.IO.FileStream(@"C:\File.jpg", System.IO.FileMode.Open));
cinfo.jpeg_read_header(true);
cinfo.jpeg_start_decompress();

but that is it , i dont know where to find that array of MCU blocks or if i am even reading the right structure

+1  A: 

I'm not sure that jpeglib has public functions for that. They support lossless transformations in the jtransform_execute_transformation, but only have rotate90/280/2780, flip, crop, and transpose.

You'll have to read the source of that function to find out how it gets the blocks. A quick look shows it using

srcinfo->mem->access_virt_barray

Which is a ptr-to-function, where srcinfo is a j_decompress_ptr. It gets a JBLOCKARRAY from that.

I would try to read do_flip_v which should be an easy transformation to understand.

Lou Franco
i dont understand to which version of jpeglib you refer? because i am looking @ Libjpeg.net which dont have such line of code (also the line of code is C and not c#)
Karim
I don't know libjpeg.net -- so hopefully there's something similar to look for. I am looking at libjpeg (the C version). You can wrap it in .NET if you want to.
Lou Franco
Check to see if they implement the lossless flip transformation. If so, read the code for that -- should be fairly easy to understand -- it just reads each block, flips it and moves it.
Lou Franco
well thanks. i will try to look into the code but the code is undocumented and so its not very easy to read. the good news is that source code is available :)
Karim
+1  A: 

Maybe BitMiracle.LibJpeg.Classic.jpeg_decompress_struct.jpeg_read_raw_data() will do what you need.

You may also want to review JpegCodec implementation in LibTiff.Net. That codec uses LibJpeg.Net for various purposes. Reading of uncompressed jpeg data is among of them.

Bobrovsky
i tried to add the code |byte[][][] arr = new byte[16][][];| cinfo.jpeg_read_raw_data(arr, 16); | but it gives me the error "Improper call to JPEG library in state 205"
Karim
i will try to trace into the source code to see why this is happening, also i wanted to ask you if u know where can i find documentation on function in libJpeg.NET for example "jpeg_read_raw_data()" because the documentation in "LibJpeg.NET documentation.chm" is nice in the tutorials but the functions themself are not documented , so i cant know what does jpeg_read_raw_data() do for example or what kind of parameters it excepts , other then that excelent work :)
Karim
unfortunately, documentation for LibJpeg.Net is not complete yet. we've prepared only some tutorials to help those who migrate from original libjpeg library.documentation for functions/methods is expected to be found in documentation for original libjpeg library for now.
Bobrovsky
maybe "Raw (downsampled) image data" chapter in original libjpeg distribution will help you to properly setup decompression structure before reading raw data.
Bobrovsky
thanks, will try that and see how it turns out :)
Karim
i turned out that i needed to get the DCT coefficients for what i needed to do (merge several jpegs in a loseless way) using jpeg_read_coefficients(). but thanks for guiding me to the right way.the problem i am having right now that i try to allocate a block array and i got out of memory when i try to alocate a large number using this code BitMiracle.LibJpeg.Classic.jpeg_common_struct.CreateBlocksArray(32 * 177, 32 * 101); . i dont think this is a lot data. i have 3.5GB of ram on windows XP 32bits. thanks
Karim
Karim, I think it would be better for all to continue this discussion by e-mail. Please write us ([email protected]) and we'll try to help you.
Bobrovsky