It is currently Tue Oct 16, 2018 9:45 am

All times are UTC + 1 hour




Post new topic Reply to topic  [ 7 posts ] 
Author Message
 Post subject: Saving 8bit Raw data as 8bit 256 color JPG no way
PostPosted: Thu Oct 11, 2018 5:57 pm 
Offline
Enthusiast
Enthusiast

Joined: Wed Jun 23, 2010 5:13 pm
Posts: 173
Hi,
Thanks to Rashad I've been able to Save 8bit Raw data as BMP but can't do the sabe for JPG

Code:

Procedure AsciiBitBit(*Source, *Dest, memSize)
  Define lupBitBit
  For lupBitBit = 0 To memSize-1
    PokeA(*Dest+lupBitBit, PeekA(*Source+lupBitBit))
  Next
EndProcedure 

Procedure FlipRawRows(*RawData, w, h)  ;Applies or unapplies #PB_PixelFormat_ReversedY
  Protected *pbuf, *psrc, *ptmp, *pdst, i, width = w, height = h
    *pbuf = *RawData
    *psrc = *pbuf
    *ptmp = AllocateMemory(width*height)
    If *ptmp
      *pdst = *ptmp + (width*height)-width
      For i = 0 To height-1
        CopyMemory(*psrc, *pdst, width)
        *pdst-width
        *psrc+width
      Next i
      CopyMemory(*ptmp, *pbuf, width*height)
      FreeMemory(*ptmp)                     
      ProcedureReturn *pbuf
    EndIf
    ProcedureReturn 0
EndProcedure

