-
Notifications
You must be signed in to change notification settings - Fork 229
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
RGGB compression somtimes worse than RawSpeedCompress+LZMA2 #151
Comments
Could you try renaming the RGGB file in PPM and compression it in FLIF interlaced ? |
I wrote a simple crusher script for flif (matthiaskrgr/flifcrush), you can try running it on one of the images:
It will take a lot of time but it might get the size down a bit further. edit: BE CAREFUL, it might need a lot of ram |
I found out what is happening. I know how to solve this. I just need to implement it. Soon FLIF will beat that other method. |
Here you go:
|
There are still RGGB that compress better with Delta + ZigZag + LZMA2 than they do with FLIF: https://drive.google.com/file/d/0ByLIAFlgldSoNlR4ZURaTUE2QTg/view?usp=sharing https://drive.google.com/file/d/0ByLIAFlgldSoSkZyQzJvWFNpQTQ/view?usp=sharing PLC helped a lot - on most files FLIF is stronger than without it, and sometimes it also |
As I see, Flif outperform the other method by >1% on average, that is really good. Because each compressor has its own method, sometimes one compressor that usually be worse than others can be more efficient for some case. You can check some plots at issue #144 you will see that FLIF can sometimes outperform JPEG in its own sandbox, and sometimes webp outperform FLIF. |
The Bayer CFA layout is different for different camera models; FLIF currently assumes some specific subpixel layout for its YIQ transform, and if this assumption is wrong, then compression suffers a bit. 14,005,394 panasonic_lumix_dmc_gh3_10.rggb.R.flif 10,481,645 pentax_q10_19.rggb.R.flif Does anyone have a good source of information on these subpixel layouts? I tried reading the |
Dcraw's exif output can show the CFA pattern as a matrix {Color1, Color2}{Color3, Color4}. DNGs and many (all?) RAWs formats has an Exif data for it. Edit: I've found the raw files of panasonic_lumix_dmc_gh3_*.rw2 here http://www.photographyblog.com/reviews/panasonic_lumix_dmc_gh3_review/sample_images/ |
maybe RawSpeed can help? |
I found that Exiftools does not mention the CFA pattern of the RW2, libopenraw documentation say that the pattern should be BGGR and DCRAW say the CFA pattern of the file is GBRG. Which one to trust ? From "ISO TC 42 N 5737" TIFF & DNGs allows patterns matrix bigger than 2x2 and can use Red, Green, Blue, Cyan, Magenta, Yellow or White colors. TIFFtag name is CFAPattern (0x828e), maybe you could check this value before converting it. |
Also: how does DCRAW output the extracted data? GG Another complication is that these camera raw formats do not seem to have the same range for R, G and B. I'm not even sure if G1 and G2 are always in the same range. So that also messes up the YIQ. Probably the way to get best compression (if you're not interested in progressive decoding) is to use |
I'm sorry but DCRAW outputs with the original pattern, the most common case is: I'm working at image-rggb.cpp there is also an issue for maxval < 0xff at blue plane |
Thanks, @psykauze . Now we should probably preprocess raw input to be in RG GB format. |
You should also change the decoder part ;) Edit.: I've made some tests with "only" the planes correction here the results: |
So... Here the new results: |
Maybe one image per camera model is not enough to really draw conclusions, but I wonder what is going on here. The differences in compression when changing the CFA pattern are not huge anyway, which suggests to me that doing the YIQ transform on R G1 B while storing 1+G2 in the 'alpha' channel is not the best approach -- it was just a quick & dirty hack anyway, to match RGGB with the RGBA functionality that was already there. Maybe a custom color transform for RGGB would be better. |
I would also vote for a custom color transformfor RGGB. The Fuji CFA pattern is different from others - see wikipedia: |
That's why I disqualified the fuji x e1. Also, I would like to try the R, (G1+G2)/2, B, (G1-G2) algo. You're right too concerning the R, G and B ranges, there's is correction factor for each color. |
Converting to R, (G1+G2)/2, B, (G1-G2) is not lossless: one bit is lost in the division by two and it cannot be recovered. But we could try to directly apply a modified variant of YIQ, maybe something like this: I'm not sure if encoding G1-G2 is really more efficient than just encoding G1 or G2. The difference is expected to be closer to zero, but it needs an extra bit to be represented. |
There's no lost on R, (G1+G2)/2, B, (G1-G2) conversion if you check if numbers is odd or even: X = (G1+G2)/2 (Always round to lower int like N.5 => N); W = G1-G2; You're right, difference need to be represented with an extra bit but I think this W extra bit can be moved to X in some cases (like if Xdec+Wdec/2 > 0xFFFF then that means Xdec = Xenc << 1 + extra_bit). I need some calculations to check if it is possible. |
What is Y in that case distinction? |
Y was W I've edited the post sorry. |
Ah, right, the parity of the sum is equal to the parity of the difference so there would indeed be enough information to restore the exact sum. I'll try that transformation. Also I'm assuming that raw files have no more than 14 bpc, is that a valid assumption? It helps if we want to represent G1-G2 in an uint16_t... |
I haven't see yet sensors up to 14bits so I think this is a valid assumption. Be carreful, (G1 - G2) is a signed value but maybe you want store the data as '(Diff << 1) + sign' ? |
I cannot use that scripts raw2flif and raw2rggb because I don't have a linux system. |
@StephanBusch Here is a *.bat that do the same as raw2rggb (It's buggy but it works) You need exiftool.exe, dcraw.exe and sed.exe (and its dependencies, see GnuWin website) in the same directory. @jonsneyers Please add this script in the root directory please. Also add this correction. |
@psykauze Thank you for the .bat file. |
I'm so sorry to read that :'( So there is some solutions I consider:
|
Storing metadata is trivial: just put it in a file and include it in the
|
@psykauze I decided to run those tests with your batch because I want to help you developing FLIF by testing. I would also want to have a batch file that produces a .flif. Would it be better to design .flif with metadata as a compressed archive such as LibreOffice/MS Office or Mmiya-Leaf .eip files and store all metadata in a XML inside that archive? I have downloaded raw2rggb.bat and put exiftool.exe, dcraw.exe sed.exe (with libintl3.dll, regex2.dll and libiconv2.dll). Then tried to run raw2rggb.bat canon_eos_5d_mark_iii_05.cr2 canon.rggb but I got an error message: "(" cannot be syntactically processed at this position. |
OK, I've found the issue(s). I'm sorry, I've made some corrections but I haven't tested the bat file :( (Reason, I have not Windows at home). I'm working on it. |
Well, currently FLIF supports metadata as arbitrary files, so it could be compressed or plaintext XML, or whatever. The FLIF decoder simply ignores all metadata at the moment, but perhaps we should standardize some metadata in order to allow It would be nice if we can eventually losslessly convert DNG to FLIF and back without any shell/bat scripts, perhaps using |
@psykauze @jonsneyers @psykauze @jonsneyers |
Sorry @StephanBusch, I've tried to debug the bat script but for some reason I've got random results (same input file, output file size change each try). You could use the previous version of bat file. It was tested and it works. |
@StephanBusch @jonsneyers The bat script is now working. I've got some issues due to Batch scripting limitation. |
thank you for the update and no .rggb was created. The message was: D:\TESTSETS\TEST_CAMERA_flif_rotate> on next image canon_eos_6d_14 the error message is: |
Please use this dcraw instead: Also, could you edit the bat and delete the first "REM" ? (Just the word, not the line) |
thank you.. using the provided DCRAW helped but not on the fujifilm_x_e1_20.raf.. the message here is: D:\TESTSETS\TEST_CAMERA_flif_rotate>IF "GGRGGBGGBGGRBRGRBGGGBGGRGGRGGBRBGBRG" == "RGGB" (SET Rotate=-t 0 ) ELSE IF "GGRGGBGGBGGRBRGRBGGGBGGRGGRGGBRBGBRG" == "GRBG" (SET Rotate=-t 270 ) ELSE IF "GGRGGBGGBGGRBRGRBGGGBGGRGGRGGBRBGBRG" == "BGGR" (SET Rotate=-t 180 ) ELSE IF "GGRGGBGGBGGRBRGRBGGGBGGRGGRGGBRBGBRG" == "GBRG" (SET Rotate=-t 90 ) ELSE ( |
Yeah. This is not an issue. This file can't be performed as a RGGB file in FLIF. The result will be worse than converting it in PGM mode. |
I get 11.098.920 bytes when input is .pgm |
after trying to compress the first canon image, I get an error with FLIF: === flif -v -v -v -v -n c1.rggb c1.flif === Loading input file: c1.rggb RGGB file should be a PGM, like the output of "dcraw -E -4". Cannot read other types. Execution time: 2.985 s |
I'm sorry but the script work as I expected. This file has a "weird" CFAPattern. FLIF can encode and decode it as a RGGB file without issues but if we try to decode it as a pam (or PNG) the result will be weird. |
you mean the fuji file in the above message? |
Yeah. I will check the canon issue maybe it's just because your flif is not from the last commit. |
It isn't, as you can tell from the date. Renaming it to @psykauze: I agree with not pretending that weird CFA patterns are actually RGGB. What do you think is the best approach to handling such files? |
if I use .pnm extension, only the PLC transform will be applied and the canon file is treated as 1 channel 16 bit: compression is also worse than it would be with .rggb extension |
Yes but if the file is not a PGM (P5) then it doesn't contain Bayered data anyway, but RGB pixels. |
I thought that raw2rggb.bat script creates RGGB rather than RGB pixels |
I found out why flif fails to decode the canon rggb file. It's because there is a 'space' caracter after P5 ("P5 \n" instead of "P5\n"). The new bat will not produce this 'space' |
hm, I'm not sure about the PGM spec but if it allows that space to be there, then this is a bug in the rggb/pgm reader... |
The problem is still there - FLIF cannot read the input file. |
@jonsneyers The fuji x e1 CFA pattern contain 8 red pixels, 8 blue pixels and 20 green pixels. We can't debayerize this file in 4 planes with the same dimensions.
@StephanBusch It seems the batch file add an extra non-printable character (0x0d) before the end of line. I think this is a flif issue not really a raw2rggb.bat issue. |
hello , first , a bit about me .. last march (2017) i spent a week throwing together a huffman coder , unhappy with 48bit cinema/ real world image compression options .. not sure what to expect...more as a learning exercise, mixed with frustration at being trapped inside 1980's image formats ... after 2 days i had imagemagick pumping out RGB files , and im compressing (easy)...and more importantly ..5 days later ..decompressing! (..got stoned after the success of compressing ....) ..compression results weren't great .. but reasonable .. so then i tried pre-filtering the image with a dumb filter (no logic like paeth etc). at that point a 300tb image archive test showed that change made files 56% smaller than the openj2k encoder managed (on average / overall etc) ..flif was a bit better of course, at about 59% smaller ..but i didn't know that for another 3weeks ..literally (*). i have j2k files that come out smaller than flif btw, just a few. my system never beats flif , but stays quite close. annoyingly , the dumb filter / first attempt/ placeholder filter seems to be the best ive come up with .. which should be good , but it means since ive picked up the code after a year long break (got stoned again) ..ive now spend several weeks wasting my time testing alternatives without results .. but i do have 7 different reversible color transforms now , and they are ALL useful / get used (hint) during my initial experiments i also made a modified version to handle bayer patterns . i downloaded the blackmagic cinema camera sample files (all 11gig) , .. DCRAW can output a text file with meta data from the file read , i parse that , and get the RGGB order .. but ive only implemented the one BMCC uses out of laziness . as for raw ..my advice for handing odd bayer patterns is to just decode them to full RGB . DCRAW can output 16bit linear bayer patterns , but odd patterns i just do a full decode , then throw away 2/3 of the values in any bayer configuration i want (or more precisely .. the same one BMCC uses..as ive implemented it already!)..reverse engineering the raw.. HOWEVER, what i intend to do in the future is count the number of original values in the red green and blue channels , and pick the bayer battern that maximizes the number of original values , OR has the widest dynamic range , with the latter trumping the former. as im compressing sequences of large images , ill do this analysis once / or a few times per sequence of images (same when choosing the correct RCT),.. then assume that pattern for all images in the sequence (or test images from the start/ middle / end of sequences and try using any filter already chosen as optimal by one of those etc) , but its pretty quick analysis. you can't cater of every proprietory pattern , unless perhaps you store metadata that can describe colors other than pure Red, Green, or Blue ( ie, by specify each bayer color component as a seperate set of R/G/B values in the metadata, yellow and purple filters can be used and colour adjusted .. but because im lazy , and manufacturers making sensors with purple and yellow filters can just f**k off / are perverts ..). the reality is , the physical colour filters used on the sensor will not be pure red green or blue anyway , and if you throw away the same amount of information that was never captured by the sensor taking the image in the first place , especially after its been fully decoded .. my tests show that reconstruction using simple bi-linear interolation never produces artifacts ive been focusing on debayering recent;y..wondering if adaptive debayering techniques can cause jitter in moving images. no artifacts appears in decoded redcode footage when simple bi-linear interpolation is used to debayer a "pseudo-reverse engineered raw file",..even after the image is downsampled to quarter resolution .. so im starting to think thats how red debayer too (no doubt because the sensor has a strong OLPF dropping optical resolution/ edge contrast making demosaicing trivial) . as for compressing DNG files (or as i call them .. dung files ) ., i reencoded the blackmagic sample files , and compressed them using the correct bayer pattern , think i knocked off between 10-20%. i used the bayer reversible colour transform described on the microsoft research site as the RCT option , and i prefiltered the raw files before compressing by simply sweeping right to left across each image row , and subtracting the value 2 places to the left (filtering right to left during encoding, filtering left to right while decoding etc ) .. but then i also filter the filtered results vertically (from bottom to top, again, subtracting the value 2 rows above from the current value) . the results of pre comrpession filtering are signed values btw, bit depth arbitrary not sure if any of that is useful , but feel free to reach out to discuss my ramblings etc/ so ..i do have a couple of questions myself .. which is why i came on here :
thanks in advance k.r |
ps, final thought, many of the RCT's ive used in my compression system were designed by other people, and based around their understanding of how the human eye/ brain works etc .. however , lossless compression has nothing to do with how the eye works, or the brain recieves colour .. its just about reducing bytes ..but a lot of research papers i read start with lossy transforms yuv etc .. then elaborate them into lossless versions .. why!! i think zig zag patterns largely fall into that category . i believe the zig zag pattern is more useful in a lossy context, and is used for perceptual reasons , less useful in a lossless context |
In my ongoing tests with RGGB created by dcraw compression of FLIF is sometimes worse
than RawSpeedCompress+LZMA2. RawSpeedCompress uses different Delta versions for uncompressed pixel data and applies ZigZag-encoding which helps all kinds of general purpose compressors.
https://drive.google.com/file/d/0ByLIAFlgldSoNFR6RmcwdTgxQTQ/view?usp=sharing
leica_m82_05.rggb 11.359.612 (FLIF 0.1)
leica_m82_05.rggb 10.928.429 (FLIF 0.1.3_slower_but_stronger -n)
leica_m82_05.rggb 5.560.365 (RawSpeedCompress3 + LZMA2)
https://drive.google.com/file/d/0ByLIAFlgldSoalhkSkdsNmJNaDA/view?usp=sharing
nikon_1_v2_17.rggb 9.931.720 (FLIF 0.1)
nikon_1_v2_17.rggb 9.636.696 (FLIF 0.1.3_slower_but_stronger -n)
nikon_1_v2_17.rggb 8.810.004 (RawSpeedCompress3 + LZMA2)
https://drive.google.com/file/d/0ByLIAFlgldSoZmJDUXl2YWVPS2M/view?usp=sharing
nikon_d5200_14.rggb 24.334.053 (FLIF 0.1)
nikon_d5200_14.rggb 23.684.359 (FLIF 0.1.3_slower_but_stronger -n)
nikon_d5200_14.rggb 21.527.675 (RawSpeedCompress3 + LZMA2)
Can you please check why FLIF is worse here?
Will future versions also get something like Delta+ZigZag+LZ?
In almost every case the -n switch provided better compression on RGGB
The text was updated successfully, but these errors were encountered: