Click here to Skip to main content
Rate this: bad
good
Please Sign up or sign in to vote.
See more: C++
Hi,
 
I want to create JPEG files which requires to be fixed size, e.g 500KB. But I have no idea how to achieve that. Who can teach me how to make it?? or like use some tools?
 
P.S How to get the smallest size file of JPEG? What is the minimum JPEG files?
Posted 24-Mar-11 23:25pm
skydead312
Edited 27-Mar-11 17:44pm
v3
Comments
DaveAuld at 25-Mar-11 5:27am
   
Why do you need a fixed sized (file size) jpeg file. This is probably impossible to do due to the compression algo's used in jpeg.
Espen Harlinn at 25-Mar-11 5:33am
   
Possibly the most reasonable "Quick answer" possible :)
DaveAuld at 25-Mar-11 5:34am
   
in that case.........
SAKryukov at 25-Mar-11 13:14pm
   
Absolutely agree. I'm sure the requirement makes no sense.
--SA
SAKryukov at 25-Mar-11 13:17pm
   
I added my Answer...
--SA
skydead at 25-Mar-11 5:36am
   
Working need! Sorry,I can't tell you more. Beside JPEG file, I also need to do the same thing with GIF file that I have finished. I used the way is that to create a GIF file lower than required size and use tool to add notes into the file.
DaveAuld at 25-Mar-11 5:39am
   
Well sounds a bit daft to me, rather than optimising to smallest file size possible, you want to fill it with garbage to consume space......daft as i say!
ThatsAlok at 25-Mar-11 7:09am
   
humm... change image dimension and check for image size :-)
Rate this: bad
good
Please Sign up or sign in to vote.

Solution 2

Write the file, check its size and if less than 500K add the requisite number of zero bytes to the end. If greater than 500K you have a problem.
  Permalink  
Comments
Thaddeus Jones at 25-Mar-11 6:52am
   
Oops, it took me so long to write my answer that you posted the short version before I was done :)
Espen Harlinn at 27-Mar-11 10:26am
   
It may be possible to do something by defining custom metatdata for the jpeg image file, as OP specified the jpeg format.
 
I've not conducted any tests to check if this would work for jpeg files, but searching google for "jpeg padding" seems to indicate that this isn't a very good idea.
Richard MacCutchan at 27-Mar-11 12:58pm
   
I agree. However the OP asked how to do it so I offered a solution. If this means his Jpegs won't render correctly that is no concern of mine.
Espen Harlinn at 27-Mar-11 16:03pm
   
All right :)
Rate this: bad
good
Please Sign up or sign in to vote.

Solution 4

Whatever you say, I'm sure that the mistake is the requirement to have 500KB and should be removed.
I already commented that this is impossible; your can pad smaller file but never guarantee smaller file.
 
In general case this is theoretically impossible.
 
Try to prove me wrong.
 
—SA
  Permalink  
Comments
Richard MacCutchan at 25-Mar-11 13:38pm
   
To be fair the OP only suggested 500KB as an example. The requirement seems to be to take an image file and pad it out to some fixed size (which could be 500KB). And we know that this is possible.
SAKryukov at 25-Mar-11 14:16pm
   
I don't think I should be tolerant to the attitude: take any requirement, even stupid, and solve a problem. Even if it was perfect solution, I would vote against it. Padding makes no sense. The requirements should be changes. I know what I'm talking about. This is Dilbertization, real thing that plaques real company. We need programmers who can turn back wrong requirements. I welcome it even I was the one who created wrong requirements. Solving a problem based on a wrong requirement without critical look at it is a form of sabotage!
Sorry, I can't agree with you. This is not even about technical detail.
About technical aspect: how can you guarantee <=500KB right after compression? You can't.
--SA
Richard MacCutchan at 25-Mar-11 15:51pm
   
I agree with you completely. However, since the OP did not explain why he wants these fixed size files I see no reason not to help. Others had already pointed out that it seemed a silly thing to do.
Emilio Garavaglia at 26-Mar-11 3:23am
   
SA, the OP didn't specify why such a requirement. It is not even evident if such a file will be stored on a file system, or, for example, in a space of embedded memory where no "acess by name" is possible and data require to be somehow indexed. May be he just want to prepare a file to be moved up there.
At the point, inflating a short file into a file system looks a nonsense. But the real fact is that we have not enough information about what will be the use of the file and its final destination. So we cannot authoritatively judge if it make sense or not.
SAKryukov at 26-Mar-11 14:32pm
   
Maybe you're right about judgment, strictly speaking. In real life, I have certain confidence that this is no more than sloppiness. As inflating looks nonsense, as you point out yourself, and the guarantee to get the file below 500KB still does not exist (nobody argued it is possible; and this is the main argument against requirement: impossibility), assuming there is a good reason for the requirement is counter-productive. The fact OP didn't specify why such a requirement also contributed to sloppiness assumption.
--SA
SAKryukov at 25-Mar-11 17:39pm
   
