1//
2//  PXPSDHandler.m
3//  Pixen-XCode
4//
5//  Created by Andy Matuschak on Thu Jun 10 2004.
6//  Copyright (c) 2004 Open Sword Group. All rights reserved.
7//
8
9#import "PXPSDHandler.h"
10#import <QuickTime/QuickTime.h>
11
12@implementation PXPSDHandler
13
14/*(Handle)dataRefForData:data
15{
16	Str255 mimeTypePascalStr;
17	Str255 namePascalStr;
18	Handle dataRefXtnsnHndl;
19	ComponentInstance dataHandler;
20	PointerDataRef dataref =
21		              (PointerDataRef)NewHandle(sizeof(PointerDataRefRecord));
22	Handle dataRefWithExtensions;
23
24
25
26	osstat = OpenADataHandler(dataRef,
27							  PointerDataHandlerSubType,
28							  nil,
29							  (OSType)0,
30							  nil,
31							  kDataHCanRead,
32							  &dataHandler);
33
34	// now that the data handler has copied it,
35	// we don't need our original copy of the data ref
36	DisposeHandle((Handle)dataref);
37
38	// mix in the the mime type of the media
39	osstat = PtrToHand(mimeTypePascalStr,
40					   &dataRefXtnsnHndl,
41					                      mimeTypePascalStr[0]+1);
42
43	osstat = DataHSetDataRefExtension(dataHandler,
44									                                    dataRefXtnsnHndl,
45									                                    kDataRefExtensionMIMEType);
46	                                  DisposeHandle(dataRefXtnsnHndl);
47
48	// mix in the name of the media
49	osstat = PtrToHand(namePascalStr,
50					                      &dataRefXtnsnHndl,
51					                      namePascalStr[0]+1);
52
53	osstat = DataHSetDataRefExtension(dataHandler,
54									                                    dataRefXtnsnHndl,
55									                                    kDataRefExtensionFileName);
56	                                  DisposeHandle(dataRefXtnsnHndl);
57
58	// retrieve the data ref with its added extensions
59	DataHGetDataRef(dataHandler, &dataRefWithExtensions);
60
61	// don't need our data handler instance anymore
62	CloseComponent(dataHandler);
63}*/
64
65+imagesForPSDData:(NSData *)data
66{
67	ComponentInstance importer, exporter = 0;
68	PointerDataRef dataRef = (PointerDataRef)NewHandle(sizeof(PointerDataRefRecord));
69	long imageCount = 0;
70	int i;
71	id images = [[NSMutableArray alloc] init];
72
73	char * buffer = malloc([data length]);
74	[data getBytes:buffer];
75	(**dataRef).data = buffer;
76	(**dataRef).dataLength = [data length];
77
78	GetGraphicsImporterForDataRef((Handle)dataRef,PointerDataHandlerSubType,&importer);
79	GraphicsImportGetImageCount(importer, &imageCount);
80	for (i = 0; i < imageCount; i++)
81	{
82		long offset, size;
83
84		GraphicsImportSetImageIndex(importer, i);
85		PointerDataRef testRef = (PointerDataRef)NewHandle(sizeof(PointerDataRefRecord));
86		GraphicsImportGetDataOffsetAndSize(importer, &offset, &size);
87		GraphicsImportGetDataReference(importer, (Handle *)testRef, NULL);
88		// create a Graphics Exporter component that will write BMP data
89		OSErr err = OpenADefaultComponent(GraphicsExporterComponentType, kQTFileTypeBMP, &exporter);
90		if (err == noErr)
91		{
92			// set export parameters
93			Handle bmpDataH = NewHandle(0);
94			GraphicsExportSetInputGraphicsImporter(exporter, importer);
95			GraphicsExportSetOutputHandle(exporter, bmpDataH);
96
97			// export data to BMP into handle
98			unsigned long actualSizeWritten = 0;
99			err = GraphicsExportDoExport(exporter, &actualSizeWritten);
100			if (err == noErr)
101			{
102				// export done: create the NSData that will be returned
103				HLock(bmpDataH);
104				id image = [[[NSImage alloc] initWithData:[NSData dataWithBytes:*bmpDataH length:GetHandleSize(bmpDataH)]] autorelease];
105				[images addObject:image];
106				HUnlock(bmpDataH);
107			}
108			DisposeHandle(bmpDataH);
109			CloseComponent(exporter);
110		}
111	}
112	CloseComponent(importer);
113	DisposeHandle((Handle)dataRef);
114	free(buffer);
115	[images autorelease];
116	return images;
117}
118
119@end
120