Problem using a compression library

This is a discussion on Problem using a compression library within the C++ Programming forums, part of the General Programming Boards category; I load a bitmap and compress it bitmap data using LZ-77 algorithm. I want to store the current address of ...

  1. #1
    abu
    abu is offline
    Registered User
    Join Date
    Mar 2003
    Posts
    11

    Problem using a compression library

    I load a bitmap and compress it bitmap data using LZ-77 algorithm.


    I want to store the current address of pointer of file stream f1,f2 in unsigned char *in,out;
    I tried ftell() , fseek() functions but it return an long and cannot convert to unsigned char* in.
    Help me!!!
    Last edited by abu; 07-18-2004 at 09:04 AM.
    The Beginner

  2. #2
    Registered User
    Join Date
    Oct 2001
    Posts
    2,934
    > int outsize=LZ_Compress(in,out,size);

    You haven't allocated any memory for in and out at this point. Plus out is declared as an unsigned char.

  3. #3
    abu
    abu is offline
    Registered User
    Join Date
    Mar 2003
    Posts
    11
    So my question is how to get the 2 pointers
    unsigned char* in,out
    to point to the current address of the pointer of the FILE* f1,f2.
    Last edited by abu; 07-17-2004 at 09:57 PM.
    The Beginner

  4. #4
    ATH0 quzah's Avatar
    Join Date
    Oct 2001
    Posts
    14,826
    You don't. It doesn't work that way. Your functions don't work with file streams. How about reading up on your functions to see how they're supposed to be used? (Since it's obvious that you didn't write either of the two functions in bold, because if you had, you'd know how they work, so you wouldn't be asking how to use them.)

    In other words, RTFM.

    Quzah.
    Hope is the first step on the road to disappointment.

  5. #5
    abu
    abu is offline
    Registered User
    Join Date
    Mar 2003
    Posts
    11
    Sorry if my question is not clear.I tried to use the library "lz.c" in my program.But it still doesn't work correctly.Anyone can help me??

    Code:
    #include "bitmap.h"
    #include<stdio.h>
    #include<iostream.h>
    
    #include "lz.c"
    
    
    int LZ_Compress( unsigned char *in, unsigned char *out,
                     unsigned int insize );
    void LZ_Uncompress( unsigned char *in, unsigned char *out,
                        unsigned int insize );
    
    int main()
    {
    
    	unsigned char *out;
    	//load bitmap file
    	Fileheader bmfh;
    	Infoheader bmih;	
    	RGBQUAD Palette[256];
    	unsigned char* Data;
    	FILE *f1;
    	FILE *f2;
    	f1=fopen("test.bmp","rb");
    	f2=fopen("LZ.77","wb");
    		
    		fread(&bmfh,sizeof(Fileheader),1,f1);
    		fread(&bmih,sizeof(Infoheader),1,f1);
    	
    	for(int i=0;i<256;i++)
    	{
    		fread(&Palette[i],sizeof(RGBQUAD),1,f1);
    	}
    	
    	//read the actual bitmap data 
    	fseek(f1,bmfh.OffBits,SEEK_SET);
    	long size=(((bmih.Width * (bmih.Bitperpixel / 8)) + 3) & (~3))*bmih.Height;
    	
    	Data=new unsigned char[size];
    	fread((unsigned char*)Data,size,1,f1);
    			
    	fwrite(&bmfh,sizeof(Fileheader),1,f2);
    	fwrite(&bmih,sizeof(Infoheader),1,f2);
    	for(i=0;i<256;i++)
    	{
    		fwrite(&Palette[i],sizeof(RGBQUAD),1,f2);
    	}
    
    	out=new unsigned char[size];
                                //pointer to output buffer
                    // begin LZ77 compression
    	int insize=4096;
    	int outsize=LZ_Compress(Data,out,insize);
    	fwrite(&out,outsize,1,f2);
    	fclose(f1);
    	fclose(f2);
    }
    Here is this guide to used the libray( file included "lz.c")

    Syntax: outsize = LZ_Compress(in,out,insize)
    outsize - Size of output buffer after compression
    in - Pointer to the input buffer (uncompressed data)
    out - Pointer to the output buffer (compressed data)
    insize - Size of input buffer

    The output buffer must be able to hold (insize*257/256 + 1) bytes.
    Attached Files Attached Files
    Last edited by abu; 07-18-2004 at 09:17 AM.
    The Beginner

  6. #6
    and the hat of wrongness Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    32,544
    Moved to C++ forum

    > int insize=4096;
    How does this relate to (insize*257/256 + 1) bytes.

    > out=new unsigned char[size];
    Besides, you only allocate 'size' bytes, not 4096 (unless you're that lucky!)
    So lying to a function about how much memory it has is a bad idea

    > fwrite(&out,outsize,1,f2);
    Should be
    fwrite(out,outsize,1,f2);

    out is already a pointer, you don't need another &
    If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
    If at first you don't succeed, try writing your phone number on the exam paper.
    I support http://www.ukip.org/ as the first necessary step to a free Europe.

  7. #7
    abu
    abu is offline
    Registered User
    Join Date
    Mar 2003
    Posts
    11
    > int insize=4096;
    How does this relate to (insize*257/256 + 1) bytes.
    insize is size of input buffer so I think it's a user defined const.

    > out=new unsigned char[size];
    Besides, you only allocate 'size' bytes, not 4096 (unless you're that lucky!)
    So lying to a function about how much memory it has is a bad idea
    size variable is the size of input file.
    Because this is a compress program so I allocate 'size' bytes for output buffer so it's remaining and I only fwrite "outsize" byte.
    > fwrite(&out,outsize,1,f2);
    Should be
    fwrite(out,outsize,1,f2);

    out is already a pointer, you don't need another &
    [/quote]
    Thanks , I'm trying to decode the output file...hope I encoded the bitmap file correct.
    The Beginner

  8. #8
    abu
    abu is offline
    Registered User
    Join Date
    Mar 2003
    Posts
    11
    > int insize=4096;
    How does this relate to (insize*257/256 + 1) bytes.
    insize is size of input buffer so I think it's a user defined const.

    > out=new unsigned char[size];
    Besides, you only allocate 'size' bytes, not 4096 (unless you're that lucky!)
    So lying to a function about how much memory it has is a bad idea
    size variable is the size of input file.
    Because this is a compress program so I allocate 'size' bytes for output buffer so it's remaining and I only fwrite "outsize" byte.
    > fwrite(&out,outsize,1,f2);
    Should be
    fwrite(out,outsize,1,f2);

    out is already a pointer, you don't need another &
    [/quote]
    Thanks , I'm trying to decode the output file...hope I encoded the bitmap file correct.
    The Beginner

  9. #9
    and the hat of wrongness Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    32,544
    > long size=(((bmih.Width * (bmih.Bitperpixel / 8)) + 3) & (~3))*bmih.Height;
    Doesn't look const to me - it varies with every single file you try and read in.

    Take a snapshot of the screen say (1024*768*32) and your code is in big trouble.

    > Because this is a compress program so I allocate 'size' bytes for output buffer
    Look at the calculation - the output buffer has to be bigger than the input buffer - temporarily at least.
    If the file turns out to be not compressable at all, then the file will expand by a very small amount - namely by 1 extra byte for every 256 bytes of input.
    If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
    If at first you don't succeed, try writing your phone number on the exam paper.
    I support http://www.ukip.org/ as the first necessary step to a free Europe.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. undefined Reference when linking a library
    By steve1_rm in forum C Programming
    Replies: 7
    Last Post: 03-12-2008, 05:34 PM
  2. Templates from DLL or static library problem
    By mikahell in forum C++ Programming
    Replies: 2
    Last Post: 01-01-2008, 12:49 AM
  3. Memory problem with Borland C 3.1
    By AZ1699 in forum C Programming
    Replies: 16
    Last Post: 11-16-2007, 10:22 AM
  4. Bin packing problem....
    By 81N4RY_DR460N in forum C++ Programming
    Replies: 0
    Last Post: 08-01-2005, 05:20 AM
  5. Problem With WinPcap library
    By DrMario in forum C Programming
    Replies: 0
    Last Post: 03-26-2005, 10:26 AM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21