Advertisement
2002VB Miscellaneous #17295

Mosaic

Takes a picturebox, and it's contents, and runs an animated mosaic transition through it

AI

สรุปโดย AI: This codebase represents a historical implementation of the logic described in the metadata. Our preservation engine analyzes the structure to provide context for modern developers.

ซอร์สโค้ด
original-source
Sub GenMosaic(pctMosaic As Variant, MosaicMode As Integer)
'Mosaic Mode is 1 for Mosaic, 2 for DeMosaic
'Declare all objects
'======================================================================
'  This code is (C) StarFox / Dave Hng '98
'  
'  Posted on http://www.planet-source-code.com during May '98.
'
'  If you distribute this code, make sure that the complete listing is intact, with these
'  comments! If you use it in a program, don't worry about this introduction.
' 
'  Email: StarFox@earthcorp.com or psychob@inf.net.au
'  UIN: 866854
'
'  Please credit me if you use this code! As far as i know, this is the only nice(ish) VB
'  image manip sub that i've seen! This is one major code hack! :)
'
'  Takes a picturebox, and runs a animated mosaic transition though it!
'
'  Uses Safearrays, CopyMemory, Bitmap basics. Not for the faint hearted.
'
'  pctMosaic is a picturebox object that you want to run the transition through
'  MosaicMode is an integer, indicating what steps of the mosaic you want to run though.
'  1 is mosaic up, 2 is mosaic down, 3 is mosaic up, then down again. Experiment! 
'
'  Not very efficient, but the code runs at about 2x to 10x emulated speed when compiled to 
'  native code! It runs really really fast compiled under the native code compiler!
'  It's capable of animating a small bitmap on a 486dx2/80, with the interval set to 1, and 
'  no re-redraws.
'
'  Only works on 256 colour, single plane bitmaps. I'll write one for truecolour images when
'  i figure out how the RGBQuad type works, (Can anyone help?) and i've finished high school.
'
'  You can change the for.. next statements with the K and L variables to change the speed of
'  the function. K is the mosaic depth, L is the number of times to call the function (limits
'  speed, so you can see it better)
'
'  Thanks to the guys that wrote the VBPJ article on direct access to memory. Without that info
'  or ideas, i wouldnt've been able to write the sub.
'
'  This code is used in StarLaunch, my multi emulator launcher:
'  http://starlaunch.home.ml.org
'  As a transition for screen size previews for snes emulators.
'
'  Note: It does crash some computers, for no known reason. 
'  I think it's as video card -> video driver problem.
'  Don't break while this sub is running, unless you really have to. If you want to stop
'  execution, you must call the cleanup code associated with what the sub's doing.
'  (Copymemory the pointer to 0& again)
'
'  Have fun!
'
'  "If you think it's not possible, make it!"
'
'  -StarFox
Static mosaicgoing As Boolean
'Keep a static variable to check if the sub's running. If it is, EXIT! Otherwise, GPF!
If mosaicgoing = True Then Exit Sub
mosaicgoing = True
'Init variables
Dim pict() As Byte
Dim SA As SafeArray2D, bmp As BITMAP
Dim r As Integer, c As Integer, Value As Byte, i As Integer, colour As Integer, j As Integer, k As Integer, L As Integer
Dim pCenter As Integer, pC As Integer, pR As Integer
Dim rRangei As Integer, rRangej As Integer, ti As Integer, ti2 As Integer
Dim uC As Integer, uR As Integer
Dim PictureArray() As Byte
Dim mRange As Integer
Dim cLimit As Integer, rLimit As Integer
'Copy to the array
'======================================================================
GetObjectAPI pctMosaic.Picture, Len(bmp), bmp
If bmp.bmPlanes <> 1 Or bmp.bmBitsPixel <> 8 Then
  MsgBox "Non-256 colour bitmap detected. No mosaic effects"
  Exit Sub
End If
'Init the SafeArray
With SA
  .cbElements = 1
  .cDims = 2
  .bounds(0).lLbound = 0
  .bounds(0).cElements = bmp.bmHeight
  .bounds(1).lLbound = 0
  .bounds(1).cElements = bmp.bmWidthBytes
  .pvData = bmp.bmBits