ProcedureDLL Save8bitImage(*RawData, w , h , filename$ = "", ColorMod = #Gray)
 
  Define Bmp.BITMAP, BmiInfo.BITMAPINFOHEADER
 
    With BmiInfo.BITMAPINFOHEADER
      \biSize         = SizeOf(BITMAPINFOHEADER)
      \biWidth        = w
      \biHeight       = h
      \biPlanes       = 1
      \biBitCount     = 8
    EndWith
    Define sz_colorbits = BITMAP_GetWidthBytes(w, 8)*h
    Define NumColors = 256
    If ColorMod = #Gray
      Define *colortable = GrayscaleTable()
    Else
      Define *colortable = ColorTable()
    EndIf 
    Define sz_image = SizeOf(BITMAPFILEHEADER) + SizeOf(BITMAPINFOHEADER) + NumColors*SizeOf(RGBQUAD) + sz_colorbits
    Define *rawimage = AllocateMemory(sz_image)
    Define *fileheader.BITMAPFILEHEADER = *rawimage
    Define *header = *rawimage + SizeOf(BITMAPFILEHEADER)
    With *fileheader
      \bfType = $4D42 ; "BM" for Bit Map
      \bfSize = sz_image
      \bfOffBits = SizeOf(BITMAPFILEHEADER) + SizeOf(BITMAPINFOHEADER) + NumColors*SizeOf(RGBQUAD)
    EndWith
   
  CopyMemory(BmiInfo, *header, SizeOf(BITMAPINFOHEADER))
  CopyMemory(*colortable, *rawimage + SizeOf(BITMAPFILEHEADER) + SizeOf(BITMAPINFOHEADER), NumColors*SizeOf(RGBQUAD))
  CopyMemory(*RawData, *rawimage + SizeOf(BITMAPFILEHEADER) + SizeOf(BITMAPINFOHEADER) + NumColors*SizeOf(RGBQUAD), sz_colorbits)
  FreeMemory(*colortable)
    define file = CreateFile(#PB_Any, filename$)
    If file
      WriteData(file,*rawimage,MemorySize(*rawimage))
      CloseFile(file)
    EndIf
    FreeMemory(*rawimage)
    ProcedureReturn 1
EndProcedure

  Define *FlipImgData = AllocateMemory(w*h)
  If *FlipImgData
    AsciiBitBit(*ImgData, *FlipImgData, w*h)
    FlipRawRows(*FlipImgData, w, h)
    If Save8bitImage(*FlipImgData, w, h, -1, "c:\temp\"+rndFileName+".bmp")
      Debug "BMPSaveOK"
      If FileSize("c:\temp\"+rndFileName+".bmp") > 0
        Debug "BMPSizeOk"
        If SoftDPIset("c:\temp\"+rndFileName+".bmp", 500)
          Debug "BMPdpiOk"
        EndIf 
      EndIf
    EndIf 
    FreeMemory(*FlipImgData)
  EndIf


Thank You very much


Top
 Profile  
Reply with quote  
 Post subject: Re: Saving 8bit Raw data as 8bit 256 color JPG no way
PostPosted: Fri Oct 12, 2018 11:14 am 
Offline
User
User

Joined: Sun Nov 23, 2014 1:18 pm
Posts: 23
Haven't tried it, but why not taking the BMP-image from memory with CatchImage() and saving it as JPG?

You could change your ProcedureDLL Save8bitImage() like this (untested!)
Code:
  Protected img_no
  UseJPEGImageEncoder()
...
  CopyMemory(BmiInfo, *header, SizeOf(BITMAPINFOHEADER))
  CopyMemory(*colortable, *rawimage + SizeOf(BITMAPFILEHEADER) + SizeOf(BITMAPINFOHEADER), NumColors*SizeOf(RGBQUAD))
  CopyMemory(*RawData, *rawimage + SizeOf(BITMAPFILEHEADER) + SizeOf(BITMAPINFOHEADER) + NumColors*SizeOf(RGBQUAD), sz_colorbits)

  img_no = CatchImage(#PB_Any, *rawimage)
  if img_no
    saveimage(img_no, filename$, #PB_ImagePlugin_JPEG)
  endif
...


Top
 Profile  
Reply with quote  
 Post subject: Re: Saving 8bit Raw data as 8bit 256 color JPG no way
PostPosted: Fri Oct 12, 2018 4:41 pm 
Offline
Enthusiast
Enthusiast

Joined: Wed Jun 23, 2010 5:13 pm
Posts: 173
Thank You very much for reply.
But Saving by means of Purebasic is 24/32 bit only , no 8 bit
During SaveImage Automatically converted to 24


Top
 Profile  
Reply with quote  
 Post subject: Re: Saving 8bit Raw data as 8bit 256 color JPG no way
PostPosted: Fri Oct 12, 2018 6:55 pm 
Offline
PureBasic Expert
PureBasic Expert

Joined: Sun Aug 08, 2004 5:21 am
Posts: 3256
Location: Netherlands
LiK137 wrote:
But Saving by means of Purebasic is 24/32 bit only , no 8 bit
During SaveImage Automatically converted to 24

There is no such thing as a 256 color JPG. It doesn't exist.

_________________
macOS 10.14 Mojave, PB 5.62 x64


Top
 Profile  
Reply with quote  
 Post subject: Re: Saving 8bit Raw data as 8bit 256 color JPG no way
PostPosted: Sat Oct 13, 2018 9:12 am 
Offline
Enthusiast
Enthusiast

Joined: Wed Jun 23, 2010 5:13 pm
Posts: 173
Sorry, You are right about 256 color but what about 8bit greyscaled jpeg?


Top
 Profile  
Reply with quote  
 Post subject: Re: Saving 8bit Raw data as 8bit 256 color JPG no way
PostPosted: Mon Oct 15, 2018 11:21 am 
Offline
User
User

Joined: Sun Nov 23, 2014 1:18 pm
Posts: 23
If PNG is a suitable image format for your demands, PB could deliver this out of the box...

The PB-Help says
Code:
The depth in which to save the image. Valid values are 1, 4, 8, 24 and 32. The default value is the original image depth. For now, only BMP and PNG encoders support palletized image format (1, 4 or 8-bit).

Otherwise i would think you have to use an external image library that is able to write 8-bit greyscale JPG.


Top
 Profile  
Reply with quote  
 Post subject: Re: Saving 8bit Raw data as 8bit 256 color JPG no way
PostPosted: Mon Oct 15, 2018 12:10 pm 
Offline
Enthusiast
Enthusiast

Joined: Wed Jun 23, 2010 5:13 pm
Posts: 173
Thank You for reply.
Only Jpeg is wanted as PureBasic naively encodes BMP in polarized.
I have reviewed all posts regarding GDI+ and FreeImage but unfortunately didn't find anything useful.
I have Jpeg images encoded (in Photoshop) but this not the solution I need.


Top
 Profile  
Reply with quote  
Display posts from previous:  Sort by  
Post new topic Reply to topic  [ 7 posts ] 

All times are UTC + 1 hour


Who is online

Users browsing this forum: No registered users and 1 guest


You cannot post new topics in this forum
You cannot reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum

Search for:
Jump to:  

 


Powered by phpBB © 2008 phpBB Group
subSilver+ theme by Canver Software, sponsor Sanal Modifiye