Coding: Fleeting Thoughts

A place to discuss the implementation and style of computer programs.

Moderators: phlip, Moderators General, Prelates

User avatar
You, sir, name?
Posts: 6983
Joined: Sun Apr 22, 2007 10:07 am UTC
Location: Chako Paul City
Contact:

Re: Coding: Fleeting Thoughts

Postby You, sir, name? » Mon Jul 05, 2010 7:34 pm UTC

TheChewanater wrote:*Cringe*

Why not just use std::vectors? No astrisks necessary.


I'm not entirely sure how a vector would replace a pointer in any way that isn't horribly awkward.
I edit my posts a lot and sometimes the words wrong order words appear in sentences get messed up.

User avatar
TheChewanater
Posts: 1279
Joined: Sat Aug 08, 2009 5:24 am UTC
Location: lol why am I still wearing a Santa suit?

Re: Coding: Fleeting Thoughts

Postby TheChewanater » Mon Jul 05, 2010 9:02 pm UTC

It would replace an array, like a char* (technically a pointer). Or use a smart pointer.
ImageImage
http://internetometer.com/give/4279
No one can agree how to count how many types of people there are. You could ask two people and get 10 different answers.

User avatar
You, sir, name?
Posts: 6983
Joined: Sun Apr 22, 2007 10:07 am UTC
Location: Chako Paul City
Contact:

Re: Coding: Fleeting Thoughts

Postby You, sir, name? » Mon Jul 05, 2010 9:18 pm UTC

What's wrong with regular pointers all of the sudden?
I edit my posts a lot and sometimes the words wrong order words appear in sentences get messed up.

User avatar
Sc4Freak
Posts: 673
Joined: Thu Jul 12, 2007 4:50 am UTC
Location: Redmond, Washington

Re: Coding: Fleeting Thoughts

Postby Sc4Freak » Mon Jul 05, 2010 11:00 pm UTC

Because there are few reasons to use raw pointers for managing objects or resources. Raw pointers don't have well-defined ownership or lifetime for their pointed-to objects. If you need a dynamically-allocated array, use std::vector (or even boost::shared_array, depending on what your specific needs are). If you just want a dynamically-allocated object, use boost::shared_ptr.

Pointers are useful for, say, iterating through a data buffer. It's often cleaner to use pointer arithmetic than it is to use array subscripts (and as a plus, you can use pointers as a type of iterator in standard C++ algorithms too). But that's about all pointers are useful for. Certainly, they should not be used to manage long-lived objects, and you should find that calling delete is very rare.

Axidos
Posts: 167
Joined: Tue Jan 20, 2009 12:02 pm UTC
Location: trapped in a profile factory please send help

Re: Coding: Fleeting Thoughts

Postby Axidos » Tue Jul 06, 2010 1:37 am UTC

Isn't Vector a little problematic as an ADT in C++? At least for me, whilst I was writing a uni project in Visual C++ 2008, attempting to iterate over an empty vector would cause a runtime error. I would be able to guard against this by making sure the vector wasn't empty first, except checking the size of an empty vector would also cause a runtime error, and in fact it's this same size-check that would crash the iterator.

User avatar
phlip
Restorer of Worlds
Posts: 7573
Joined: Sat Sep 23, 2006 3:56 am UTC
Location: Australia
Contact:

Re: Coding: Fleeting Thoughts

Postby phlip » Tue Jul 06, 2010 1:46 am UTC

Well, you were almost certainly doing something wrong, because I've never had that happen. There's no reason why vect.size() should ever cause an error. Maybe pVect->size() if pVect hasn't been initialised yet?

This should just print "0":

Code: Select all

#include <iostream>
#include <vector>
int main()
{
  std::vector<int> vect;
  // leave vect empty
  std::cout << vect.size() << std::endl; // should be 0
  for (std::vector<int>::iterator i = vect.begin(); i != vect.end(); ++i)
    std::cout << *i << std::endl; // it shouldn't get in here at all
  return 0;
}

Code: Select all

enum ಠ_ಠ {°□°╰=1, °Д°╰, ಠ益ಠ╰};
void ┻━┻︵​╰(ಠ_ಠ ⚠) {exit((int)⚠);}
[he/him/his]

Axidos
Posts: 167
Joined: Tue Jan 20, 2009 12:02 pm UTC
Location: trapped in a profile factory please send help

Re: Coding: Fleeting Thoughts

