« first day (2088 days earlier)      last day (1383 days later) » 

acr
9:05 AM
Hi everyone. I have a question regarding trait implementation strategy. Basically, I have a trait to convert a slice of pixels into a slice of pixels of another type: https://github.com/raymanfx/ffimage/blob/next/src/core/traits.rs#L124. Imagine converting from a slice of RGB to a slice of Gray pixels. My design is based around slice conversion so I can easily handle macropixels, where one pixel is converted into 2 or more pixels of another type.

Implementing the trait in the crate itself is fine for me, but I have an external crate to handle YUV buffers. There, I cannot implement the trai
 
 
1 hour later…
10:16 AM
@acr You are very likely looking for the newtype pattern (example) which you could use to wrap the slice type in with.
(At the same time, you could / should implement From / Into for this newtype and its wrapped slice type to make it more convenient to use if you public interfaces rely on slices instead of the newtype)
*your
 
acr
10:39 AM
@PeterVaro thanks for the hint. I thought about adding a new type for this exact reason. Would you recommend wrapping the slice of pixels itself or wrapping a reference to such a slice instead?
Thinking about it, won't I just run into the very same issue again?
I would define the new type (let's call it PixelSlice) in the main ffimage crate, but I need to implement the conversion (From/Into if you will) in the ffimage-yuv subcrate. Since PixelSlice is now a foreign type, how does this help my original problem?
 
@acr You wouldn't use it like that. You would define YuvSlice and RgbSlice separately in their own crates, and in their own crates you would implement the external trait
(It is quite possible I misunderstood your situation / problem)
 
acr
Hm, I think I get the idea now, thank you! I'll try to prototype the solution in an hour.
 
Sure thing, let me know how it goes and whether the proposed idiom is actually the thing you were looking for..
 
acr
11:00 AM
@PeterVaro my existing convert implementation is here: github.com/raymanfx/ffimage/blob/next/src/packed/convert/….
How would I implement this for generic pixel types when I have distinct Slice types such as YuvSlice and RgbSlice?
 
11:17 AM
@acr Umm.. Am I correct to think: A pixel could be represented by a bunch of sequential numerical values, which as far as I can see, you decided to express as StorageType and Pixel traits. Then you need to express a bunch of pixels together (as an image perhaps) and you created the GenericBuffer type. And then to convert between two pixel representations you created the TryConvertSlice trait which you use to convert between two generic buffers.
 
I can't believe there is no way using entry doc.rust-lang.org/std/collections/hash_map/…, to replace a value , return the old one and get a reference to the new one - -
 
@Stargateur insert + get ?
 
@PeterVaro O(1) please
 
@Stargateur If you already have an entry it won't do yet another lookup when you call the get, so it is O(1)
 
there is no reason this should not be O(1)
@PeterVaro ah thx, I assume self was consumed
perfect
std entry is so good
in fact if you want the full story I search a crate that do size limit cache
but they are not good at all
so I try to fix them
lru is a nightmare
 
acr
11:24 AM
@PeterVaro what you stated seems to be correct as far as I can see.
 
cached look ok
 
acr
(re: ffimage design - pixels, buffers, etc)
I'm now struggling to express the conversion while being able to implement n-to-n pixel conversion. 1-to-1 is easy, but I need to work with slices to cover macropixels, where 5 YUYV pixels convert into 10 RGB ones.
 
@acr Umm, a naive thought: why don't you create a universal representation into which all of types can be converted, and from which all of your types could be converted to?
It feels to me, that would solve all of your problems. And that would be just a slice of numerical values I believe
(For which I would probably use the num package (or just num-traits) to express any kind of numerical values instead of the slightly bit clunky StorageType of yours )
 
acr
I am using num-traits in other parts of the library anyway, so maybe I can use it instead of StorageType as well. That's a different issue though I believe.
 
@acr Hence my comment is in parenthesis ;)
 