Richard, I just pointed out nearly the same.
At this moment we may have a bit different motivation; that's why you don't see a reason not to answer, and I do (but I do see a reason to help, which is different); this is normal and expected,
Thank you for understanding.
--SA
Espen Harlinn at 27-Mar-11 16:05pm
   
Right - a 5!
SAKryukov at 27-Mar-11 16:09pm
   
It's so good that somebody understand it :-)
Thank you, Espen.
--SA
Rate this: bad
good
Please Sign up or sign in to vote.

Solution 1

This is probably impossible to do due to the compression algo's used in jpeg.
  Permalink  
v2
Comments
Espen Harlinn at 25-Mar-11 5:35am
   
5ed! :)
Pravin Patil, Mumbai at 25-Mar-11 5:51am
   
I agree with you..
My 5.
SAKryukov at 25-Mar-11 13:13pm
   
Of course it is impossible in general case.
More importantly, it makes no sense.
--SA
SAKryukov at 25-Mar-11 13:17pm
   
I added my Answer, pleas see.
5 for yours.
--SA
Rate this: bad
good
Please Sign up or sign in to vote.

Solution 3

I don't agree with the statements that it's not possible, you can safely pad an existing JPEG file which is under 500Kb with zeros to increase the length to 500Kb. It works for GIF files too.
 
#include <stdio.h>
#include <stdlib.h>
 
void main(void)
{
	FILE *fp = fopen("C:\\myimage.jpg","ab");
	
	fseek(fp,0,SEEK_END);
	long size = ftell(fp);
 
	char *buffer = (char*)calloc(500*1024-size,1);
	fwrite(buffer,500*1024-size,1,fp);
 
	fclose(fp);
}
 
Note that I consider this quite an ugly hack, but it's up to you to decide if you wish to use this.
  Permalink  
v2
Comments
Richard MacCutchan at 25-Mar-11 7:02am
   
My answer was tongue in cheek, yours was far superior technically.
DaveAuld at 25-Mar-11 9:58am
   
Ok, i also agree, that its not impossible, you've proved that, but i fail to see why the hell you would want to pad out an image file just to fill space....must be a strange requirement to require a fixed size storage size.
Is there a risk that any apps reading this modified image file would fail because the image data length versus file length are different?
Thaddeus Jones at 25-Mar-11 10:15am
   
This is a good question, I think it's certainly a risk. On the bright side, I tried a padded JPEG and GIF file with a few image reading programs (Adobe Photoshop 11.0, MSPaint XP, Paint Shop Pro 5.0, Firefox 3.6.15) and they all worked fine.
SAKryukov at 25-Mar-11 13:13pm
   
It is impossible. Is is only possible if compressed size is less then 500kb, but it is impossible to guarantee.
--SA
Thaddeus Jones at 25-Mar-11 13:21pm
   
I mentioned specifically in my answer that it only works on files which are under 500Kb in size, and claiming something is not impossible is not the same as claiming something is always, under any circumstances, possible.
Personally I feel that offering a solution which works with a restriction, which is mentioned, is not deserving of a 1-vote.
SAKryukov at 25-Mar-11 14:11pm
   
Maybe you're right. But you not offering real useful thing. Real useful (and the only reasonable) would be 1) throwing out requirement, 2) deny to big files in first place.
Padding is crazy and useless idea. Wasting space just out of someone's inability to design software?
This is very fundamental. Who needs problem solvers, especially volunteering?
I don't care it was requirement. Throwing out stupid requirements is the most important skill.
--SA
Thaddeus Jones at 25-Mar-11 14:25pm
   
Commonly used formats such as the NTFS and FAT file systems use padding to fit files in clusters.
Considering he didn't provide a context, I feel it's not our place to decide that padding is such a bad idea it doesn't deserve consideration.
SAKryukov at 25-Mar-11 15:06pm
   
I strongly disagree, sorry. To answer or not to answer is your choice. I think giving "right" answer to "bad" question serves no purpose. I would somehow understand if you've been pressed to do provide some solutions in response to "bad" situation. I think giving any help is positive thing is it is just the idea, not giving any help is (quite naturally) acceptable and is much better than supporting bad requirements. You "not our place" is something your own in your mind which nobody defined. I would understand is you said "not may place" about yourself, isn't that fair?
--SA

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)

  Print Answers RSS
0 CHill60 320
1 Sarvesh Kumar Gupta 238
2 Sergey Alexandrovich Kryukov 173
3 OriginalGriff 163
4 DamithSL 153


Advertise | Privacy | Mobile
Web01 | 2.8.140709.1 | Last Updated 27 Mar 2011
Copyright © CodeProject, 1999-2014
All Rights Reserved. Terms of Service
Layout: fixed | fluid