End With
'Map the pointer over
CopyMemory ByVal VarPtrArray(pict), VarPtr(SA), 4
'Make a temporary array to hold the bitmap data.
ReDim PictureArray(UBound(pict, 1), UBound(pict, 2))
'Copy the bitmap into this array. I could use copymemory again, but this is fast enough, 
'and a lot safer :)
For c = 0 To UBound(pict, 1)
  For r = 0 To UBound(pict, 2)
      PictureArray(c, r) = pict(c, r)
  Next r
Next c
'Clean up
CopyMemory ByVal VarPtrArray(pict), 0&, 4
'======================================================================
Select Case MosaicMode
  Case 1
  'Mosaic
    For k = 1 To 16 Step 1
      For L = 1 To 1
		'Cube roots used, because the squaring effect looks nicer. Also, due to the
		'Nature of my code, it hides irregular the pixel expansion
        mRange = k ^ 1.5
        GoSub Mosaic
      Next L
    Next k
  Case 2
  'DeMosaic
    For k = 16 To 0 Step -(1)
      For L = 1 To 1
        mRange = k ^ 1.5
        GoSub Mosaic
      Next L
    Next k
  Case 3
  'Mosaic, then DeMosaic
    For k = 1 To 8 Step 1
      mRange = k ^ 1.5
        GoSub Mosaic
    Next k
    For k = (8) To 0 Step -(1)
      mRange = k ^ 1.5
        GoSub Mosaic
    Next k
End Select
mosaicgoing = False
Exit Sub
'Actual Mosaic Code
'======================================================================
Mosaic:
'Get the bitmap info again, in case something's changed
GetObjectAPI pctMosaic.Picture, Len(bmp), bmp
'Reinit the SA
With SA
  .cbElements = 1
  .cDims = 2
  .bounds(0).lLbound = 0
  .bounds(0).cElements = bmp.bmHeight
  .bounds(1).lLbound = 0
  .bounds(1).cElements = bmp.bmWidthBytes
  .pvData = bmp.bmBits
End With
''Fake' the pointer
CopyMemory ByVal VarPtrArray(pict), VarPtr(SA), 4
'Work out the distance between the square division grid, and the pixel to get data from.
pCenter = (mRange) \ 2
'Find the limits of the image
uC = UBound(pict, 1)
uR = UBound(pict, 2)
For c = 0 To UBound(pict, 1) Step (mRange + 1)
  For r = 0 To UBound(pict, 2) Step (mRange + 1)
	  'Work out the distance between the square division grid, and the pixel to get data from.
      pCenter = (mRange) \ 2
      
	  'Pixel size to copy over
	  rRangei = (mRange)
      rRangej = (mRange)
      
      'Check if it's running out of bound, in case you turned the compiler option off.
      If c + mRange > UBound(pict, 1) Then rRangei = UBound(pict, 1) - c
      If r + mRange > UBound(pict, 2) Then rRangej = UBound(pict, 2) - r
      
      'Work out where to get the data from
      pC = c + pCenter
      pR = r + pCenter
      If pC > UBound(pict, 1) Then pC = c
      If pR > UBound(pict, 2) Then pR = r
      'Get the palette entry
      Value = PictureArray(pC, pR)
      If c = 0 Then cLimit = -pCenter
      If r = 0 Then rLimit = -pCenter
      
      'Copy the palette entry number over the region's pixels
      For i = cLimit To (rRangei)
        For j = rLimit To (rRangej)
          If c + i < 0 Or r + j < 0 Then GoTo SkipPixel
          pict(c + i, r + j) = Value
SkipPixel:
        Next j
      Next i
SkipThis:
  
  Next r
