Добавил:
Upload Опубликованный материал нарушает ваши авторские права? Сообщите нам.
Вуз: Предмет: Файл:
Linux Timesaving Techniques For Dummies.pdf
Скачиваний:
59
Добавлен:
15.03.2015
Размер:
15.98 Mб
Скачать

408 Technique 53: Using Open-Source APIs to Save Time

Building a Simple Flash Movie with Ming

Listing 53-2 shows a simple Ming program that slowly rotates the colors in a given photo.

Examining the program

The following sections take a closer look at the colors program, shown in Listing 53-2.

LISTING 53-2: COLORS.C

Line 6: Defining the functions and data types

Every Ming program must include the ming.h header file (line 6). ming.h defines the data types and function prototype for the Ming library.

Line 19: Creating an empty movie

To create a new Flash movie, start with a call to the newSWFMovie() function (see line 19). newSWFMovie() creates a new, empty movie and returns a handle that you can use later when you call other Ming functions.

1

/* File: colors.c

 

2

**

 

 

3

**

Rotate colors within a photo

4

*/

 

 

5

 

 

 

6

#include <ming.h>

 

7

#include <stdio.h>

 

8

#include <stdlib.h>

 

9

 

 

 

10

static SWFDisplayItem load_photo( SWFMovie movie, const char * name );

11

 

 

 

12

int main( int argc, char * argv[] )

13

{

 

 

14

 

SWFMovie

movie;

15

 

SWFDisplayItem item;

16

 

int

frames = 40;

17

 

float

count;

18

 

 

 

19movie = newSWFMovie();

20item = load_photo( movie, argv[1] );

22for( count = 0; count < frames; ++count )

23{

24SWFDisplayItem_multColor( item, 1.0, 1.0, 1.0 - count/frames, 1.0 );

25SWFDisplayItem_addColor( item, 255 * count/frames, 0, 0, 1.0 );

26

27SWFMovie_nextFrame( movie );

28}

29

30for( count = frames; count > 0; --count )

31{

32SWFDisplayItem_multColor( item, 1.0, 1.0, 1.0 - count/frames, 1.0 );

33SWFDisplayItem_addColor( item, 255 * count/frames, 0, 0, 1.0 );

34

35 SWFMovie_nextFrame( movie );

36 }

Building a Simple Flash Movie with Ming

409

37

38 SWFMovie_save( movie, “colors.swf”, -1 );

39

40exit( EXIT_SUCCESS );

41}

42

 

 

43

static SWFDisplayItem load_photo( SWFMovie movie, const char * name )

44

{

 

45

SWFBitmap

bitmap;

46

SWFInput

photo_file;

47

FILE

* photo;

48

int

height;

49

int

width;

50

 

 

51if(( photo = fopen( name, “rb” )) == NULL )

52{

53fprintf( stderr, “can’t open photo\n” );

54exit( EXIT_FAILURE );

55}

56else

57{

58photo_file = newSWFInput_file( photo );

59}

60

61 bitmap = newSWFBitmap_fromInput( photo_file ); 62

63width = SWFBitmap_getWidth( bitmap );

64height = SWFBitmap_getHeight( bitmap );

66SWFMovie_setDimension( movie, width, height );

68return( SWFMovie_add( movie, (SWFBlock) bitmap ));

70}

Line 20: Adding a photo to the movie

At line 20, you see a call to the load_photo() function. load_photo() is not a Ming function (we show it to you in a moment in the section, “Line 43: Telling Ming how to read the photo file”). load_photo() returns a handle to a bitmap that’s been added to the new movie.

Lines 22–28 and 30–36: Making the photo move through frames

Line 24 adjusts each pixel in the photo. In a Flash movie, every pixel has four values: a red value, a green value, a blue value, and an opaqueness value. The color values range from zero (no color) to 255 (full color). The opaqueness value (also known as the alpha value) determines how much of the background shows through. An opaqueness value of zero means the pixel is invisible, and a value of 255 means the pixel is completely opaque (none of the background shows through).

Lines 22–28 create a number of frames (a movie is made up of one or more frames), each with a slightly modified version of the photo.

Each time through the loop (lines 22–28),

SWDisplayItem_multColor() multiplies each color value in each pixel. The first argument identifies the

410 Technique 53: Using Open-Source APIs to Save Time

display item you want to adjust. The second item specifies a multiplier for the red value of each pixel (multiplying by 1.0 means the red value is unchanged). The third, fourth, and fifth arguments specify the multipliers applied to the green, blue, and opaqueness values for each pixel.

Line 24 decreases the blue value by a fraction each time through the loop but leaves the other values unchanged. The net effect is that the blueness of your photo slowly fades away with each frame. Line 25 adds a dash of red to each pixel in the frame (again, leaving the other color components unchanged).

Lines 30–36 do essentially the same thing as lines 22–28, but rotate through colors in the opposite direction — subtracting some red and adding back the blue in each frame.

Line 38: Saving the completed movie

After the movie is complete, call SWFMovie_save() to write it to disk (in this case, to a file named colors.swf).

Line 43: Telling Ming how to read the photo file

Now take a look at the load_photo() function (starting at line 43). load_photo() starts by opening the file whose name was passed as the second argument. The Ming library can’t really do much with a FILE handle.

To read the content of the photo, you have to convert the FILE handle into an SWFInput. Whenever the Ming library needs to read information from some source, it uses an SWFInput handle. Ming can create SWFInput handles from a number of different source types. For example, the newSWFInput_buffer() function will create an SWFInput that can read data from an area of memory. Because you already have a FILE handle, you can use newSWFInput_file() to convert it into an SWFInput (see line 58).

Line 61 and 63–66: Showing Ming how to work with the photo

Line 61 reads the photo bits and creates a new structure called an SWFBitmap by calling newSWFBitmap_ fromInput().

Lines 63 and 64 interrogate the bitmap to find the width and height (in pixels), and line 66 changes the size of the movie to match the bitmap. Finally, load_ photo() adds the bitmap to the current frame of the movie and returns a handle to the caller.

Compiling the program

Before you can run colors, you have to compile it, and the following steps show you how to do that using make:

1. Open your favorite editor and enter the following text:

LDFLAGS += -lming -lz

LDFLAGS += -lungif -lpng12 -lm colors: colors.c

2. Save your changes in a file named Makefile and close the editor.

3. Type make and press Enter.

The compiler starts running.

If everything goes well, you have a new program in your current directory named colors. If you see any error messages, go back and correct the errors before proceeding.

Running the program

To run colors, find a photo (JPEGs work well) that you like and then run the following command:

$ ./colors /path-to-photo

Substitute the name of your photo for path-to-photo. When colors is finished, you’ll have a new Flash movie in your current directory named colors.swf.

Соседние файлы в предмете Операционные системы