void CanvasRenderingContext2D::setShadow(float width, float height, float blur, float c, float m, float y, float k, float a) { state().m_shadowOffset = FloatSize(width, height); state().m_shadowBlur = blur; state().m_shadowColor = ""; GraphicsContext* dc = drawingContext(); if (!dc) return; // FIXME: Do this through platform-independent GraphicsContext API. #if PLATFORM(CG) const CGFloat components[5] = { c, m, y, k, a }; CGColorSpaceRef colorSpace = CGColorSpaceCreateDeviceCMYK(); CGColorRef shadowColor = CGColorCreate(colorSpace, components); CGColorSpaceRelease(colorSpace); CGContextSetShadowWithColor(dc->platformContext(), CGSizeMake(width, height), blur, shadowColor); CGColorRelease(shadowColor); #endif }
void myOperator_Do(CGPDFScannerRef s, void *info) { // Check to see if this is an image or not. const char *name; CGPDFObjectRef xobject; CGPDFDictionaryRef dict; CGPDFStreamRef stream; CGPDFContentStreamRef cs = CGPDFScannerGetContentStream(s); // The Do operator takes a name. Pop the name off the // stack. If this fails then the argument to the // Do operator is not a name and is therefore invalid! if(!CGPDFScannerPopName(s, &name)){ fprintf(stderr, "Couldn't pop name off stack!\n"); return; } // Get the resource with type "XObject" and the name // obtained from the stack. xobject = CGPDFContentStreamGetResource(cs, "XObject", name); if(!xobject){ fprintf(stderr, "Couldn't get XObject with name %s\n", name); return; } // An XObject must be a stream so obtain the value from the xobject // as if it were a stream. If this fails, the PDF is malformed. if (!CGPDFObjectGetValue(xobject, kCGPDFObjectTypeStream, &stream)){ fprintf(stderr, "XObject '%s' is not a stream!\n", name); return; } // Streams consist of a dictionary and the data associated // with the stream. This code only cares about the dictionary. dict = CGPDFStreamGetDictionary(stream); if(!dict){ fprintf(stderr, "Couldn't obtain dictionary from stream %s!\n", name); return; } // An XObject dict has a Subtype that indicates what kind it is. if(!CGPDFDictionaryGetName(dict, "Subtype", &name)){ fprintf(stderr, "Couldn't get SubType of dictionary object!\n"); return; } // This code is interested in the "Image" Subtype of an XObject. // Check whether this object has Subtype of "Image". printf("%s\n",name); if(strcmp(name, "Image") != 0){ // The Subtype is not "Image" so this must be a form // or other type of XObject. return; } else { CGPDFArrayRef colorSpaceArray; CGPDFDictionaryGetArray(dict,"ColorSpace" ,&colorSpaceArray); CGColorSpaceRef colorSpace=NULL; colorSpace=colorSpaceFromPDFArray(colorSpaceArray); CGPDFDataFormat format; const char *name = NULL, *colorSpaceName = NULL,*renderingIntentName = NULL;; CFDataRef data=CGPDFStreamCopyData(stream,&format); if (format == CGPDFDataFormatRaw){ CGColorSpaceRef cgColorSpace; CGPDFInteger width, height, bps, spp; CGColorRenderingIntent renderingIntent; CGPDFBoolean interpolation = 0; CGDataProviderRef dataProvider = CGDataProviderCreateWithCFData(data); if (!CGPDFDictionaryGetInteger(dict, "Width", &width)) return ; if (!CGPDFDictionaryGetInteger(dict, "Height", &height)) return ; if (!CGPDFDictionaryGetInteger(dict, "BitsPerComponent", &bps)) return ; if (!CGPDFDictionaryGetBoolean(dict, "Interpolate", &interpolation)) interpolation = 0; if (!CGPDFDictionaryGetName(dict, "Intent", &renderingIntentName)) renderingIntent = kCGRenderingIntentDefault; else{ renderingIntent = kCGRenderingIntentDefault; // renderingIntent = renderingIntentFromName(renderingIntentName); } if (CGPDFDictionaryGetArray(dict, "ColorSpace", &colorSpaceArray)) { cgColorSpace = CGColorSpaceCreateDeviceRGB(); // cgColorSpace = colorSpaceFromPDFArray(colorSpaceArray); spp = CGColorSpaceGetNumberOfComponents(cgColorSpace); } else if (CGPDFDictionaryGetName(dict, "ColorSpace", &colorSpaceName)) { if (strcmp(colorSpaceName, "DeviceRGB") == 0) { cgColorSpace = CGColorSpaceCreateDeviceRGB(); // CGColorSpaceCreateWithName(kCGColorSpaceGenericRGB); spp = 3; } else if (strcmp(colorSpaceName, "DeviceCMYK") == 0) { cgColorSpace = CGColorSpaceCreateDeviceCMYK(); // CGColorSpaceCreateWithName(kCGColorSpaceGenericCMYK); spp = 4; } else if (strcmp(colorSpaceName, "DeviceGray") == 0) { cgColorSpace = CGColorSpaceCreateDeviceGray(); // CGColorSpaceCreateWithName(kCGColorSpaceGenericGray); spp = 1; } else if (bps == 1) { // if there's no colorspace entry, there's still one we can infer from bps cgColorSpace = CGColorSpaceCreateDeviceGray(); // colorSpace = NSDeviceBlackColorSpace; spp = 1; } } CGFloat *decodeValues = NULL; decodeValues = decodeValuesFromImageDictionary(dict, cgColorSpace, bps); int rowBits = bps * spp * width; int rowBytes = rowBits / 8; // pdf image row lengths are padded to byte-alignment if (rowBits % 8 != 0) ++rowBytes; CGImageRef sourceImage = CGImageCreate(width, height, bps, bps * spp, rowBytes, cgColorSpace, 0, dataProvider, decodeValues, interpolation, renderingIntent); CGDataProviderRelease(dataProvider); CGDataProviderRef dataProvider2 = CGImageGetDataProvider(sourceImage); CFDataRef data = CGDataProviderCopyData(dataProvider2); int fd; findex; char file[256]; memset(file,0,sizeof(file)); sprintf(file,"%d.jpg",findex); fd=open(file, O_RDWR|O_CREAT); write(fd, CFDataGetBytePtr(data), CFDataGetLength(data)); findex++; close(fd); //[[NSImage alloc] initWithCGImage:sourceImage size:NSMakeSize(0, 0)]; }else { int fd; findex; char file[256]; memset(file,0,sizeof(file)); sprintf(file,"%d.jpg",findex); fd=open(file, O_RDWR|O_CREAT); write(fd, CFDataGetBytePtr(data), CFDataGetLength(data)); findex++; close(fd); } } // This is an Image so figure out what variety of image it is. checkImageType(dict, (MyDataScan *)info); }
CGColorSpaceRef colorSpaceFromPDFArray(CGPDFArrayRef colorSpaceArray){ CGColorSpaceRef cgColorSpace = NULL, alternateColorSpace = NULL; CGPDFStreamRef stream; const char *colorSpaceName = NULL, *alternateColorSpaceName = NULL; CGPDFInteger numberOfComponents; CGPDFDictionaryRef dict; bool retrieved; CGFloat *range; CGPDFArrayRef rangeArray; if (CGPDFArrayGetName(colorSpaceArray, 0, &colorSpaceName)) { if (strcmp(colorSpaceName, "ICCBased") == 0) { if (CGPDFArrayGetStream(colorSpaceArray, 1, &stream)) { dict = CGPDFStreamGetDictionary(stream); // First obtain the alternate color space if present if (CGPDFDictionaryGetName(dict, "Alternate", &alternateColorSpaceName)) { if (strcmp(alternateColorSpaceName, "DeviceRGB") == 0) { alternateColorSpace = CGColorSpaceCreateDeviceRGB(); } else if (strcmp(alternateColorSpaceName, "DeviceGray") == 0) { alternateColorSpace = CGColorSpaceCreateDeviceGray(); } else if (strcmp(alternateColorSpaceName, "DeviceCMYK") == 0) { alternateColorSpace = CGColorSpaceCreateDeviceCMYK(); } } // Obtain the preferential color space CGPDFDataFormat dataFormat; CFDataRef colorSpaceDataPtr = CGPDFStreamCopyData(stream, &dataFormat); if (dataFormat == CGPDFDataFormatRaw) { CGDataProviderRef profile = CGDataProviderCreateWithCFData(colorSpaceDataPtr); retrieved = CGPDFDictionaryGetInteger(dict, "N", &numberOfComponents); // Deduce an alternate color space if we don't have one //already if (alternateColorSpace == NULL) { switch (numberOfComponents) { case 1: alternateColorSpace = CGColorSpaceCreateDeviceGray(); break; case 3: alternateColorSpace = CGColorSpaceCreateDeviceRGB(); break; case 4: alternateColorSpace = CGColorSpaceCreateDeviceCMYK(); break; default: break; } } range = malloc(numberOfComponents * 2 * sizeof(CGFloat)); if (!CGPDFDictionaryGetArray(dict, "Range", &rangeArray)) { int i = 0; for (; i < numberOfComponents * 2; i += 2) { range[i] = (i % 2 == 0) ? 0.0 : 1.0; } } else { size_t count = CGPDFArrayGetCount(rangeArray); int i = 0; for (; i < count; i++) { (void)CGPDFArrayGetNumber(rangeArray, i, &range[i]); } } cgColorSpace = CGColorSpaceCreateICCBased(numberOfComponents, range, profile, alternateColorSpace); CGDataProviderRelease(profile); free(range); if (cgColorSpace) { // Since we have a preferential color space, we no //longer need the hang on to the alternate color space CGColorSpaceRelease(alternateColorSpace); } else { cgColorSpace = alternateColorSpace; } } else if (dataFormat == CGPDFDataFormatJPEGEncoded) { // } else if (dataFormat == CGPDFDataFormatJPEG2000) { // } } } else if (strcmp(colorSpaceName, "Indexed") == 0) { CGColorSpaceRef baseSpace; CGPDFArrayRef base = NULL; CGPDFInteger highValue = 0; CGPDFStreamRef stream = NULL; CGPDFStringRef string; const unsigned char *chars; const char *namedColorSpaceName; if (CGPDFArrayGetArray(colorSpaceArray, 1, &base)) { baseSpace = colorSpaceFromPDFArray(base); } else if (CGPDFArrayGetName(colorSpaceArray, 1, &namedColorSpaceName)) { if (strcmp(namedColorSpaceName, "DeviceRGB") == 0) { baseSpace = CGColorSpaceCreateDeviceRGB(); } else if (strcmp(namedColorSpaceName, "DeviceGray") == 0) { baseSpace = CGColorSpaceCreateDeviceGray(); } else if (strcmp(namedColorSpaceName, "DeviceCMYK") == 0) { baseSpace = CGColorSpaceCreateDeviceCMYK(); } } retrieved = CGPDFArrayGetInteger(colorSpaceArray, 2, &highValue); if (CGPDFArrayGetStream(colorSpaceArray, 3, &stream)) { chars = CFDataGetBytePtr(CGPDFStreamCopyData(stream, NULL)); } else if (CGPDFArrayGetString(colorSpaceArray, 3, &string)) { chars = CGPDFStringGetBytePtr(string); } else { // TODO: Raise some error state? } cgColorSpace = CGColorSpaceCreateIndexed(baseSpace, highValue, chars); } } return (CGColorSpaceRef)CFMakeCollectable(cgColorSpace); }