Postby Axidos » Tue Jul 06, 2010 6:16 am UTC

Size or empty will crash the program:

Code: Select all

vector<int>* vect;
vect->size();
vect->empty();


Now it won't:

Code: Select all

vector<int> vect;
vect.size();
vect.empty();


Edit: Oh, now I see why. Pointers don't initialise themselves. Well, I'm a twit.
Last edited by Axidos on Tue Jul 06, 2010 6:22 am UTC, edited 1 time in total.

User avatar
Sc4Freak
Posts: 673
Joined: Thu Jul 12, 2007 4:50 am UTC
Location: Redmond, Washington

Re: Coding: Fleeting Thoughts

Postby Sc4Freak » Tue Jul 06, 2010 6:21 am UTC

That's not a problem with vector. That's a problem because you didn't initialize your pointer. It doesn't matter what the pointed-to-type is, the following are all invalid:

Code: Select all

int* a;
*a;

vector<int>* b;
b->size();

string* c;
c->length();

Axidos
Posts: 167
Joined: Tue Jan 20, 2009 12:02 pm UTC
Location: trapped in a profile factory please send help

Re: Coding: Fleeting Thoughts

Postby Axidos » Tue Jul 06, 2010 6:24 am UTC

Oh hey a reply. Yeah, I edited my post. After some searching and winding I found this. The project I ranted about was during a half-semester I'm not proud of, clearly I didn't do my research.

User avatar
TheChewanater
Posts: 1279
Joined: Sat Aug 08, 2009 5:24 am UTC
Location: lol why am I still wearing a Santa suit?

Re: Coding: Fleeting Thoughts

Postby TheChewanater » Wed Jul 07, 2010 3:24 am UTC

FT: Is this somehow wrong? It works fine and lets me use html5 shortcuts.

Code: Select all

function html5_to_html ($html5)
{
  $html5tags = array ("<header", "<nav", ... "</header>", "</nav>", ...);
  $htmltags = array ("<div class = header", "<div class = nav", ... "</div>", "</div>", ...);
    
  return str_replace 
($html5tags, $htmltags, $html5);
}
 

Code: Select all

header, .header
{
  ...
}
nav, .nav
{
  ...
}

Code: Select all

if (no_html5 ())
{
  ...
  echo html5_to_html ($body);
}
else
{
  ...
  echo $body;
}
 
ImageImage
http://internetometer.com/give/4279
No one can agree how to count how many types of people there are. You could ask two people and get 10 different answers.

User avatar
Xanthir
My HERO!!!
Posts: 5426
Joined: Tue Feb 20, 2007 12:49 am UTC
Location: The Googleplex
Contact:

Re: Coding: Fleeting Thoughts

Postby Xanthir » Wed Jul 07, 2010 3:46 am UTC

That particular string-replace won't let you put any additional classes on your elements.

