Announcement

Collapse
No announcement yet.

text file to graphic file png with added code for bmp

Collapse
X
  • Filter
  • Time
  • Show
Clear All
new posts

  • text file to graphic file png with added code for bmp

    Here is some code i am working on and figured i would go ahead and share it
    The ideal program for me is text file to black and white png file

    During the testing
    I found out that if i did not go back through the image and convert all pixels that where not white to black,
    I would end up with not all pixels that where not white to be black and some grayshade colors.
    I left the code in to create a bmp file so you can see for yourself.

    I worked on parsing out the text for a full day from the text file.
    A text file may have form feeds in it and it approached it wrong and still do.
    I should have parsed out the pages into separate arrays of text for each page.
    The reason for day long event was because i wanted add formatting for pages, you know adjustable margins sizes,
    beginning of page and ending of line spacing, adjustable number of spaces for tabs, etc.
    But I sort of hit a wall and needed to think about it more.
    So I jumped to working on the text to graphic part with some more basic formatting.

    This program will trim the text file to a end of file marker, I believe it is character CHR$(26) if it is found in the text file.
    This program replaces all CRLF with LF, and then CR with LF. I have came across files i need to convert that have a mixture of CRLF, LF, and CR in them.
    This better anyway sticking with LFs because it will handle files from other operating systems.

    This program is ok, but i still wanted to handle the pages differently than this program is doing but this program does allow for page breaks
    with form feed characters in the file.

    Page breaks are handled and replaced by a single continuous line of x number of underline characters in this program, but the x is fixed for now.
    I also want to make my graphic images a multiple of 4 in height and width, this program does not do that yet
    Also i want to save the png file as tight as possible but i am having trouble with learning some gdiplus functions and i do not thing my gidplus.inc is up to date.
    I used another set of gdiplus include files that should be able to be found on the forum or replace it with your own gdiplus include files.
    Also, I would like to create a 2 color(monochrome/depth2) bmp file . They make use of indexes in the bmp file as far as i can tell and i am not there yet.
    Hopefully I will be able to knock out a few things, specially the formatting part.
    Of course you can break out text files with multiple pages just process them separately to create separate graphic files.
    One of options hopefully in the future on the formatting will be an option to create individual graphic file from a text file with form feeds,
    but all this takes time to work out.

    There is a program that will compress your png files called "pngout.exe" and can be gotten on the internet.
    There is a program call PDFREAD on the web that has some useful tools in it if you want see those.

    Playing with "pngout" here are few command line options you might want to tryout to compress your png file from the output of this program.
    PNGOUT does replace the png file, so you are warned.
    fastest time to convert and compress a png file commmand "pngout /d1 /c0 /f5 /s2 filename.png"
    smallest png file out produced with command options plus with acceptable speed "pngout /d1 /c0 /f5 /s1 filename.png"
    to create even smaller png files other command options or no options can be used but it is much slower.

    Put a single text file on the command line
    This program will create a bmp file without the conversion of non white pixels to black for your inspecting purposes.
    The bmp file created will be named your command line full file name with .bmp added on to the end like "fulltextfilename.bmp"
    The png file created will be named your command line full file name with .png added on to the end like "fulltextfilename.png"
    The png file will have only solid black and solid white pixels only.

    Code:
    'text2png.bas
    'read text file and convert it to a png file
    'compiled with pbwin10 but pbcc 4.04 should work fine and i started the code with pbcc
    '
    #COMPILE EXE
    #DIM ALL
    #INCLUDE ONCE "WIN32API.INC"
    #INCLUDE ONCE "inc\image\gdip_cut.inc"
    #INCLUDE ONCE "inc\image\ImgFtoBM.inc"
    
    
    'GraphicSaveBitmapToImageFile(dwnewbitmap,"c:\pbwin10\a.jpg")
    GLOBAL gsinputfilename  AS STRING
    GLOBAL gspngoutputfilename AS STRING
    GLOBAL gsbmpoutputfilename AS STRING
    
    GLOBAL gsfilecontents AS STRING
    GLOBAL gtextlines() AS STRING
    GLOBAL glprintgraphicheight AS LONG
    GLOBAL glprintgraphicwidth AS LONG
    GLOBAL gtextlines() AS STRING
    GLOBAL gifontheight AS SINGLE
    GLOBAL gsfontname AS STRING
    GLOBAL gsleftmargin AS STRING
    GLOBAL gsrightmargin AS STRING
    GLOBAL gimakeimagebinarycolor AS LONG
    GLOBAL gifontstype AS LONG
    GLOBAL gifontcharset AS LONG
    
    
    
    FUNCTION PBMAIN () AS LONG
        LOCAL iresult AS LONG
        LOCAL i AS LONG
    
        FUNCTION=1
        gsfontname="Consolas"
        gifontheight=36 '72
        'gsleftmargin=SPACE$(4)
        'gsrightmargin=SPACE$(4)
        gimakeimagebinarycolor=1&
        gifontstype=0&  ' 0=normal 1=bold 2=italic 4=underline 8=strikeout 16=normal
        gifontcharset=0&
        gsinputfilename=COMMAND$(1)
        IF LCASE$(RIGHT$(gsinputfilename,4))<>".txt" THEN EXIT FUNCTION
        gsbmpoutputfilename=gsinputfilename+".bmp"
        gspngoutputfilename=gsinputfilename+".png"
    
        IF LEN(gsinputfilename)=0 OR LEN(gspngoutputfilename)=0 OR NOT ISFILE(gsinputfilename) THEN
            EXIT FUNCTION
        END IF
        iresult=readinputfile 'get contents from the file
        IF iresult=0 THEN EXIT FUNCTION
        iresult=convertcontents2array
        IF iresult=0 THEN EXIT FUNCTION
        IF LEN(gsfilecontents)=0 THEN EXIT FUNCTION
        IF UBOUND(gtextlines())=1 THEN EXIT FUNCTION
        findmaxlinesizes
        creategraphicfile
        FUNCTION=0
    END FUNCTION
    
    
    
    FUNCTION findmaxlinesizes() AS LONG
        LOCAL pImage AS DWORD
        LOCAL i AS LONG
        LOCAL fonthndl AS DWORD
        LOCAL itestWidthVar,itestHeightVar AS SINGLE
    
    
    
        glprintgraphicwidth=0&
        glprintgraphicheight=0&
        GRAPHIC BITMAP NEW 15&,15& TO pImage
        GRAPHIC ATTACH pImage, 0
        FONT NEW gsfontname,gifontheight, gifontstype, gifontcharset, 0&, 0& TO fonthndl
        GRAPHIC SET FONT fonthndl
        GRAPHIC CLEAR %WHITE , 0
        GRAPHIC COLOR %BLACK, %WHITE
        FOR i= LBOUND(gtextlines()) TO UBOUND(gtextlines())
            REM  GRAPHIC TEXT SIZE gsleftmargin+gtextlines(i)+gsrightmargin TO itestWidthVar,itestHeightVar
             GRAPHIC TEXT SIZE gsleftmargin+REPEAT$(80,"W")+gsrightmargin TO itestWidthVar,itestHeightVar
             IF glprintgraphicwidth<itestWidthVar THEN glprintgraphicwidth=itestWidthVar
              glprintgraphicheight+=itestHeightVar
        NEXT i
        FONT END fonthndl
        GRAPHIC DETACH
        GRAPHIC BITMAP END
    END FUNCTION
    
    FUNCTION creategraphicfile() AS LONG
        LOCAL pImage AS DWORD
        LOCAL i AS LONG
        LOCAL hStatus AS LONG
        LOCAL pixelptr AS LONG PTR
        LOCAL fonthndl AS DWORD
        LOCAL sgraphicbits AS STRING
        LOCAL GdipToken AS DWORD
        LOCAL StartupInput AS GdiplusStartupInput
    
    
        StartupInput.GdiplusVersion = 1
        IF GdiplusStartup(GdipToken,StartupInput, BYVAL %NULL) THEN
            MSGBOX "Error initialising GDI+ ... (Also, program needs XP or later)"
            EXIT FUNCTION
        END IF
    
    
        IF glprintgraphicwidth=0& THEN EXIT FUNCTION
        IF glprintgraphicheight=0& THEN EXIT FUNCTION
        GRAPHIC BITMAP NEW glprintgraphicwidth,glprintgraphicheight TO pImage
        GRAPHIC ATTACH pImage, 0
        FONT NEW gsfontname,gifontheight, gifontstype, gifontcharset, 0&, 0& TO fonthndl
        GRAPHIC SET FONT fonthndl
        GRAPHIC CLEAR %WHITE , 0
        GRAPHIC COLOR %BLACK, %WHITE
    
        FOR i= LBOUND(gtextlines()) TO UBOUND(gtextlines())
            GRAPHIC PRINT gsleftmargin+gtextlines(i)+gsrightmargin
        NEXT i
        FONT END fonthndl
    
       GRAPHIC SAVE gsbmpoutputfilename
    
        IF gimakeimagebinarycolor THEN
            GRAPHIC GET BITS TO sgraphicbits
            pixelptr= STRPTR(sgraphicbits)+8&
            FOR i=1& TO (glprintgraphicwidth*glprintgraphicheight)
                IF @PixelPtr <> BGR(%WHITE) THEN @PixelPtr = BGR(%BLACK)
                INCR PixelPtr
            NEXT i
            GRAPHIC SET BITS sgraphicbits
            sgraphicbits=""
        END IF
    
    '    GRAPHIC SAVE gsbmpoutputfilename
    '    GRAPHIC DETACH
        GraphicSaveBitmapToImageFile(pImage,gspngoutputfilename)
        GRAPHIC BITMAP END
        GdiplusShutdown GdipToken
    
    END FUNCTION
    
    FUNCTION  readinputfile() AS LONG
         'get contents from the file
        FUNCTION=0
        TRY
            OPEN gsinputfilename FOR BINARY ACCESS READ WRITE LOCK SHARED AS #1 LEN=32767
        CATCH
            CLOSE #1
            EXIT FUNCTION
        EXIT FUNCTION
        EXIT TRY
        FINALLY
        END TRY
        GET$ #1, LOF(1), gsFileContents
        CLOSE #1
        IF LEN(gsFileContents)=0 THEN EXIT FUNCTION
        FUNCTION=1
    END FUNCTION
    
    FUNCTION convertcontents2array()AS LONG
        LOCAL i,j AS DWORD
        LOCAL stemp AS STRING
        LOCAL stabspaces AS STRING
        LOCAL sformfeedreplacement AS STRING
    
        FUNCTION=0
    
        stabspaces=SPACE$(8)
        sformfeedreplacement=REPEAT$(79,"_")
        i=INSTR(gsfilecontents, CHR$(26))
        IF i THEN
            IF i=1 THEN gsfilecontents="":EXIT FUNCTION
            gsfilecontents=LEFT$(gsfilecontents,i-1)
        END IF
        IF INSTR(gsfilecontents, $FF) THEN
            IF  gsfilecontents=$FF THEN
                gsfilecontents="":EXIT FUNCTION
            END IF
            WHILE INSTR(gsfilecontents, $FF+$FF)
                REPLACE $FF+$FF WITH $FF IN  gsfilecontents
                IF  gsfilecontents=$FF THEN
                    gsfilecontents="":EXIT FUNCTION
                END IF
            WEND
            WHILE RIGHT$(gsfilecontents,1)=$FF
                gsfilecontents=LEFT$(gsfilecontents,LEN(gsfilecontents)-1)
                IF gsfilecontents=$FF THEN
                    gsfilecontents="":EXIT FUNCTION
                END IF
            WEND
            WHILE LEFT$(gsfilecontents,1)=$FF
                gsfilecontents=RIGHT$(gsfilecontents,LEN(gsfilecontents)-1)
                IF gsfilecontents=$FF THEN
                    gsfilecontents="":EXIT FUNCTION
                END IF
            WEND
            WHILE INSTR(gsfilecontents, $FF)
                IF gsfilecontents=$FF THEN
                    gsfilecontents="":EXIT FUNCTION
                END IF
                REPLACE $FF WITH sformfeedreplacement+$LF IN  gsfilecontents
            WEND
        END IF
    
        IF INSTR(gsfilecontents, $TAB) THEN
            WHILE INSTR(gsfilecontents, $TAB)
                REPLACE $TAB WITH stabspaces IN  gsfilecontents
            WEND
        END IF
        WHILE INSTR(gsfilecontents, $CRLF)
            REPLACE $CRLF WITH $LF  IN  gsfilecontents
        WEND
        WHILE INSTR(gsfilecontents, $CR)
            REPLACE $CR WITH $LF  IN  gsfilecontents
        WEND
        IF LEN(gsfilecontents)=0 THEN EXIT FUNCTION
        IF RIGHT$(gsfilecontents,1)<>$LF THEN gsfilecontents=gsfilecontents+$LF
        I=PARSECOUNT(gsfilecontents,$LF)
        REDIM gtextlines(1 TO i) AS STRING
        PARSE gsfilecontents,gtextlines(),$LF
        FOR i= LBOUND(gtextlines()) TO UBOUND(gtextlines())
            gtextlines(i)=RTRIM$(gtextlines(i))
        NEXT i
        FUNCTION=1
    
    END FUNCTION



    Attached Files
    p purvis

  • #2
    bytes usage report on a readme.txt text file for an example
    Code:
           4,912 ReadMe.txt
      38,089,014 readme.txt.bmp
         415,697 readme.txt.png
    
    pngout program using fastest to slowest compression with options i used on file readme.txt.png
    pngout using options /d1 /c0 /f5 /s2
         107,721 readme.txt.pngout.png
    pngout using options /d1 /c0 /f5 /s1
          81,750 readme.txt.pngout.png
    pngout using options /d1 /c0 /f5 /s0
          81,555 readme.txt.pngout.png
    p purvis

    Comment

    Working...
    X