View Single Post
Old 4th March 2017, 03:35   #382  |  Link
StainlessS
HeartlessS Usurer
 
StainlessS's Avatar
 
Join Date: Dec 2009
Location: Over the rainbow
Posts: 10,980
RT_Stats v2.0 Beta 08, new version, see MediaFire (or SendSpace) in sig below this post.

Code:
    v2.0 Beta 6. 28 Feb 2017.
        RT_DBaseReadCSV(), relaxed Separator requirement if SPACE/TAB separated. Added StrDelimiter string Arg.
    v2.0 Beta 7. 02 Mar 2017.
        RT_DBaseReadCSV(), Added StartField and EndField args. Max CSV line length increased to 64*1024 bytes.
    v2.0 Beta 8. 04 Mar 2017.
        Added Functions:
            RT_DBaseWriteCSV(), RT_DBaseFindSeq() and RT_DBaseRgbColorCount().
RT_DBaseWriteCSV
Code:
RT_DBaseWriteCSV(String DB, String CSV, String "Separator"=",",String "StrDelimiter"="\"",Int "StartField"=0,Int "EndField"=last_field
        Int "Low"=0,Int "High"=Last_Record,Bool "Append"=False,String "Fmt"="")

    This function will write a consecutinve number of fields from a DBase to CSV values in text file, for each DBase record between
    records low and high inclusive. 

    DB,           FileName of an RT_Stats DBase file.
    CSV,          FileName of an output text CSV (Comma Separated Value) file.
    Separator     Default "," ie comma separator (first character only, Chr()'s 0, 13, 10, and 34 [Double Quote] illegal).
                  Default Separator "," (Comma).
                   
    StrDelimiter  Default = "\"" ie single character string Double Quote. String delimiters written around string fields when written to CSV.
                  StrDelimiter (String Delimiter) can be multiple characters (empty string "" illegal, also cannot contain the Separator 
                  character nor SPACE or TAB), Default is a Double Quote single character string.
                  DBase string values CANNOT contain StrDelimiter string, ie for default StrDelimiter, strings cannot contain a double quote
                  character string. So long as the StrDelimiter does not appear within any DB string, nearly any StrDelimiter can be 
                  used, eg "@@@" is a 3 character StrDelimiter that you could use in place of double quote string delimiters, where the 
                  string values could then contain a double quote character, in such a case you could enclose CSV strings as in 
                  @@@Some text that contains a " double quote.@@@.
                  NOTE, StrDelimiter IS case sensitive, so if using alphabetic characters they have to match exactly.
                  
    StartField    Default = 0.
    EndField      Default = last field in DBase record (By default, all fields are written to CSV file).
                  Above StartField and EndField args allow writing of limited number of CONSECUTIVE fields within a record.

    Append        Default False. When True, append if CSV file already exists. 

    Fmt           Default, "". The Fmt string provides a little bit of optional formatting for field types Int, Bin and Bool.
                  The length of Fmt string can be up to EndField-StartField+1, ie 1 character for each of the selected fields.
                  A single character can be set for each of the fields, first one affects StartField, and the last one affects
                  EndField (assuming all characters are set). A shorter Fmt string only affects those fields for the characters that 
                  exist in the Fmt string.
                  A SPACE (' ') or '.' Fmt character uses default formatting behaviour (Proper Case for Bool, ie 1st charcter is upper 
                  case, remainder are lower case).
                  Type Int, If ' ' or '.' then default eg "-1". 
                    If 'x' then print fields as lower case Hex eg ffffffff.
                    If 'X' then print fields as upper case Hex eg FFFFFFFF.
                    If '$' then print fields as upper case Hex preceded by a '$' Dollar character eg  eg $FFFFFFFF.
                  Type Bool. If ' ' or '.' then default eg "True".
                    if 'l' or 'L' then lower case eg 'true'.                    
                    if 'u' or 'U' then upper case eg 'TRUE'.                    
                  Type Bin, If ' ' or '.' then default eg "255". 
                    If 'x' then print fields as lower case Hex eg ff.
                    If 'X' then print fields as upper case Hex eg FF.
                    If '$' then print fields as upper case Hex preceded by a '$' Dollar character eg  eg $FF.
                  Types String, Float (or internal use type Double) will error if using anything other than '.' or ' '.
                                        

    Function returns Int, the number of records added to the CSV file.

    Example:-
        CSVTXT="""
          1,42.5,$FF,true, "D:\OUT\Test_000001.d2v"   # Comment
          2,43.5,$FE,false,"D:\OUT\Test_000002.d2v"
          3,44.5,$FD,true, "D:\OUT\Test_000003.d2v"
          4,45.5,$FC,false,"D:\OUT\Test_000004.d2v"
         -1,45.5,$FC,false,"D:\OUT\Test_000004.d2v" 
        """
        
        DB     = "My.DB"
        CSVIN  = "CSVIN.txt"
        CSVOUT = "CSVOUT.txt"  # Output CSV file.
        RT_WriteFile(CSVIN,"%s",CSVTXT)       # Create test Input CSV
        RT_DBaseAlloc(DB,0,"ifnbs512")        # Allocate DBase with 0 pre-allocated records, 5 fields, Int, Float, Bin, Bool and String[512].
        Read=RT_DBaseReadCSV(DB,CSVIN)
        Written=RT_DBaseWriteCSV(DB,CSVOUT,Append=false,Fmt="$.x..")        
        S=RT_String("Read = %d written=%d",read,Written)   # Shows "Read=5 Written=5".
        MessageClip(S)
RT_DBaseFindSeq (same functionality as DB_FindSeq script function here:- https://forum.doom9.org/showthread.p...16#post1799116 )
Code:
RT_DBaseFindSeq(String DB,Int S_Field,Int E_Field,Int Frame,Int "Low"=0,Int "High"=RT_DBaseRecords(DB)-1)
 
 RT_DBaseFindSeq Returns clip (or record) number containing movie frame Frame, using Binary Search.
   
   DB,      DBase FileName,
   S_Field, DB field that contains the first frame of the record clip/sequence (within entire movie).
   E_Field, DB field that contains the Last frame of the record clip/sequence (within entire movie).
   Low,     Default 0. Lowest record number to scan (likely almost always 0).
   High,    Default RT_DBaseRecords(DB)-1. Hightest record number to scan (likely almost always as default).
   
   Return of -1 = RECORD NOT FOUND.
   
   eg If you have 4 clips in DBase, each of 10 frames then fields should contain data EXACTLY as below (must be ordered).
      Record     S_Field   E_Field
         0           0        9
         1          10       19
         2          20       29
         3          30       39
RT_DBaseRgbColorCount
Code:
RT_DBaseRgbColorCount(String DB, clip c,Int "n"=current_frame)
    RT_DBaseRgbColorCount, creates an RT_Stats DBase containing data on colors contained a video clip frame.
    RGB24 and RGB32 only.
    DB, Name of DBase.
    c,  Clip.
    n,  Default current_frame, frame to count RGB (will use current_frame in runtime environment).
    Returns number of records in DBase, ie number of unique colors on frame n.
    DBase Fields:-
        ColorField = 0
        CountField = 1
        
    Example:-
        DB="COLS.DB"
        FRAME=0
        ColorField = 0
        CountField = 1
        ###
        Avisource("OutAvi.Avi").ConvertToRGB24.Trim(FRAME,-1)
        Cnt=RT_DBaseRgbColorCount(DB,Last,0)                  # Count colors of frame 0  (originally FRAME). 
        RT_DBaseQuickSort(DB,Field=CountField,Field2=ColorField)  # Sort primarily by count, and then by color (ascending)
        MostCommonColor=RT_DBaseGetField(DB,Cnt-1,ColorField)     # Last one in DBase (last record)
        MostCommonColorCount=RT_DBaseGetField(DB,Cnt-1,CountField)
        RT_Subtitle("Unique Colors=%d\nMost Common Color=$%X (Count=%d)",Cnt,MostCommonColor,MostCommonColorCount)
        Overlay(BlankClip(Width=64,Height=64,Color=MostCommonColor,Length=1).LetterBox(2,2,2,2,$FF00FF),x=(width/2)-34,y=(height/2)-34)
        return last
__________________
I sometimes post sober.
StainlessS@MediaFire ::: AND/OR ::: StainlessS@SendSpace

"Some infinities are bigger than other infinities", but how many of them are infinitely bigger ???

Last edited by StainlessS; 4th March 2017 at 06:20.
StainlessS is offline   Reply With Quote