|
What's the easiest way to read the width and height of a png file using C++ it’s the ONLY image type I'm going to read so massive image libraries are a waste of space in my program. Any ideas?
Cheers
RW
|
|
|
|
|
|
I'm having trouble getting understanding the header information. It doesnt seem very clear to me however regardless of that, how can i change my four bits stored in 4 indexes of an array into a recognisable number? I'm not much good with bit shifting
|
|
|
|
|
Do you have a code snippet of what you are trying to do?
|
|
|
|
|
Sure.
The basic idea here is that its going to create a button for a menu in directx. Its gotta find the size of the button image before it can create the panel to texture the png file onto [hence the panel and graphic are the same size] All I need for this is the width and height. DirectX can do the rest.
<br />
std::ifstream data_file;<br />
data_file.open("filename.png", std::ios::in | std::ios::binary);<br />
<br />
char signature[8];<br />
<br />
char width[4];<br />
char height[4];<br />
<br />
data_file.read(signature,8);
<br />
<br />
data_file.read(width,4);<br />
data_file.read(height,4);<br />
<br />
int texWidth = (int) ((width[0] << 32)+(width[1] << 16)+(width[2] << 8)+width[3]);<br />
data_file.close();
|
|
|
|
|
OK heres a better one since I just solved one problem and got another...
<br />
std::ifstream data_file;<br />
data_file.open("filename.png", std::ios::in | std::ios::binary);<br />
<br />
char signature[16];<br />
char width[4];<br />
char height[4];<br />
<br />
data_file.read(signature,16);<br />
<br />
data_file.read(width,4);<br />
data_file.read(height,4);<br />
<br />
int texWidth = 0;<br />
for(int i=0; i<4; ++i){<br />
texWidth <<= 8;<br />
texWidth += width[i];<br />
}<br />
<br />
<br />
data_file.close();<br />
Now it says the width of my image is -56!?
|
|
|
|
|
Real World wrote:
texWidth <<= 8;
texWidth += width[i];
Did you mean to use both of these or just one of them?
|
|
|
|
|
This should work:
// here's the bit-shifting stuff!
#define SH(p) ((unsigned short)(unsigned char)((p)[1]) | ((unsigned short)(unsigned char)((p)[0]) << 8))
#define LG(p) ((unsigned long)(SH((p)+2)) | ((unsigned long)(SH(p)) << 16))
void main( int argc, char *argv[] )
{
FILE *pFile;
unsigned char uBuffer[1024];
long lWidth,
lHeight,
lSize;
pFile = fopen(argv[1], "rb");
if (NULL != pFile)
{
fread(uBuffer, sizeof(unsigned char), 8, pFile);
fread(uBuffer, sizeof(unsigned char), 4, pFile);
// convert this to a long
lSize = (uBuffer[0] & 0xff);
lSize <<= 8;
lSize |= (uBuffer[1] & 0xff);
lSize <<= 8;
lSize |= (uBuffer[2] & 0xff);
lSize <<= 8;
lSize |= (uBuffer[3] & 0xff);
fread(uBuffer, sizeof(unsigned char), 4, pFile);
fread(uBuffer, sizeof(unsigned char), lSize, pFile);
lWidth = LG(uBuffer);
lHeight = LG(uBuffer + 4);
fclose(pFile);
}
}
|
|
|
|
|
Hi,
I'm having a frustrating little problem with 64-bit integer arithmetic. It goes something like this:
0xba5da800 * 0x38E
which should equal:
0x29678EB3000
Unfortunately, the code is a bit like this:
#define unsigned __int64 uint64_t<br />
<br />
unsigned long myval = 0xba5e3500;<br />
<br />
uint64_t myfunction(unsigned long val)<br />
{<br />
return val * 1000;<br />
}
This doesn't work, nor does:
return ((uint64_t)val * (uint64_t)1000);
or
return (uint64_t)((uint64_t)val * (uint64_t)1000);
The value I get returned looks like:
0xfffffeefffff0800 (or similar)
Now, I've run into problems before with not casting everything into __int64 twice (ok I exadurate slightly) for every calculation - but this is weird. It seems that casting val to uint64_t is turning it into:
0xffffffffba5e3500
Why is this happening?!
I truly do hate __int64, any help would be appreciated.
thanks,
Simon
--
Simon Steele
Programmers Notepad - http://www.pnotepad.org/
|
|
|
|
|
I get the desired results with:
typedef unsigned __int64 uint64_t;
unsigned long myval = 0xba5da800;
uint64_t myfunction(unsigned long val)
{
return (uint64_t) val * (uint64_t) 0x38e;
}
void myotherfunction()
{
TRACE("%I64u\n", myfunction(myval));
}
|
|
|
|
|
Check your tests?
The second two should work fine. The first one fails because the values are multiplied as longs and then returned as a uint64. The multiplication as a long wraps. Casting one value to a uint64 will make the multiplication occur on uint64's and should give correct results.
Your message mentions 0xba5da800 * 0x38E = 0x29678EB3000 at the top and your tests use 0xba5e3500 * 1000...
The code below seeems to give the results you expect.
typedef unsigned __int64 uint64_t;<br />
<br />
unsigned long myVal = 0xba5da800 ;<br />
<br />
uint64_t myfunction1(unsigned long val)<br />
{<br />
return val * 0x38E;<br />
}<br />
<br />
uint64_t myfunction2(unsigned long val)<br />
{<br />
return ((uint64_t)val * (uint64_t)0x38E);<br />
}<br />
<br />
uint64_t myfunction3(unsigned long val)<br />
{<br />
return (uint64_t)((uint64_t)val * (uint64_t)0x38E);<br />
}<br />
<br />
int main(int argc, char* argv[])<br />
{<br />
uint64_t result1 = myfunction1(myVal);<br />
uint64_t result2 = myfunction2(myVal);<br />
uint64_t result3 = myfunction3(myVal);<br />
<br />
printf("myfunction1 - 0x%I64x\n", result1);<br />
printf("myfunction2 - 0x%I64x\n", result2);<br />
printf("myfunction3 - 0x%I64x\n", result3);<br />
<br />
return 0;<br />
}
Len Holgate
www.jetbyte.com
The right code, right now.
|
|
|
|
|
Hi,
thanks for saying that it works for you, it led me to the problem. The value being passed in was actually a long not an unsigned long, I didn't notice because it's actually part of a structure the definition of which has changed.
"it does work, you fool!" posts are just as useful as solution advice so thanks to those who replied. I'll write a test harness next time before posting, my bad
thanks again,
simon.
--
Simon Steele
Programmers Notepad - http://www.pnotepad.org/
|
|
|
|
|
Hi everybody.I want programmaticly to change or set the 'Author' property for the newly created file.Any help will be appreciated.
|
|
|
|
|
Re: Changing file properties DavidCrow 15:30 7 May '03
Use the CFile::SetStatus() method, or use SetFileAttributes().
here you go, btw search forum before posting
Greetings
Jens
|
|
|
|
|
Unfortunately, those do not work with compound properties.
|
|
|
|
|
CFileStatus has not field Author.I think it is not suit for this case.But thank you anyway.
|
|
|
|
|
While it's not exactly what you are after, see if MSDN article Q186898 is of any help.
|
|
|
|
|
This example explaines about compound file whereas i need for text file.
|
|
|
|
|
It'll work with "regular" files, too. You just have to use StgOpenStorageEx() instead of StgOpenStorage().
|
|
|
|
|
Hi
I want to center the title text of my application (mfc) in the title bar.
Now it is positioned left.
Anyone can help?
Greetings
Jens
|
|
|
|
|
Good question. I suspect it's somewhat difficult, if it's even possible. You might end up creating a caption-less dialog (i.e., a dialog without the WS_CAPTION style), and then drawing your own caption in the client area of the dialog.
|
|
|
|
|
Good question....
Thank you for the appreciation
I'll see if i can fix the problem. If i have working code, i'll send it to you or post it somewhere on forum here.
Best regards
Jens
|
|
|
|
|
i hav some idea..
title bar length = widhth of ur dialog/window
u alos get the indent/width of the text
then find the half of width
find the haif of string
place the string from halfof titlebar - half of text
you can add the spaces in fornt of the string to place the text in the appropriate position.
hope u got it..
CodeTheDreams();
|
|
|
|
|
This is the way I would do it as well.
modified 12-Sep-18 21:01pm.
|
|
|
|
|
I have not done it... never...
but...
1.filling the beggining of the string with spaces is not good if you change the font... because spaces size and font size changes also.
2.Deleting the caption and placing a label centered... is also a not standard thing that will make your app. to seem strange...
3.I'm sure that I've seen here in CP some pieces of code that show how to paint over the Caption bar (the typical thing of adding additional buttons and so on...).
I think that the best approach would be to leave the caption text blank and painting the desired text calculating the width of the text that will be displayed in pixels and...
Take a look at the articles that show how to draw in the caption bar...
then you can search for how to find the width of a outputted text via GDI calls.
then it's only a matter of time, dividing in order to get the coordinate where to start painting and so on...
Well, I have not done this and this is only theory, but it sounds good to me...
PS:
my english is not my best...
Hope this helps...
|
|
|
|