acr
11:37 AM
About the universal representation: that doesn't really make sense to me (might be because I'm missing something). Do you have a prototype for a similar problem?
Pixels don't just convert into something universal I believe. In my case, YUYV converts into YUV, which then converts into RGB.
My main issue I think is this: if slices of own types were not treated as foreign types, I would not have this issue. This leads me to believe I'm very close to an actual solution (a thought that may very well be very wrong).
Also, re: using From/Into instead of my own convert traits. I remember why I decided against that. Often, you want to convert buffers in a loop, meaning the output buffer only needs to be allocated once (and then pixel data can be rendered etc). Using From/Into, a new output buffer would be returned each time, thus a new allocation in each iteration.
 
@acr Slices are not owned types! They are only views to a memory location with a known length
Anyway, let me think about this for a second
 
acr
I know :) I just tried to explain why I'm not eager to change the design at this stage, because I believe a solution with the existing design is possible. I could very well be wrong though.
 
My problem now with what you just is that if 1 YUYV converts to 2 RGB, then how do you want to handle the other way around, where you have 1 RGB?
You can't convert that to 1/2 YUYV, right?
 
acr
no, but that's why the trait is called TryConvertSlice. I'm checking the amount of input pixels at runtime. If there's only 1 RGB input, I'll return Err().
Hang on, I'll upload the exact YUV code I'm talking about. Maybe that helps you understand what I'm saying.
 
I think I do understand.. what I don't understand is why you make your life so complex
Give me a sec, let me show what I mean
 
acr
11:47 AM
@PeterVaro Here's the case that doesn't work: github.com/raymanfx/ffimage-yuv/blob/tmp/src/yuyv.rs#L76.
Beware, that entire repo is WIP code, no comments.
 
 
2 hours later…
acr
2:02 PM
@PeterVaro I came up with a really dumb workaround for my problem to avoid the foreign type clash: hastebin.com/korijukuno.php.
 
2:22 PM
@acr That looks.. well.. not so great IMO :see_no_evil: Sorry mate for not getting back to you -- I have to work at the same time and I'm in a bit of a pickle atm. I still believe, you're overcomplicating this: if I were you I would use the &[N] (where N: Num) as the bottom-type, all the wrapper types (RGB, YUYB, etc.) should be just newtypes and could be converted from and into slices (with for example TryFrom and TryInto) and that's it.
You have your n-to-n conversion, the error handling, and far less types and traits (and way less macros!)
 
acr
Hey, don't worry - I'm not expecting any response or waiting for you. After all, we do this in our free time.
I guess I'm just not familiar enough to see what you mean. It sounds like a major redesign so I'm afraid it won't work out in the end and just be a waste of time instead.
 
Umm.. so think about it this way: every representation that you want to use for pixels is just a sequence of numbers stored somewhere. The main difference is the amount of those numbers (and obviously the extra logic in which way these numbers are used in calculations). So if you would have struct XRepr<'a, N: Num>(&'a [N]); and struct YRepr<'a, N: Num>(&'a [N]); as two different representations of pixels, you could immediately see, that actually the bottom type is indeed &[N] and you
could convert into and from this type. Then you could have your fancy implementations of XRepr and YRepr which would do their own stuff, internally relying on the fact that their length is known since their creation (which should only happen via the try_into or try_from methods)
 
acr
That makes it kind of hard to interact with images though. Imagine a function like set_pixel(pix: XRepr). This would be kind of annoying since one would need to pass some arbitrary existing memory location instead of creating a pixel object (XRepr) in-place.
I think that was my main argument for designing Pixels as [N] and not &[N]
 
@acr fn set_pixel<P: TryInto<XRepr>>(pix: P) { todo!() }
 
acr
That doesn't help the case, does it? I can't do:
```
let pix = Rgb<u8>::new([255, 255, 255]);
img.set_pixel(pix);
```
with your version, some memory must already exist somewhere which is then interpreted as a pixel
I get that the same functionality is possible with your design
I'm just thinking it takes away a bit of clarity and at the same time I'm not able to see the benefits (which is probably my fault).
Can you show me a prototype of color format conversion using your design? It's still not clear to me how you convert between arbitrary slices I think.
I'll create a playground to see if I understand your idea.
 
