0

I am trying to implement the JBIG compression for some images. I just want to know the compression ratio achieved by the algorithm. For this, I am using JBIG-KIT by Markus K.

https://www.cl.cam.ac.uk/~mgk25/jbigkit/ Also, there is a MATLAB implementation available that I am using the code pasted below: Can you please tell me the questions regarding the following MATLAB code? It is code from the wavelet toolbox with paths and commands added for the JBIG-KIT's executables.

function [y,nbr_bits] = perform_jbig_coding(x)
% perform_jbig_coding - perform binary image coding
%  [y,nbr_bits] = perform_jbig_coding(x);
%  It requires pbmtojbg and jbgtopbm executable.
%  Copyright (c) 2006 Gabriel Peyr
name_pbm = 'b.pbm';
name_jbg = 'c.jbg';
if size(x,1)>1 && size(x,2)>1
% forward transform
% save as pbm
imwrite(rescale(x), name_pbm, 'pbm');
% convert to jgib
!/Users/sahilsharma/Documents/MATLAB/JBIG/pbmtojbg -q b.pbm c.jbg
% read jbig file
fid = fopen(name_jbg); %Here%
if fid<0
    error('Unable to open Jbig file.');
end
[y,cnt] = fread(fid, Inf);
fclose(fid);
nbr_bits = length(y)*8;
% remove tmp files
!del c.jbg
!del b.pbm
else
% backward transform
fid = fopen(name_jbg, 'wb');
if fid<0
    error('Unable to open Jbig file.');
end
fwrite(fid, x);
fclose(fid);
% convert to pbm
!/Users/sahilsharma/Documents/MATLAB/JBIG/jbgtopbm c.jbg b.pbm
% read pbm
y = imread(name_pbm);
% remove tmp files
!del c.jbg
!del b.pbm
nbr_bits = -1;
end

I have added the path here to run my code. It is working now. However I have two doubts,

  1. !del command is not working, MATLAB is telling that "Command not found:del". So I thought that "rm" might work here. However, that is also not working, if you have any idea how will I be able to delete those files, please do answer.
  1. [y,cnt] = fread(fid, Inf); (I have commented %Here% in code), am I getting encoded values here? Cause I need to find the compression ratio achieved by JBIG. JBIG uses context-based arithmetic encoding. So I wanted to know if the [y,cnt] reads the encoded data. Through this, I would directly be able to get CR as I know the original size.

'x' is a binary image, currently, I am using an image of size 740x628 (size(x) = [740 628]). cnt = 115392 and y= 14424x1 double. I wanted to have a confirmation about 'y', that if it is the encoded image. If it is then my Compression Ratio becomes (740*628)/115392. The operating system that I am using is macOS.

Oh very sorry 115392 is the value of 'nbr_bits' and 'cnt' = 14424.

Mark Adler
  • 101,978
  • 13
  • 118
  • 158
  • If you expect to get any help here, you need to show your code and exactly what you did with what input, what happened, and what you expected to happen instead. Your vaguely outlined question as it currently stands is not answerable. – Mark Adler Feb 10 '23 at 01:20
  • Yes, actually the previous question I posted was vague cause I myself was not clear on how to put the question. This is the new implementation I am trying for the same problem, please do comment if the problem still seems vague. – Sahil Sharma Feb 10 '23 at 17:14
  • The `del` command has nothing to do with either JBIG or MATLAB. You need to say _what operating system_ you are doing all this under. MATLAB's `!` just sends what follows to your operating system as is, so whatever you would type at your operating system command line should work just the same after the `!` in MATLAB. – Mark Adler Feb 10 '23 at 21:33
  • You are getting closer to an answerable question, but you're not there yet. What `cnt` value are you getting, and what were you expecting? What are the dimensions of the image `x` that you are compressing? – Mark Adler Feb 10 '23 at 21:43
  • 'x' is a binary image, currently, I am using an image of size 740x628 (size(x) = [740 628]). cnt = 115392 and y= 14424x1 double. I wanted to have a confirmation about 'y', that if it is the encoded image. If it is then my Compression Ratio becomes (740*628)/115392. The operating system that I am using is macOS. – Sahil Sharma Feb 10 '23 at 22:16
  • That goes in the question, not in a comment. – Mark Adler Feb 11 '23 at 03:45
  • What is the size of the `b.pbm` file? – Mark Adler Feb 11 '23 at 04:49
  • b.pbm is showing equivalent to 740*628 bits. – Sahil Sharma Feb 11 '23 at 05:45
  • No. When you list your files, what is the size of that file in bytes? – Mark Adler Feb 11 '23 at 05:53
  • Surprisingly it is the same 58kb and in the files list c.jbg has the same size as 14kb roughly equivalent to 115392/8. – Sahil Sharma Feb 11 '23 at 07:19
  • What? You said `cnt` is 115392. That can't be if `c.jbg` is 14 KB. `cnt` would be about 14,000 then. Which is it? – Mark Adler Feb 11 '23 at 08:24
  • 'cnt' is in bits so 115392 bits making around 14424 bytes. Maybe that is why in the file listing the size is shown to be 14kb – Sahil Sharma Feb 11 '23 at 08:36
  • `cnt` is not in bits. Your code gets `cnt` directly from `read()`, which does _not_ return bits. You do not multiply `cnt` by eight anywhere. Did you print `cnt` to actually see its value? – Mark Adler Feb 11 '23 at 18:09
  • Oh very sorry 115392 is the value of 'nbr_bits' and 'cnt' = 14424. – Sahil Sharma Feb 11 '23 at 18:48

1 Answers1

0

Once you get your numbers straight, there doesn't seem to be an issue. You are compressing a 57 KB bi-level image to a 14 KB JBIG compression of that image. Well within the realm of expectation.

Mark Adler
  • 101,978
  • 13
  • 118
  • 158