AGB  ·  Datenschutz  ·  Impressum  







Anmelden
Nützliche Links
Registrieren
Zurück Delphi-PRAXiS Programmierung allgemein Multimedia Delphi Fast integer RGB-HSL
Thema durchsuchen
Ansicht
Themen-Optionen

Fast integer RGB-HSL

Ein Thema von WojTec · begonnen am 29. Dez 2010 · letzter Beitrag vom 30. Dez 2010
Antwort Antwort
Thom

Registriert seit: 19. Mai 2006
570 Beiträge
 
Delphi XE3 Professional
 
#1

AW: Fast integer RGB-HSL

  Alt 29. Dez 2010, 22:55
@himitsu,
so, wie ich das verstanden habe, geht es darum, dass ihm diese Funktionen zu langsam sind:
Zitat:
It is exactly what I need... One thing I can't accept is performance
Eine mögliche Antwort wäre: Umschreiben in Assembler...
Thomas Nitzschke
  Mit Zitat antworten Zitat
Medium

Registriert seit: 23. Jan 2008
3.688 Beiträge
 
Delphi 2007 Enterprise
 
#2

AW: Fast integer RGB-HSL

  Alt 30. Dez 2010, 00:49
As far as I see it, all the calculations can be done purely with integers. To keep some proper precision, this should be done at some higher scaling than 0..255, a factor of 100 or 1000 should suffice - any scale up to |scale*MaxValueAnywhere|<MaxInt may be chosen though.
Working strictly in the range 0..1 usually only makes things a whole lot easier (or merely possible in some cases), if nonlinear functions like powers or trigonometry come into play, of which none are involved here.

Going assembler does not generally yield better performance, MMX is not an option with values >255, but handmade SSE(2) might work. Downside: This should be done right where the load of calculations are performed, to avoid switching the FPU from/into SSE mode for every tiny conversion - this could even make things slower than the current solution. Thus, I'd first try to go all integer, and see how that performs.

Edit: Another thing might be de-modularizing the whole thing. There's a lot of calling to tiny helper functions going on, and iirc, Delphi doesn't inline (at least not per default, and not at all with older versions). Doing everything in one go, even if it results in much longer and possibly uglier code, may improve speed a bit, too. But certainly not as much as eliminating the floats completely, by far.

Edit2: Also, I'm a little unsure why these functions are so long. I haven't read them that thoroughly, but here's two methods I use in a C# project of mine:
Code:
public void ColorToHSV(Color c, out double h, out double s, out double v)
      {
         double r = c.R/255f;
         double g = c.G/255f;
         double b = c.B/255f;
         
         double max = Math.Max(r, Math.Max(g, b));
         double min = Math.Min(r, Math.Min(g, b));
         
         if (max.Equals(min)) h = 0; else
         if (max.Equals(r)) h = 60f*((g-b)/(max-min)); else
         if (max.Equals(g)) h = 60f*(2f+(b-r)/(max-min)); else
         if (max.Equals(b)) h = 60f*(4f+(r-g)/(max-min)); else
         h = 0;
         
         if (h<0) h += 360;
         
         if (max.Equals(0)) s = 0; else
         s = (max-min)/max;
         
         v = max;
         
         return;
      }
      
      public Color HSVToColor(double h, double s, double v)
      {
         int hi = (int)Math.Floor(h/60f);
         double f = h/60f - hi;
         double p = (v*(1-s))*255f;
         double q = (v*(1-s*f))*255f;
         double t = (v*(1-s*(1-f)))*255f;
         v *= 255f;
         Color cn = new Color();
         switch (hi) {
            case 0:
               cn = Color.FromArgb(255, (byte)v, (byte)t, (byte)p);
               break;
            case 1:
               cn = Color.FromArgb(255, (byte)q, (byte)v, (byte)p);
               break;
            case 2:
               cn = Color.FromArgb(255, (byte)p, (byte)v, (byte)t);
               break;
            case 3:
               cn = Color.FromArgb(255, (byte)p, (byte)q, (byte)v);
               break;
            case 4:
               cn = Color.FromArgb(255, (byte)t, (byte)p, (byte)v);
               break;
            case 5:
               cn = Color.FromArgb(255, (byte)v, (byte)p, (byte)q);
               break;
            case 6:
               cn = Color.FromArgb(255, (byte)v, (byte)t, (byte)p);
               break;               
         }
         return cn;
      }
It's full of floats though, and thus not meant for speed, but it seems a lot shorter and less complex than what you posted, and can most certainly even be optimized and shortened further
"When one person suffers from a delusion, it is called insanity. When a million people suffer from a delusion, it is called religion." (Richard Dawkins)

Geändert von Medium (30. Dez 2010 um 01:04 Uhr)
  Mit Zitat antworten Zitat
Benutzerbild von himitsu
himitsu

Registriert seit: 11. Okt 2003
Ort: Elbflorenz
44.378 Beiträge
 
Delphi 12 Athens
 
#3

AW: Fast integer RGB-HSL

  Alt 30. Dez 2010, 08:19
@Medium: HSV <> HSL (Wiki)
Aber grundsätzlich ist es ähnlich, so da man sich einen HSV-Code eventuell entsprechend anpassen könnte.

@Thom: Ich dachte er hat sich auch noch drüber beschwert, daß dieses nur von 0..255, anstatt von 0..359 geht
Ein Therapeut entspricht 1024 Gigapeut.

Geändert von himitsu (30. Dez 2010 um 08:21 Uhr)
  Mit Zitat antworten Zitat
Medium

Registriert seit: 23. Jan 2008
3.688 Beiträge
 
Delphi 2007 Enterprise
 
#4

AW: Fast integer RGB-HSL

  Alt 30. Dez 2010, 14:10
HSL and HSV are that similar, that there is virtually no notable difference. It's just that L ranges from black to white, while V is from black to 50% gray. The RGB->HSL/V conversions shown here reflect that similarity pretty well, so that my code's coarse structure is still a valid indicator for the algorithms complexity.
"When one person suffers from a delusion, it is called insanity. When a million people suffer from a delusion, it is called religion." (Richard Dawkins)
  Mit Zitat antworten Zitat
WojTec

Registriert seit: 17. Mai 2007
482 Beiträge
 
Delphi XE6 Professional
 
#5

Re: Fast integer RGB-HSL

  Alt 30. Dez 2010, 15:38
Maybe you mean HSV/HSB? If I know HSL != HSV.

Ok, let's back to the topic. I made some changes, so now working not bad and I can use in image processing
  Mit Zitat antworten Zitat
Antwort Antwort


Forumregeln

Es ist dir nicht erlaubt, neue Themen zu verfassen.
Es ist dir nicht erlaubt, auf Beiträge zu antworten.
Es ist dir nicht erlaubt, Anhänge hochzuladen.
Es ist dir nicht erlaubt, deine Beiträge zu bearbeiten.

BB-Code ist an.
Smileys sind an.
[IMG] Code ist an.
HTML-Code ist aus.
Trackbacks are an
Pingbacks are an
Refbacks are aus

Gehe zu:

Impressum · AGB · Datenschutz · Nach oben
Alle Zeitangaben in WEZ +1. Es ist jetzt 22:39 Uhr.
Powered by vBulletin® Copyright ©2000 - 2025, Jelsoft Enterprises Ltd.
LinkBacks Enabled by vBSEO © 2011, Crawlability, Inc.
Delphi-PRAXiS (c) 2002 - 2023 by Daniel R. Wolf, 2024-2025 by Thomas Breitkreuz