2:53 PM
I will get back to you later (hopefully) but I think there's a bit of confusion around who owns the values! I thought the pixel values are stored somewhere already, and you just need to view on them. Whenever you're about to perform an action that requires the construction of a representation (such as your set_pixel function), the slice could point to the array defined in your scope at that point.
If that's not enough, because a pixel should outlive the call of such function, then you should consider using an owned type, such a fixed-sized array over a slice
 
acr
@PeterVaro Something I just noticed: conversion using From/Into is inefficient for slices. In my case, I'm going to convert pixel rows, often with 1000 pixels per row or more. Using From/Into, I need to allocate the output buffer instead of just taking a preallocated one, no?
 
@acr A slice only consists of a pointer and a length, practically speaking, that's the size of two usize (give or take) -- there's no allocation involved in the From/Into or TryFrom/TryInto when we're talking about slices
 
acr
this shows where my confusion is I think .. the conversion method in line 24 is not clear to me
 
@acr Will take a look at it later -- thanks for the reduced example!
@acr &'a [T; 3] This is not a slice! That's a reference to an array btw
(anyway, bbl)
 
acr
I know, but those representations can be used interchangeably afaik?
kk, see you later
 
3:42 PM
@E_net4ofthedownvotebrigade Guess what ? I need GATs !
 
4:14 PM
@acr I meant something like this, where, as you can see, each type's responsibility is to know how to be converted down to the common type and then you can do rest by x -> common -> y instead of x -> y
(Again, as I said before: you may want to use an owned type in the first place if that makes more sense to you (which in this scenario simply could be a fixed-size array). I just assumed, that you have a huge chunk of memory somewhere, representing all the pixels as an array, and you just create these small fragments (i.e. pixel representations) on top of that, i.e. you don't need ownership of the data they hold -- hence my suggestion to use &[N])
(I just realised that I forgot to update the C/P of <Yuyv as Pixel>::channels which should return 4.. not that it would make any difference in this toy example, but still)
@Stargateur Practically speaking, I stumble into that problem twice a week.. very upsetting, to say the least!
 
acr
4:31 PM
@PeterVaro thank you for the code. Let me ask a silly question: are you familiar with image color format conversion? My main problem with your example is that the TryFrom impl for Rgb just reinterprets the bytes, but no actual conversion is performed.
Now if I introduce a packed YUV format (say YUV4:4:4), how will I implement the conversion between Rgb <-> YUYV and Rgb <-> YUV at the same time?
I cannot just convert between slice and pixel type, because I need to know at compile time what pixel type is converted into what other pixel type.
 
Right, so it's not just a different view on the same data, but actual conversion on it (as in, changing the internal values)? How could you generalise that completely (in an n-to-n fashion) without specifying the different cases one by one?
 
acr
Exactly, it's actual conversion I'm interested in.
My latest next branch on the ffimage crate does exactly that - generalised n-to-n conversion for multiple types (views, flat buffers and owning buffers).
--- with the ugly workaround I showed you earlier, that is ---
The cases are of course still implemented separately. My design is composed of several stages:

1) Pixels
2) Convert between single pixels (1-to-1) or macropixels and other pixels (n-to-n)
3) Define pixel row conversion based on 2)
4) Define whole image conversion based on 3)
 
We may have a miscommunication here, when you say n-to-n, are you referring to the 5x to 10y ?
Because I was referring to n types to n types (as in convert any pixel to any other pixel)
 
acr
Examples: RGB to YUYV (1-to-2), RGB to YUV (1-to-1), YUYV to X (2-to-n)
 
So the former..
 
acr
4:45 PM
Oh, we have a misunderstanding then.
Yeah
 
Okay, so let's go layer by layer: for the pixel-to-pixel conversion, you have to define your TryFrom/TryInto types for every possible combination you want to have, correct?
 