Next c
EndThis:
'Clean up
CopyMemory ByVal VarPtrArray(pict), 0&, 4
'Refresh, so the user sees the change. Don't replace with a DoEvents! 
'Refreshing is slower, but it's less dangerous!
pctMosaic.Refresh
'======================================================================
Return
End Sub
#include <iostream.h>
#include <math.h>
#include <fstream> 
void main()
{
	int asmd;
	float add1;
	float add2;
	float subtract1;
	float subtract2;
	float multiply1;
	float multiply2;
	float divide1;
	float divide2;
	float x;
	float dia;
	float quad1;
	float quad2;
	float ang1;
	float ang2;
	float adj;
	float opp;
	float celsius;
	int quit;
	cout << " Do you want to..." << endl; 
	cout << " 1) Add" << endl;
	cout << "  2) Subtract" << endl; 
	cout << "  3) Multiply" << endl;
	cout << "  4) Divide" << endl; 
	cout << "  5) Find the square root of a number" << endl; 
	cout << "   6) Find out the circumference of a circle" << endl; 
	cout << "   7) Find out the area of a quadralateral" << endl;
	cout << "   8) Find out what an angle of a triangle is" << endl;
	cout << "   9) Solve for the hypontinus of a right angle (Pythagoreams Theorum)" << endl; 
	cout << "    10) Convert from Farenheit to Celsius" << endl;
	cin >> asmd;
	{
	if (asmd==1)
	{
		cout << "Enter the first number" << endl;
  cin >> add1;
		cout << "Enter the second number" << endl;
		cin >> add2;
		cout << "Your answer is: " << add1+add2 << endl;
	}
	else if(asmd==2)
	{
		cout << "Enter the first number" << endl;
	 cin >> subtract1;
		cout << "Enter the second number" << endl;
		cin >> subtract2;
		cout << "Your answer is: " << subtract1-subtract2 << endl;
	}
 else if(asmd==3)
	{
		cout << "Enter the first number" << endl;
	 cin >> multiply1;
		cout << "Enter the second number" << endl;
		cin >> multiply2;
		cout << "Your answer is: " << multiply1*multiply2 << endl;
	}
 else if(asmd==4)
	{
		cout << "Enter the dividend" << endl;
	 cin >> divide1;
		cout << "Enter the divisor" << endl;
		cin >> divide2;
		cout << "Your answer is: " << divide1/divide2 << endl;
	}
	
	else if(asmd==5)
	{
		cout << "Enter a number" << endl;
	 cin >> x;
		cout << "Your answer is: " << sqrt(x) << endl;
	}
	else if(asmd==6)
	{
		cout << "Enter the diameter of the circle" << endl;
	 cin >> dia;
		cout << "Your answer is: " << dia*3.14 << endl;
	}
	else if(asmd==7)
	{
		cout << "Enter the length of the quadrilateral" << endl;
	 cin >> quad1;
		cout << "Enter the width of the quadrilateral" << endl;
		cin >> quad2;
		cout << "Your answer is: " << quad1*quad2 << endl;
	}
	else if(asmd==8)
	{
		cout << "Enter the 1st angle" << endl;
	 cin >> ang1;
		cout << "Enter the 2nd angle" << endl;
		cin >> ang2;
		cout << "Your answer is: " << 180-(ang1+ang2) << endl;
	}
	else if(asmd==9)
	{
		cout << "Enter the value of the adjectant side" << endl;
	 cin >> adj;
		cout << "Enter the value of the opposite side" << endl;
		cin >> opp;
		cout << "Your answer is: " << sqrt((adj*adj)+(opp*opp) )<< endl;
	}
	else if(asmd==10)
 Range:
 	cout << "Enter a range of degrees (Ex. 0 150)";
 int low;
 int high;
 	cin >> low >> high;
 int diff = high - low;
 if (diff >= 10001) 
  	{
  	cout << "Enter a smaller range.\n";
  	goto Range;
  	}
  	
  	std::fstream fout;
  	fout.open("f2c.txt" , std::ios::out);
  	if (!fout.is_open()) return;
  
  	cout << "Farenheit to Celcius Conversion\n";
  	fout << "Farenheit to Celcius Conversion\n";
  	cout << "Farenheit\tCelcius\n";
  	fout << "Farenheit\tCelcius\n";
  	cout << "---------\t-------\n";
  	fout << "---------\t-------\n";
  int faren2;
  	while (faren2 < high)
   {
   faren2 = low;
   celsius = 0.55f * (faren2 - 32);
   cout << faren2 << "\t\t" << celsius << '\n'; 
   low++;
  }
	cout << "Press any key and return to quit" << endl;
	cin >> quit;
	
}
}
ความคิดเห็นดั้งเดิม (3)
กู้คืนจาก Wayback Machine