Hi,
Code: Select all
;
; Little Image Tool for PB Windows
; Subset of the upcoming Xpm viewer/converter :wink:
;
; Demonstrates: copy/paste images, load images from memory,
; stretch/update images, save images in Bmp format, include images in the exe.
;
; Written in PureBasic by El_Choni
;
; Thanks: Rings for helping me to deal with bitmaps in memory
;
version.s = "0.1"
Structure ImageData
width.l
height.l
colors.l
chars.l
bits.b
EndStructure
Global IData.ImageData, BmpSize, BmpImage, FileSize, loaded.b, WindowWidth, WindowHeight, ImgWidth, ImgHeight, MenuHeight, xImg, yImg, ParentHandle, ptMinTrackSizex, aspect.f
*BmpFile.BITMAPFILEHEADER
*BmpInfo.BITMAPINFOHEADER
bmp.BITMAP
#BI_RGB = 0
Global File.s
delayed.b
ptMinTrackSizex = 320
Procedure Mod(dividend.l, divisor.l)
If divisor0
integerquotient = dividend/divisor
remainder = dividend-(integerquotient*divisor)
Else
remainder = 0
EndIf
ProcedureReturn remainder
EndProcedure
Procedure UpdateImage()
If loaded
ImgHeight = WindowHeight
ImgWidth = ImgHeight*aspect
If ImgWidth>WindowWidth
ImgWidth = WindowWidth
ImgHeight = ImgWidth/aspect
EndIf
If WindowWidth=ptMinTrackSizex And ImgWidth> 16
UpdateImage()
If loaded
SetWindowText_(ParentHandle, "Little Image Tool - "+File+" "+StrF(((ImgWidth/IData\width)*100), 2)+"%")
EndIf
EndIf
Case #WM_PAINT
UpdateImage()
EndSelect
EndProcedure
Main:
tab.s = Chr(9)
open.s
save.s
tabquit.s
cien.s
zoomin.s
zoomout.s
copy.s
paste.s
If InitKeyboard()
open = tab+"Ctrl+O"
save = tab+"Ctrl+S"
tabquit = tab+"Ctrl+Q"
cien = tab+"Ctrl+Alt+0"
zoomin = tab+"+"
zoomout = tab+"-"
copy = tab+"Ctrl+C"
paste = tab+"Ctrl+V"
EndIf
If CreateMenu(0)
MenuTitle("&File")
MenuItem(0, "&Open"+open)
MenuBar()
MenuItem(1, "&Save as..."+save)
MenuBar()
MenuItem(2, "&Quit"+tabquit)
MenuTitle("&Tools")
MenuItem(3, "100%"+cien)
MenuItem(4, "Zoom in"+zoomin)
MenuItem(5, "Zoom out"+zoomout)
MenuBar()
MenuItem(6, "&Copy"+copy)
MenuItem(7, "Paste"+paste)
EndIf
InitGadget(1)
If OpenWindow(0, 100, 100, 480, 320, #PB_Window_SystemMenu|#PB_Window_MinimizeGadget|#PB_Window_SizeGadget, "Little Image Tool")
ParentHandle = WindowID()
MenuHeight = GetSystemMetrics_(#SM_CYCAPTION)+GetSystemMetrics_(#SM_CYMENU)+8
Brush.LOGBRUSH\lbColor = 0
SetClassLong_(ParentHandle, #GCL_HBRBACKGROUND, CreateBrushIndirect_(Brush))
If CreateGadgetList(ParentHandle)
CreateImage(0, WindowWidth(), WindowHeight())
ImageGadget = ImageGadget(0, 0, MenuHeight, WindowWidth(), WindowHeight()+MenuHeight, UseImage(0))
hDC = GetDC_(ImageGadget)
WindowWidth = WindowWidth()
WindowHeight = WindowHeight()
AttachMenu(0, ParentHandle)
SetWindowCallback(@ResizeCallback())
File = ProgramParameter()
If LCase(Right(File, 4))=".bmp" ; If no bmp file in the command line, then
Gosub Open
Else
BmpImage = ?IncludedImage ; we show the included image (at the bottom)
*BmpFile = BmpImage
*BmpInfo = BmpImage+SizeOf(BITMAPFILEHEADER)
IData\width = *BmpInfo\biWidth
IData\height = *BmpInfo\biHeight
Gosub ShowImage
Gosub ActualPixels
EndIf
delayed = 1
SetForegroundWindow_(ParentHandle)
Repeat
If delayed ; Keyboard check
If ExamineKeyboard()
If KeyboardPushed(#PB_Key_Substract)
Gosub ZoomOut
Else
If KeyboardPushed(#PB_Key_Add)
Gosub ZoomIn
Else
If KeyboardPushed(#PB_Key_LeftControl) Or KeyboardPushed(#PB_Key_RightControl)
If KeyboardPushed(#PB_Key_LeftAlt) Or KeyboardPushed(#PB_Key_RightAlt)
If KeyboardPushed(#PB_Key_0) Or KeyboardPushed(#PB_Key_Pad0)
Gosub ActualPixels
EndIf
Else
If KeyboardPushed(#PB_Key_V)
Gosub Paste
Else
If KeyboardPushed(#PB_Key_C)
Gosub Copy
Else
If KeyboardPushed(#PB_Key_O)
Gosub Open
Else
If KeyboardPushed(#PB_Key_S)
Gosub Save
Else
If KeyboardPushed(#PB_Key_Q)
Quit = 1
EndIf
EndIf
EndIf
EndIf
EndIf
EndIf
EndIf
EndIf
EndIf
EndIf
Else
Sleep_(200)
delayed = 1
EndIf
Select WaitWindowEvent()
Case #PB_EventMenu
Select EventMenuID()
Case 0 ; Open
Gosub Open
Case 1 ; Save
Gosub Save
Case 2 ; Quit
Quit = 1
Case 3 ; 100%
Gosub ActualPixels
Case 4 ; Zoom in
Gosub ZoomIn
Case 5 ; Zoom out
Gosub ZoomOut
Case 6 ; Copy image
Gosub Copy
Case 7 ; Paste image
Gosub Paste
EndSelect
Case #PB_EventCloseWindow
Quit = 1
EndSelect
Until Quit = 1
EndIf
EndIf
End
;
; Actual pixels (100%)
;
ActualPixels:
If loaded
ResizeWindow(IData\width, IData\height+MenuHeight)
EndIf
Return
;
; Zoom out
;
ZoomOut:
If loaded
ResizeWindow(ImgWidth/2, (ImgHeight/2)+MenuHeight)
delayed = 0
EndIf
Return
;
; Zoom in
;
ZoomIn:
If loaded
ResizeWindow(ImgWidth*2, (ImgHeight*2)+MenuHeight)
delayed = 0
EndIf
Return
;
; Open
;
Open:
File = OpenFileRequester("Open image file","","Windows BMP Image (*.bmp)|*.bmp", 0)
If LCase(Right(File, 4))=".bmp"
Gosub OpenBmp
Gosub ShowImage
Gosub ActualPixels
EndIf
Return
;
; Save
;
Save:
If loaded
File = SaveFileRequester("Save image file","","Windows BMP Image (*.bmp)|*.bmp", 0)
If CreateFile(0, File)
WriteData(BmpImage, *BmpFile\bfSize)
CloseFile(0)
EndIf
EndIf
Return
;
; Copy image to clipboard
;
Copy:
If loaded
ciHwnd = CreateCompatibleBitmap_(hDC, IData\width, IData\height)
SetDIBits_(hDC, ciHwnd, 0, IData\height, BmpImage+*BmpFile\bfOffBits, BmpImage+SizeOf(BITMAPFILEHEADER), 0)
OpenClipboard_(ParentHandle)
EmptyClipboard_()
SetClipboardData_(#CF_BITMAP, ciHwnd)
CloseClipboard_()
EndIf
Return
;
; Paste image
;
Paste:
OpenClipboard_(ParentHandle)
If IsClipboardFormatAvailable_(#CF_BITMAP)
If loaded
DeleteObject_(iHwnd)
FreeMemory(0)
Else
FreeImage(0)
EndIf
ciHwnd = GetClipboardData_(#CF_BITMAP)
GetObject_(ciHwnd, SizeOf(BITMAP), @bmp)
SelectObject_(hDC, ciHwnd)
IData\width = bmp\bmWidth
IData\height = bmp\bmHeight
IData\bits = bmp\bmBitsPixel*bmp\bmPlanes
pad = (bmp\bmWidthBytes-IData\width)*IData\height
iHwnd = CreateCompatibleBitmap_(hDC, IData\width, IData\height)
loaded = 1
aspect = IData\width/IData\height
RGBQuad = 0
If IData\bits>3))+pad
BmpImage = AllocateMemory(0, BmpSize, 0)
*BmpInfo = BmpImage+SizeOf(BITMAPFILEHEADER)
*BmpInfo\biSize = SizeOf(BITMAPINFOHEADER)
*BmpInfo\biWidth = IData\width
*BmpInfo\biHeight = IData\height
*BmpInfo\biPlanes = 1
*BmpInfo\biBitCount = IData\bits
*BmpInfo\biCompression = #BI_RGB; Currently supports only uncompressed images
GetDIBits_(hDC, ciHwnd, 0, IData\height, BMPImage+SizeOf(BITMAPFILEHEADER)+SizeOf(BITMAPINFOHEADER)+2+RGBQuad, BMPImage+SizeOf(BITMAPFILEHEADER), #DIB_RGB_COLORS)
CopyMemory(*BmpInfo, BmpImage+SizeOf(BITMAPFILEHEADER), SizeOf(BITMAPINFOHEADER)+RGBQuad+2)
FreeMemory(1)
*BmpFile = BmpImage
*BmpFile\bfType = 19778
*BmpFile\bfOffBits = SizeOf(BITMAPFILEHEADER)+SizeOf(BITMAPINFOHEADER)+RGBQuad+2
*BmpFile\bfSize = BmpSize
*BmpFile\bfReserved1 = 0
*BmpFile\bfReserved2 = 0
SetDIBits_(hDC, iHwnd, 0, IData\height, BmpImage+*BmpFile\bfOffBits, BmpImage+SizeOf(BITMAPFILEHEADER), 0)
ResizeWindow(IData\width, IData\height+MenuHeight)
EndIf
CloseClipboard_()
Return
;
; Open Bmp file
;
OpenBmp:
If ReadFile(0, File)
FileSize = FileSize(File)
If loaded
FreeMemory(0)
EndIf
BmpImage = AllocateMemory(0, FileSize, 0)
ReadData(BmpImage , FileSize)
CloseFile(0)
*BmpFile = BmpImage
*BmpInfo = BmpImage+SizeOf(BITMAPFILEHEADER)
IData\width = *BmpInfo\biWidth
IData\height = *BmpInfo\biHeight
EndIf
Return
;
; Save Bmp file
;
SaveBmp:
If CreateFile(0, File)
WriteData(BmpImage, *BmpFile\bfSize)
CloseFile(0)
EndIf
Return
;
; Show image
;
ShowImage:
If BmpImage
If loaded
DeleteObject_(iHwnd)
Else
FreeImage(0)
EndIf
ResizeWindow(IData\width, IData\height+MenuHeight)
iHwnd = CreateCompatibleBitmap_(hDC, IData\width, IData\height)
SetDIBits_(hDC, iHwnd, 0, IData\height, BmpImage+*BmpFile\bfOffBits, BmpImage+SizeOf(BITMAPFILEHEADER), 0)
loaded = 1
aspect = IData\width/IData\height
SetForegroundWindow_(ParentHandle)
EndIf
Return
IncludedImage: ; change path to meet your Bmp file path
IncludeBinary "C:\Sistema\Programacion\PureBasic\Programas\Xpm\testicon.bmp"
El_Choni
Edited by - EL_Choni on 12 March 2002 01:48:33