acr
Exactly.
 
When you say macropixels, you mean a sequence of one type to a sequence of another type (which you referred to as n-to-n), correct?
 
acr
It also includes the 2-to-1 case, because e.g. YUYV is really just two image pixels (YUV)
But yes.
Oh wait, sorry.
Not "a sequence of a type"
It's a type in itself. Just like a regular Pixel, but a single macropixel - when converted - yields several other pixels or vice versa
Maybe this explains it a bit better: fourcc.org/pixel-format/yuv-yuy2.
 
Umm.. still a bit a fuzzy, but essentially, [Rgb; 2] -> Yuyv is not the correct way to think about it, right?
 
acr
4:52 PM
It is correct.
 
I'm correct of being not correct? :see_no_evil:
 
acr
This is the sole reason why my design converts pixel slices into pixel slices and not just (what I had initially) pixels into pixels.
What you wrote there is correct :P
[Rgb; 2] -> Yuyv <------- this part
 
@acr Great, so it is THEN a sequence of a type to sequence of another type
As a generalisation, in this case it will be a sequence of one element
 
acr
Oh well, in that case I guess it is.
I stand corrected.
 
It's not just about being pedantic, but actually IMO this would influence your implementation
You may not need a new macro type (though you could have one if you like) but you could think about those as sequences of pixels
 
acr
4:55 PM
That's what I'm doing right now :P
I don't have a separate Macropixel type. For me, those are indeed just sequences of pixels.
 
Marvellous! So far we are on the exact same page
 
acr
I'm sorry for all the trouble - I guess this is a rather specific problem domain and not an easy one at that.
 
It is specific (though I worked with graphics before) there were just many things that lost in the communication
(and the linked repo is too big to just grasp what's going on with a single look)
 
acr
Me not being a native english speaker doesn't help here either.
 
Me neither, so don't worry about it. Besides your english is quite good
 
acr
4:59 PM
And yes, the ffimage repo is already quite complex (at least I tried to comment).
 
All right, so finally I think we arrived to the point where I will be able to understand your problem: which exact trait (not the name but its functionality) that you are struggling with?
(As in, defining it somewhere and use it in another crate)
 
acr
Without this change, I cannot implement the TryConvertSlice trait for Pixel types which are defined in external crates. Because here: impl<SP: Pixel, DP: From<SP>> TryConvertSlice<DP> for [SP] the [SP] type is foreign since slices are always foreign.
 
acr
And if now DP is a foreign type too (let's say the Rgb<T> type of the main ffimage crate), the compiler barks because every single type is foreign (SP would be the local Pixel type, but [SP] is foreign).
That commit itself is the workaround which makes the design work for me.
 
@acr and so why can't you write this entire thing as:
impl<'a, D> TryFrom<&'a [D]> for Whatever
    where D: Pixel
{
    type Error = /* ... */;

    fn try_from(pixels: &'a [D]) -> Result<Self, Self::Error>
    {
        todo!()
    }
}
where Whatever is a newtype wrapping your slice of "SP"?
(and then it could be: Whatever::try_from(d)?.inner() where Whatever::inner gives you the internal slice if you still need that and the newtype is not enough)
 
acr
5:16 PM
So first of all, please correct me if I'm wrong, I think From/TryFrom is inefficient here.
It requires me to allocate the output pixel row every time instead of being able to reuse an existing row buffer.
The wrapping newtype sounds like a workable solution to me other than that. I'm still worried about the efficiency though.
Or actually, it doesn't. Because this then breaks my conversion design, see: github.com/raymanfx/ffimage/blob/next/src/packed/convert/….
Here I make use of the trait bound SP: TryConvertSlice<DP> to implement whole image conversions based on the slice conversions.
With your approach, I would need to somehow integrate Whatever there as well, no? And since Whatever is different for each Pixel type, well ...
 
 
1 hour later…
6:40 PM
@PeterVaro wow so much ? you must like use trait :p
 

« first day (2088 days earlier)      last day (1383 days later) »