Just use the HTML5 elements normally and use the shim to make IE treat them right.
(defun fibs (n &optional (a 1) (b 1)) (take n (unfold '+ a b)))

Axidos
Posts: 167
Joined: Tue Jan 20, 2009 12:02 pm UTC
Location: trapped in a profile factory please send help

Re: Coding: Fleeting Thoughts

Postby Axidos » Wed Jul 07, 2010 7:51 am UTC

There is one thing wrong if you're trying to comply with XHTML standards - <div class = header should be <div class="header"

User avatar
Emu*
Posts: 689
Joined: Mon Apr 28, 2008 9:47 am UTC
Location: Cardiff, UK
Contact:

Re: Coding: Fleeting Thoughts

Postby Emu* » Wed Jul 07, 2010 8:31 am UTC

HTML5 was written to degrade elegantly, so some display:block in the CSS, a little bit of JS, no more HTML4!
Cosmologicon wrote:Emu* implemented a naive east-first strategy and ran it for an hour, producing results that rivaled many sophisticated strategies, visiting 614 cells. For this, Emu* is awarded Best Deterministic Algorithm!

User avatar
You, sir, name?
Posts: 6983
Joined: Sun Apr 22, 2007 10:07 am UTC
Location: Chako Paul City
Contact:

Re: Coding: Fleeting Thoughts

Postby You, sir, name? » Wed Jul 07, 2010 10:50 am UTC

TheChewanater wrote:FT: Is this somehow wrong? It works fine and lets me use html5 shortcuts.

Spoiler:

Code: Select all

function html5_to_html ($html5)
{
  $html5tags = array ("<header", "<nav", ... "</header>", "</nav>", ...);
  $htmltags = array ("<div class = header", "<div class = nav", ... "</div>", "</div>", ...);
    
  return str_replace 
($html5tags, $htmltags, $html5);
}
 

Code: Select all

header, .header
{
  ...
}
nav, .nav
{
  ...
}

Code: Select all

if (no_html5 ())
{
  ...
  echo html5_to_html ($body);
}
else
{
  ...
  echo $body;
}
 



... or you could use XSLT. Though it's a real nazi about proper XML syntax.
I edit my posts a lot and sometimes the words wrong order words appear in sentences get messed up.

User avatar
Rippy
Posts: 2101
Joined: Sun Jul 22, 2007 11:27 pm UTC
Location: Ontario, Can o' Duh

Re: Coding: Fleeting Thoughts

Postby Rippy » Wed Jul 07, 2010 1:10 pm UTC

Is there a standard way in Haskell to throw away a function's return value? I would imagine putting it in a "do" block with "return ()" after it would work, but that's hideous. I also imagine, based on my limited Haskell knowledge, that you could just write it yourself:

Code: Select all

stripFunc :: a -> ()
stripFunc _ = ()


Just wondering if that already exists. I'm working with HDBC and SQLite and it can be annoying when your function just has to write to a table, i.e. IO (), but it ends up as type Int because the function it calls returns a row number.

User avatar
TheChewanater
Posts: 1279
Joined: Sat Aug 08, 2009 5:24 am UTC
Location: lol why am I still wearing a Santa suit?

Re: Coding: Fleeting Thoughts

Postby TheChewanater » Wed Jul 07, 2010 1:30 pm UTC

I ended up getting rid of the str_replace mess and Google gave me this.
ImageImage
http://internetometer.com/give/4279
No one can agree how to count how many types of people there are. You could ask two people and get 10 different answers.

User avatar
Berengal
Superabacus Mystic of the First Rank
Posts: 2707
Joined: Thu May 24, 2007 5:51 am UTC
Location: Bergen, Norway
Contact:

Re: Coding: Fleeting Thoughts

Postby Berengal » Wed Jul 07, 2010 3:57 pm UTC

Rippy wrote:Is there a standard way in Haskell to throw away a function's return value? I would imagine putting it in a "do" block with "return ()" after it would work, but that's hideous. I also imagine, based on my limited Haskell knowledge, that you could just write it yourself:

Code: Select all

stripFunc :: a -> ()
stripFunc _ = ()


Just wondering if that already exists. I'm working with HDBC and SQLite and it can be annoying when your function just has to write to a table, i.e. IO (), but it ends up as type Int because the function it calls returns a row number.

Making such a function is useless, since a function's only as good as it's return value, and () is an entirely useless value (it has absolutely no information).

Unless you're talking about actions (such as IO (), which are not functions because functions are pure and take input but actions don't). If that's the case, import Control.Applicative and use '(<$)'. For example:

Code: Select all

pause :: IO ()
pause = do
  putStrLn "Press enter to continue"
  () <$ getLine


Anyway, 'return ()' is a pretty common idiom when you really do need to return ().
It is practically impossible to teach good programming to students who are motivated by money: As potential programmers they are mentally mutilated beyond hope of regeneration.

qbg
Posts: 586
Joined: Tue Dec 18, 2007 3:37 pm UTC

Re: Coding: Fleeting Thoughts

Postby qbg » Wed Jul 07, 2010 5:12 pm UTC

How do you test functional code? Functions near the leaves of the computation tend to be easy to test, but how do you test functions near the root of the computation? Their inputs tend to be complicated and their output tends to be even more complicated.

Likewise, how do you test a generic maze generation program? It seems to me that you would have to write much more code (and tests for that code) than the program itself to actually be able to test the program.

User avatar
Rippy
Posts: 2101
Joined: Sun Jul 22, 2007 11:27 pm UTC
Location: Ontario, Can o' Duh

Re: Coding: Fleeting Thoughts

Postby Rippy » Wed Jul 07, 2010 6:28 pm UTC

Berengal wrote:Making such a function is useless, since a function's only as good as it's return value, and () is an entirely useless value (it has absolutely no information).

Unless you're talking about actions (such as IO (), which are not functions because functions are pure and take input but actions don't). If that's the case, import Control.Applicative and use '(<$)'. For example:

Code: Select all

pause :: IO ()
pause = do
  putStrLn "Press enter to continue"
  () <$ getLine


Anyway, 'return ()' is a pretty common idiom when you really do need to return ().

Ah my bad, I totally mean actions. I've learned the pure/impure distinction, but hadn't carried that over to function/action :P.

Yeah in the end it didn't matter because I remembered I needed to commit the database after doing the table operation, and since that required a do block anyway, I might as well return (). I just figured, had I only needed one line, that there'd be nicer way to ignore the value. Does it make more sense if my above function had the type "stripFunc :: IO a -> ()"?

Quantining impure code is tricky. I'm trying not to be all imperative, but this little program basically runs on user input so there's IO this-and-that everywhere.

User avatar
chridd
Has a vermicelli title
Posts: 846
Joined: Tue Aug 19, 2008 10:07 am UTC
Location: ...Earth, I guess?
Contact:

Re: Coding: Fleeting Thoughts

Postby chridd » Wed Jul 07, 2010 7:34 pm UTC

Rippy wrote:Does it make more sense if my above function had the type "stripFunc :: IO a -> ()"?
It would have to be stripFunc :: IO a -> IO ().
~ chri d. d. /tʃɹɪ.di.di/ (Phonotactics, schmphonotactics) · she · Forum game scores
mittfh wrote:I wish this post was very quotable...

User avatar
phlip
Restorer of Worlds
Posts: 7573
Joined: Sat Sep 23, 2006 3:56 am UTC
Location: Australia
Contact:

Re: Coding: Fleeting Thoughts

Postby phlip » Thu Jul 08, 2010 12:17 am UTC

It seems it does exist... but for some reason it's in some obscure side-module. The definition's pretty simple, though... just a de-syntactic-sugared version of

Code: Select all

void act = do
  act
  return ()


There's not a whole lot of call for it, though, since

Code: Select all

do stuff
   someAction
   more stuff
will already perform the action and throw away the result... I guess it's useful if you need to pass an IO () to some other function...

Code: Select all

enum ಠ_ಠ {°□°╰=1, °Д°╰, ಠ益ಠ╰};
void ┻━┻︵​╰(ಠ_ಠ ⚠) {exit((int)⚠);}
[he/him/his]

User avatar
Rippy
Posts: 2101
Joined: Sun Jul 22, 2007 11:27 pm UTC
Location: Ontario, Can o' Duh

Re: Coding: Fleeting Thoughts

Postby Rippy » Fri Jul 09, 2010 4:33 pm UTC

It was only 10 minutes thinking about a game premise.

3 hours later, I'm still going through the PNG specification so I can write a PNG reading/writing library, which I'll then use to write out the results of procedural map generation, which I may then get around to working into some kind of game.

That's DFS for ya.

User avatar
TheChewanater
Posts: 1279
Joined: Sat Aug 08, 2009 5:24 am UTC
Location: lol why am I still wearing a Santa suit?

Re: Coding: Fleeting Thoughts

Postby TheChewanater » Fri Jul 09, 2010 4:41 pm UTC

Rippy wrote:It was only 10 minutes thinking about a game premise.

3 hours later, I'm still going through the PNG specification so I can write a PNG reading/writing library, which I'll then use to write out the results of procedural map generation, which I may then get around to working into some kind of game.

That's DFS for ya.

The same happened to me when I tried to figure out how to write an analog MP3 decoder for a 3d model for a contest just because it explicitly said not to make a 1930's MP3 player.
ImageImage
http://internetometer.com/give/4279
No one can agree how to count how many types of people there are. You could ask two people and get 10 different answers.

User avatar
You, sir, name?
Posts: 6983
Joined: Sun Apr 22, 2007 10:07 am UTC
Location: Chako Paul City
Contact:

Re: Coding: Fleeting Thoughts

Postby You, sir, name? » Sat Jul 10, 2010 6:57 am UTC

Dwarf fortress computer documentation (yeah, you read that right, someone built a frickin' computer. In dwarf fortress):
http://docs.google.com/Doc?docid=0AdISz ... dnaA&hl=en

Assembled:
http://mkv25.net/dfma/map-8269



I'm afraid.
I edit my posts a lot and sometimes the words wrong order words appear in sentences get messed up.

0xBADFEED
Posts: 687
Joined: Mon May 05, 2008 2:14 am UTC

Re: Coding: Fleeting Thoughts

Postby 0xBADFEED » Sun Jul 11, 2010 3:32 pm UTC

Rippy wrote:It was only 10 minutes thinking about a game premise.

3 hours later, I'm still going through the PNG specification so I can write a PNG reading/writing library, which I'll then use to write out the results of procedural map generation, which I may then get around to working into some kind of game.

That's DFS for ya.

What's wrong with using one of the many libraries that already exist for reading/writing PNG files? Writing a robust and exhaustive PNG reader/writer is a nontrivial project. If your aim is procedural map generation why waste time working on what is essentially a tangential and solved (many times over in fact) problem?

User avatar
PM 2Ring
Posts: 3715
Joined: Mon Jan 26, 2009 3:19 pm UTC
Location: Sydney, Australia

Re: Coding: Fleeting Thoughts

Postby PM 2Ring » Sun Jul 11, 2010 5:12 pm UTC

0xBADFEED wrote:
Rippy wrote:3 hours later, I'm still going through the PNG specification so I can write a PNG reading/writing library

What's wrong with using one of the many libraries that already exist for reading/writing PNG files? Writing a robust and exhaustive PNG reader/writer is a nontrivial project.

And may lead to madness. :) Any decent GUI library should give you all that you need in the way of reading & writing image files, and support for multiple file formats.

I admit I once had similar ideas, because I wanted to make something that was compact, but after getting about halfway through the PNG specs, I muttered "these guys are crazy" and gave up. OTOH, it is good to have a bit of exposure to these sort of things, but mostly so you understand why it's not productive to re-invent the wheel.

User avatar
Rippy
Posts: 2101
Joined: Sun Jul 22, 2007 11:27 pm UTC
Location: Ontario, Can o' Duh

Re: Coding: Fleeting Thoughts

Postby Rippy » Mon Jul 12, 2010 8:04 pm UTC

0xBADFEED wrote:What's wrong with using one of the many libraries that already exist for reading/writing PNG files? Writing a robust and exhaustive PNG reader/writer is a nontrivial project. If your aim is procedural map generation why waste time working on what is essentially a tangential and solved (many times over in fact) problem?

Oh I'm well aware it's reinventing the wheel. At the very least, I could be writing .bmp's and avoid the checksums and compression. But I don't have any specific goals apart from some leisure coding, and in this case reinventing the wheel seems like a nice challenge :P. Plus writing code to a specification is probably good practice for me.

As for "robust and exhaustive", I'm just writing a minimum, one-bit-depth, no-interlacing, minimum-compression implementation, keeping it modular but just writing what I need.

Speaking of which, any suggestions for a structure for storing an image in memory (in C)? Right now I'm working with something like this:
Spoiler:

Code: Select all

typedef struct {
        uint32_t height;
        uint32_t width;
        uint8_t depth;
        void **data;
} png_image_t;
The idea being that it could accommodate different **data types for different bit depths.

User avatar
You, sir, name?
Posts: 6983
Joined: Sun Apr 22, 2007 10:07 am UTC
Location: Chako Paul City
Contact:

Re: Coding: Fleeting Thoughts

Postby You, sir, name? » Mon Jul 12, 2010 8:28 pm UTC

Why do you use void**? A flat array would be much faster and safer and better in pretty much all conceivable ways.
I edit my posts a lot and sometimes the words wrong order words appear in sentences get messed up.

User avatar
Rippy
Posts: 2101
Joined: Sun Jul 22, 2007 11:27 pm UTC
Location: Ontario, Can o' Duh

Re: Coding: Fleeting Thoughts

Postby Rippy » Tue Jul 13, 2010 3:28 am UTC

You, sir, name? wrote:Why do you use void**? A flat array would be much faster and safer and better in pretty much all conceivable ways.

What do you mean? I'd use void so that it could point to different types for different bit depths (an array of 3 chars for RGB, just char for 8-bit colour etc), and ** to create a dynamically-allocated 2d array.

I feel like I'm missing something important here.

Ubik
Posts: 1016
Joined: Thu Oct 18, 2007 3:43 pm UTC

Re: Coding: Fleeting Thoughts

Postby Ubik » Tue Jul 13, 2010 7:48 am UTC

Wouldn't an union be nicer, one that contains explicitly typed pointers for each bit depth?

User avatar
phlip
Restorer of Worlds
Posts: 7573
Joined: Sat Sep 23, 2006 3:56 am UTC
Location: Australia
Contact:

Re: Coding: Fleeting Thoughts

Postby phlip » Tue Jul 13, 2010 8:00 am UTC

Yeah, a union is better than a void* when there's a list of possible types it can be... void* is better suited for when any datatype is fine, and when the function or library or whatever doesn't actually need to care what it points to (like malloc/free, which don't need to care what you're storing there, just how big it is... or qsort, which doesn't need to know the values in the array, it just needs to be able to marshal pointers into the provided comparison function).

Also: dynamically-allocated 2D arrays are just asking for severe pain... as a general rule, any time you see "a pointer to a pointer to X" you should really think hard about whether it's necessary. You'd have much less hassle with a 1D array with width*height elements.

Code: Select all

enum ಠ_ಠ {°□°╰=1, °Д°╰, ಠ益ಠ╰};
void ┻━┻︵​╰(ಠ_ಠ ⚠) {exit((int)⚠);}
[he/him/his]

User avatar
Ephphatha
Posts: 625
Joined: Sat Sep 02, 2006 9:03 am UTC
Location: Bathurst, NSW, Australia

Re: Coding: Fleeting Thoughts

Postby Ephphatha » Tue Jul 13, 2010 10:55 am UTC

phlip wrote:Also: dynamically-allocated 2D arrays are just asking for severe pain... as a general rule, any time you see "a pointer to a pointer to X" you should really think hard about whether it's necessary. You'd have much less hassle with a 1D array with width*height elements.

Most common image formats store the width and height of the image and every image loader I've looked at (not many) stores image data in a 1D array, so this is the way to go.
I'm not lazy, I'm just getting in early for Christmas is all...

User avatar
Rippy
Posts: 2101
Joined: Sun Jul 22, 2007 11:27 pm UTC
Location: Ontario, Can o' Duh

Re: Coding: Fleeting Thoughts

Postby Rippy » Tue Jul 13, 2010 12:42 pm UTC

Unions! Something I have never used practically before.

So something like this then:
Spoiler:

Code: Select all

//not too sure about naming convention for this stuff...

typedef struct {
        uint8_t bit_depth; //0 for 1bit, 1 for 8bit, 2 for 24bit, (or use an enum)
        union {
                png_pixel1_t *un_1bit;   //types for each depth supported,
                png_pixel8_t *un_8bit;   //though I suppose depths 1,2,4, and 8
                png_pixel24_t *un_24bit; //would all just be uint8_t / char...
        } id_un;
} png_imagedata_t;

typedef struct {
        uint32_t height;
        uint32_t width;
        png_imagedata_t data;
} png_image_t;

// And then some constructor/destructor functions to take care of
// setting the fields and malloc-ing/freeing.
Anyway thanks for the excellent help, I'll start a thread next time so I don't hijack fleeting thoughts, I didn't expect such an involved exchange :P


Edit: Oh and as for lack of wariness of pointers, I blame my crazy (well, crazy-awesome though) first-year advanced CS prof:
(introducing pointers-to-pointers, students groan): "Oh you think that's scary? Wait til you get to your assignment, I'm pretty sure you need triple-indirection for one problem."

To be fair, though, that was a headache-inducing data structure problem where you really did need *** IIRC (I'll try to dig up the assignment).
Last edited by Rippy on Tue Jul 13, 2010 12:55 pm UTC, edited 1 time in total.

User avatar
You, sir, name?
Posts: 6983
Joined: Sun Apr 22, 2007 10:07 am UTC
Location: Chako Paul City
Contact:

Re: Coding: Fleeting Thoughts

Postby You, sir, name? » Tue Jul 13, 2010 12:54 pm UTC

Rippy wrote:Unions! Something I have never used practically before.

So something like this then:
...code...
nyway thanks for the excellent help, I'll start a thread next time so I don't hijack fleeting thoughts, I didn't expect such an involved exchange :P


It's close, but what you'll want to do is this:

Code: Select all

typedef struct {
        uint32_t height;
        uint32_t width;
        uint8_t bit_depth; //0 for 1bit, 1 for 8bit, 2 for 24bit, (or use an enum)
        union {
                png_pixel1_t *un_1bit;   //types for each depth supported,
                png_pixel8_t *un_8bit;   //though I suppose depths 1,2,4, and 8
                png_pixel24_t *un_24bit; //would all just be uint8_t / char...
        } pixel_data;

} png_image_t;
I edit my posts a lot and sometimes the words wrong order words appear in sentences get messed up.

User avatar
Sc4Freak
Posts: 673
Joined: Thu Jul 12, 2007 4:50 am UTC
Location: Redmond, Washington

Re: Coding: Fleeting Thoughts

Postby Sc4Freak » Tue Jul 13, 2010 12:59 pm UTC

Actually, I'm not sure if that will be optimal. A union of pointers will ensure optimal packing of your pixels. A pointer to a union will mean that each pixel, no matter its type, will be at least as large as the largest pixel type in the union.

User avatar
jaap
Posts: 2094
Joined: Fri Jul 06, 2007 7:06 am UTC
Contact:

Re: Coding: Fleeting Thoughts

Postby jaap » Tue Jul 13, 2010 1:02 pm UTC

You, sir, name? wrote:It's close, but what you'll want to do is this:

You kept ninja-editing it, while I was forming a reply to point out that what you wrote is not what Rippy'd want (e.g. having a type flag for every pixiel, or then having a pointer to unions instead of union of pointers) and now in your final edit it is just about the same as the original that Rippy has.

User avatar
PM 2Ring
Posts: 3715
Joined: Mon Jan 26, 2009 3:19 pm UTC
Location: Sydney, Australia

Re: Coding: Fleeting Thoughts

Postby PM 2Ring » Tue Jul 13, 2010 2:48 pm UTC

phlip wrote:Also: dynamically-allocated 2D arrays are just asking for severe pain... as a general rule, any time you see "a pointer to a pointer to X" you should really think hard about whether it's necessary. You'd have much less hassle with a 1D array with width*height elements.

+ Eleventy billion.

Also, it's traditional in image format headers, etc to put the width before the height. One reason is that your image consists of rows of fixed width, so you need to know the width to index into the 1D array correctly, but the height value's not so important. If the file is truncated & the true height doesn't match what's in the header, it's nice if your program can still do something with the data its got.

User avatar
Rippy
Posts: 2101
Joined: Sun Jul 22, 2007 11:27 pm UTC
Location: Ontario, Can o' Duh

Re: Coding: Fleeting Thoughts

Postby Rippy » Tue Jul 13, 2010 3:23 pm UTC

PM 2Ring wrote:Also, it's traditional in image format headers, etc to put the width before the height.

I figured, but in my initial 2d array mindset it seemed most intuitive to reverse them.

On a somewhat unrelated topic, since I have little experience organizing larger projects, I'm looking for some well-structured source code to peek at. So if anyone has some open-source projects they're fond of, I'd be interested in checking them out. (that goes for Haskell and C)

User avatar
Ephphatha
Posts: 625
Joined: Sat Sep 02, 2006 9:03 am UTC
Location: Bathurst, NSW, Australia

Re: Coding: Fleeting Thoughts

Postby Ephphatha » Tue Jul 13, 2010 4:08 pm UTC

Rippy wrote:On a somewhat unrelated topic, since I have little experience organizing larger projects, I'm looking for some well-structured source code to peek at. So if anyone has some open-source projects they're fond of, I'd be interested in checking them out. (that goes for Haskell and C)

Chrome and Firefox have some pretty decent coding standards, I haven't looked at the code much so I can't really tell you if they actually follow the standards or not. (If you want some horrible code to look at, try VLC).
I'm not lazy, I'm just getting in early for Christmas is all...

User avatar
You, sir, name?
Posts: 6983
Joined: Sun Apr 22, 2007 10:07 am UTC
Location: Chako Paul City
Contact:

Re: Coding: Fleeting Thoughts

Postby You, sir, name? » Tue Jul 13, 2010 4:28 pm UTC

jaap wrote:
You, sir, name? wrote:It's close, but what you'll want to do is this:

You kept ninja-editing it, while I was forming a reply to point out that what you wrote is not what Rippy'd want (e.g. having a type flag for every pixiel, or then having a pointer to unions instead of union of pointers) and now in your final edit it is just about the same as the original that Rippy has.


Yeah, sorry about that. I was in a hurry when I wrote the first revision and made odd mistakes, then I wrote a couple of revisions in an even bigger hurry. Also, I edit basically all of my posts. A lot. Including this one.
I edit my posts a lot and sometimes the words wrong order words appear in sentences get messed up.


Return to “Coding”

Who is online

Users browsing this forum: No registered users and 5 guests