2 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
5 % DDDD IIIII SSSSS TTTTT RRRR IIIII BBBB U U TTTTT EEEEE %
6 % D D I SS T R R I B B U U T E %
7 % D D I SSS T RRRR I BBBB U U T EEE %
8 % D D I SS T R R I B B U U T E %
9 % DDDDA IIIII SSSSS T R R IIIII BBBB UUU T EEEEE %
11 % CCCC AAA CCCC H H EEEEE %
13 % C AAAAA C HHHHH EEE %
15 % CCCC A A CCCC H H EEEEE %
18 % MagickCore Distributed Pixel Cache Methods %
25 % Copyright 1999-2013 ImageMagick Studio LLC, a non-profit organization %
26 % dedicated to making software imaging solutions freely available. %
28 % You may not use this file except in compliance with the License. You may %
29 % obtain a copy of the License at %
31 % http://www.imagemagick.org/script/license.php %
33 % Unless required by applicable law or agreed to in writing, software %
34 % distributed under the License is distributed on an "AS IS" BASIS, %
35 % WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. %
36 % See the License for the specific language governing permissions and %
37 % limitations under the License. %
39 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
41 % A distributed pixel cache is an extension of the traditional pixel cache
42 % available on a single host. The distributed pixel cache may span multiple
43 % servers so that it can grow in size and transactional capacity to support
44 % very large images. Start up the pixel cache server on one or more machines.
45 % When you read or operate on an image and the local pixel cache resources are
46 % exhausted, ImageMagick contacts one or more of these remote pixel servers to
47 % store or retrieve pixels.
54 #include "MagickCore/studio.h"
55 #include "MagickCore/cache.h"
56 #include "MagickCore/cache-private.h"
57 #include "MagickCore/distribute-cache.h"
58 #include "MagickCore/distribute-cache-private.h"
59 #include "MagickCore/exception.h"
60 #include "MagickCore/exception-private.h"
61 #include "MagickCore/geometry.h"
62 #include "MagickCore/image.h"
63 #include "MagickCore/locale_.h"
64 #include "MagickCore/memory_.h"
65 #include "MagickCore/policy.h"
66 #include "MagickCore/random_.h"
67 #include "MagickCore/registry.h"
68 #include "MagickCore/splay-tree.h"
69 #include "MagickCore/string_.h"
70 #include "MagickCore/string-private.h"
71 #if defined(MAGICKCORE_HAVE_SOCKET)
72 #include <netinet/in.h>
74 #include <sys/socket.h>
75 #include <arpa/inet.h>
81 #define DPCHostname "127.0.0.1"
83 #define DPCSessionKeyLength 8
86 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
90 + A c q u i r e D i s t r i b u t e C a c h e I n f o %
94 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
96 % AcquireDistributeCacheInfo() allocates the DistributeCacheInfo structure.
98 % The format of the AcquireDistributeCacheInfo method is:
100 % DistributeCacheInfo *AcquireDistributeCacheInfo(ExceptionInfo *exception)
102 % A description of each parameter follows:
104 % o exception: return any errors or warnings in this structure.
108 static MagickSizeType CRC64(const unsigned char *message,const size_t length)
116 static MagickBooleanType
117 crc_initial = MagickFalse;
119 static MagickSizeType
122 if (crc_initial == MagickFalse)
127 for (i=0; i < 256; i++)
132 alpha=(MagickSizeType) i;
133 for (j=0; j < 8; j++)
135 if ((alpha & 0x01) == 0)
138 alpha=(MagickSizeType) ((alpha >> 1) ^
139 MagickULLConstant(0xd800000000000000));
143 crc_initial=MagickTrue;
146 for (i=0; i < (ssize_t) length; i++)
147 crc=crc_xor[(crc ^ message[i]) & 0xff] ^ (crc >> 8);
151 static int ConnectPixelCacheServer(const char *hostname,const int port,
152 MagickSizeType *session_key,ExceptionInfo *exception)
154 #if defined(MAGICKCORE_HAVE_SOCKET)
156 secret[MaxTextExtent];
175 session[MaxTextExtent];
178 Connect to distributed pixel cache and get session key.
181 shared_secret=GetPolicyValue("shared-secret");
182 if (shared_secret == (const char *) NULL)
184 (void) ThrowMagickException(exception,GetMagickModule(),CacheError,
185 "DistributedPixelCache","'%s'","shared secret expected");
188 (void) CopyMagickString((char *) session,shared_secret,MaxTextExtent-
189 DPCSessionKeyLength);
190 host=gethostbyname(hostname);
191 client_socket=socket(AF_INET,SOCK_STREAM,0);
192 if (client_socket == -1)
194 (void) ThrowMagickException(exception,GetMagickModule(),CacheError,
195 "DistributedPixelCache","'%s'",hostname);
198 (void) ResetMagickMemory(&address,0,sizeof(address));
199 address.sin_family=AF_INET;
200 address.sin_port=htons((uint16_t) port);
201 address.sin_addr=(*((struct in_addr *) host->h_addr));
202 status=connect(client_socket,(struct sockaddr *) &address,(socklen_t)
203 sizeof(struct sockaddr));
206 (void) ThrowMagickException(exception,GetMagickModule(),CacheError,
207 "DistributedPixelCache","'%s'",hostname);
210 count=read(client_socket,secret,MaxTextExtent);
213 (void) memcpy(session+strlen(shared_secret),secret,(size_t) count);
214 *session_key=CRC64(session,strlen(shared_secret)+count);
216 if (*session_key == 0)
218 close(client_socket);
221 return(client_socket);
223 (void) ThrowMagickException(exception,GetMagickModule(),MissingDelegateError,
224 "DelegateLibrarySupportNotBuiltIn","distributed pixel cache");
229 static char *GetHostname(int *port,ExceptionInfo *exception)
246 Parse host list (e.g. 192.168.100.1:6668,192.168.100.2:6668).
248 hosts=(char *) GetImageRegistry(StringRegistryType,"cache:hosts",
250 if (hosts == (char *) NULL)
253 return(AcquireString(DPCHostname));
255 (void) SubstituteString(&hosts,","," ");
256 hostlist=StringToArgv(hosts,&argc);
257 hosts=DestroyString(hosts);
258 if (hostlist == (char **) NULL)
261 return(AcquireString(DPCHostname));
263 hosts=AcquireString(hostlist[(id++ % (argc-1))+1]);
264 for (i=0; i < (ssize_t) argc; i++)
265 hostlist[i]=DestroyString(hostlist[i]);
266 hostlist=(char **) RelinquishMagickMemory(hostlist);
267 (void) SubstituteString(&hosts,":"," ");
268 hostlist=StringToArgv(hosts,&argc);
269 if (hostlist == (char **) NULL)
272 return(AcquireString(DPCHostname));
274 host=AcquireString(hostlist[1]);
275 if (hostlist[2] == (char *) NULL)
278 *port=StringToLong(hostlist[2]);
279 for (i=0; i < (ssize_t) argc; i++)
280 hostlist[i]=DestroyString(hostlist[i]);
281 hostlist=(char **) RelinquishMagickMemory(hostlist);
285 MagickPrivate DistributeCacheInfo *AcquireDistributeCacheInfo(
286 ExceptionInfo *exception)
292 *distribute_cache_info;
297 distribute_cache_info=(DistributeCacheInfo *) AcquireMagickMemory(
298 sizeof(*distribute_cache_info));
299 if (distribute_cache_info == (DistributeCacheInfo *) NULL)
300 ThrowFatalException(ResourceLimitFatalError,"MemoryAllocationFailed");
301 (void) ResetMagickMemory(distribute_cache_info,0,
302 sizeof(*distribute_cache_info));
303 distribute_cache_info->signature=MagickSignature;
305 Contact pixel cache server.
307 distribute_cache_info->port=0;
308 hostname=GetHostname(&distribute_cache_info->port,exception);
310 distribute_cache_info->file=ConnectPixelCacheServer(hostname,
311 distribute_cache_info->port,&session_key,exception);
312 distribute_cache_info->session_key=session_key;
313 (void) CopyMagickString(distribute_cache_info->hostname,hostname,
315 hostname=DestroyString(hostname);
316 if (distribute_cache_info->file == -1)
317 distribute_cache_info=DestroyDistributeCacheInfo(distribute_cache_info);
318 return(distribute_cache_info);
322 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
326 + D e s t r o y D i s t r i b u t e C a c h e I n f o %
330 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
332 % DestroyDistributeCacheInfo() deallocates memory associated with an
333 % DistributeCacheInfo structure.
335 % The format of the DestroyDistributeCacheInfo method is:
337 % DistributeCacheInfo *DestroyDistributeCacheInfo(
338 % DistributeCacheInfo *distribute_cache_info)
340 % A description of each parameter follows:
342 % o distribute_cache_info: the distributed cache info.
345 MagickPrivate DistributeCacheInfo *DestroyDistributeCacheInfo(
346 DistributeCacheInfo *distribute_cache_info)
348 assert(distribute_cache_info != (DistributeCacheInfo *) NULL);
349 assert(distribute_cache_info->signature == MagickSignature);
350 distribute_cache_info->signature=(~MagickSignature);
351 distribute_cache_info=(DistributeCacheInfo *) RelinquishMagickMemory(
352 distribute_cache_info);
353 return(distribute_cache_info);
357 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
361 + D i s t r i b u t e P i x e l C a c h e S e r v e r %
365 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
367 % DistributePixelCacheServer() waits on the specified port for commands to
368 % create, read, update, or destroy a pixel cache.
370 % The format of the DistributePixelCacheServer() method is:
372 % void DistributePixelCacheServer(const size_t port)
374 % A description of each parameter follows:
376 % o port: connect the distributed pixel cache at this port.
378 % o exception: return any errors or warnings in this structure.
382 static MagickBooleanType CreateDistributeCache(SplayTreeInfo *image_registry,
383 int file,const MagickSizeType session_key)
394 register unsigned char
404 buffer[MaxTextExtent];
406 exception=AcquireExceptionInfo();
407 image=AcquireImage((ImageInfo *) NULL,exception);
408 exception=DestroyExceptionInfo(exception);
409 length=sizeof(image->columns)+sizeof(image->rows)+
410 sizeof(image->number_channels);
411 count=read(file,buffer,length);
412 if (count != (ssize_t) length)
415 (void) memcpy(&image->columns,p,sizeof(image->columns));
416 p+=sizeof(image->columns);
417 (void) memcpy(&image->rows,p,sizeof(image->rows));
418 p+=sizeof(image->rows);
419 (void) memcpy(&image->number_channels,p,sizeof(image->number_channels));
420 p+=sizeof(image->number_channels);
421 status=AddValueToSplayTree(image_registry,(const void *) session_key,image);
425 static MagickBooleanType DestroyDistributeCache(SplayTreeInfo *image_registry,
426 int file,const MagickSizeType session_key)
428 return(DeleteNodeFromSplayTree(image_registry,(const void *) session_key));
431 static MagickBooleanType ReadDistributeCacheMetacontent(
432 SplayTreeInfo *image_registry,int file,const MagickSizeType session_key)
443 register const Quantum
446 register unsigned char
456 buffer[MaxTextExtent];
458 image=(Image *) GetValueFromSplayTree(image_registry,(const void *)
460 if (image == (Image *) NULL)
462 length=sizeof(region.width)+sizeof(region.height)+sizeof(region.x)+
463 sizeof(region.y)+sizeof(length);
464 count=read(file,buffer,length);
465 if (count != (ssize_t) length)
468 (void) memcpy(®ion.width,q,sizeof(region.width));
469 q+=sizeof(region.width);
470 (void) memcpy(®ion.height,q,sizeof(region.height));
471 q+=sizeof(region.width);
472 (void) memcpy(®ion.x,q,sizeof(region.x));
473 q+=sizeof(region.width);
474 (void) memcpy(®ion.y,q,sizeof(region.y));
475 q+=sizeof(region.width);
476 (void) memcpy(&length,q,sizeof(length));
478 exception=AcquireExceptionInfo();
479 p=GetVirtualPixels(image,region.x,region.y,region.width,region.height,
481 exception=DestroyExceptionInfo(exception);
482 if (p == (const Quantum *) NULL)
484 count=write(file,p,length);
485 if (count != (ssize_t) length)
490 static MagickBooleanType ReadDistributeCachePixels(
491 SplayTreeInfo *image_registry,int file,const MagickSizeType session_key)
502 register const Quantum
505 register unsigned char
515 buffer[MaxTextExtent];
517 image=(Image *) GetValueFromSplayTree(image_registry,(const void *)
519 if (image == (Image *) NULL)
521 length=sizeof(region.width)+sizeof(region.height)+sizeof(region.x)+
522 sizeof(region.y)+sizeof(length);
523 count=read(file,buffer,length);
524 if (count != (ssize_t) length)
527 (void) memcpy(®ion.width,q,sizeof(region.width));
528 q+=sizeof(region.width);
529 (void) memcpy(®ion.height,q,sizeof(region.height));
530 q+=sizeof(region.width);
531 (void) memcpy(®ion.x,q,sizeof(region.x));
532 q+=sizeof(region.width);
533 (void) memcpy(®ion.y,q,sizeof(region.y));
534 q+=sizeof(region.width);
535 (void) memcpy(&length,q,sizeof(length));
537 exception=AcquireExceptionInfo();
538 p=GetVirtualPixels(image,region.x,region.y,region.width,region.height,
540 exception=DestroyExceptionInfo(exception);
541 if (p == (const Quantum *) NULL)
543 count=write(file,p,length);
544 if (count != (ssize_t) length)
549 static MagickBooleanType WriteDistributeCacheMetacontent(
550 SplayTreeInfo *image_registry,int file,const MagickSizeType session_key)
567 register unsigned char
577 buffer[MaxTextExtent];
579 image=(Image *) GetValueFromSplayTree(image_registry,(const void *)
581 if (image == (Image *) NULL)
583 length=sizeof(region.width)+sizeof(region.height)+sizeof(region.x)+
584 sizeof(region.y)+sizeof(length);
585 count=read(file,buffer,length);
586 if (count != (ssize_t) length)
589 (void) memcpy(®ion.width,p,sizeof(region.width));
590 p+=sizeof(region.width);
591 (void) memcpy(®ion.height,p,sizeof(region.height));
592 p+=sizeof(region.width);
593 (void) memcpy(®ion.x,p,sizeof(region.x));
594 p+=sizeof(region.width);
595 (void) memcpy(®ion.y,p,sizeof(region.y));
596 p+=sizeof(region.width);
597 (void) memcpy(&length,p,sizeof(length));
599 exception=AcquireExceptionInfo();
600 q=GetAuthenticPixels(image,region.x,region.y,region.width,region.height,
602 exception=DestroyExceptionInfo(exception);
603 if (q == (Quantum *) NULL)
605 count=read(file,q,length);
606 if (count != (ssize_t) length)
608 status=SyncAuthenticPixels(image,exception);
612 static MagickBooleanType WriteDistributeCachePixels(
613 SplayTreeInfo *image_registry,int file,const MagickSizeType session_key)
630 register unsigned char
640 buffer[MaxTextExtent];
642 image=(Image *) GetValueFromSplayTree(image_registry,(const void *)
644 if (image == (Image *) NULL)
646 length=sizeof(region.width)+sizeof(region.height)+sizeof(region.x)+
647 sizeof(region.y)+sizeof(length);
648 count=read(file,buffer,length);
649 if (count != (ssize_t) length)
652 (void) memcpy(®ion.width,p,sizeof(region.width));
653 p+=sizeof(region.width);
654 (void) memcpy(®ion.height,p,sizeof(region.height));
655 p+=sizeof(region.width);
656 (void) memcpy(®ion.x,p,sizeof(region.x));
657 p+=sizeof(region.width);
658 (void) memcpy(®ion.y,p,sizeof(region.y));
659 p+=sizeof(region.width);
660 (void) memcpy(&length,p,sizeof(length));
662 exception=AcquireExceptionInfo();
663 q=GetAuthenticPixels(image,region.x,region.y,region.width,region.height,
665 exception=DestroyExceptionInfo(exception);
666 if (q == (Quantum *) NULL)
668 count=read(file,q,length);
669 if (count != (ssize_t) length)
671 status=SyncAuthenticPixels(image,exception);
675 static void *DistributePixelCacheClient(void *socket)
704 session[MaxTextExtent];
707 Generate session key.
709 shared_secret=GetPolicyValue("shared-secret");
710 if (shared_secret == (const char *) NULL)
711 ThrowFatalException(CacheFatalError,"shared secret expected");
712 (void) CopyMagickString((char *) session,shared_secret,MaxTextExtent-
713 DPCSessionKeyLength);
714 random_info=AcquireRandomInfo();
715 secret=GetRandomKey(random_info,DPCSessionKeyLength);
716 (void) memcpy(session+strlen(shared_secret),GetStringInfoDatum(secret),
717 DPCSessionKeyLength);
718 session_key=CRC64(session,strlen(shared_secret)+DPCSessionKeyLength);
719 random_info=DestroyRandomInfo(random_info);
720 image_registry=NewSplayTree((int (*)(const void *,const void *)) NULL,
721 (void *(*)(void *)) NULL,(void *(*)(void *)) NULL);
722 client_socket=(*(int *) socket);
723 count=write(client_socket,GetStringInfoDatum(secret),DPCSessionKeyLength);
724 secret=DestroyStringInfo(secret);
727 count=read(client_socket,&command,1);
730 count=read(client_socket,&key,sizeof(key));
731 if ((count != (ssize_t) sizeof(key)) && (key != session_key))
738 status=CreateDistributeCache(image_registry,client_socket,session_key);
743 status=ReadDistributeCachePixels(image_registry,client_socket,
749 status=WriteDistributeCachePixels(image_registry,client_socket,
755 status=DestroyDistributeCache(image_registry,client_socket,session_key);
760 status=ReadDistributeCacheMetacontent(image_registry,client_socket,
766 status=WriteDistributeCacheMetacontent(image_registry,client_socket,
773 count=write(client_socket,&status,sizeof(status));
774 if (count != (ssize_t) sizeof(status))
777 return((void *) NULL);
780 MagickExport void DistributePixelCacheServer(const size_t port,
781 ExceptionInfo *exception)
783 #if defined(MAGICKCORE_HAVE_SOCKET) && defined(MAGICKCORE_THREAD_SUPPORT)
798 Launch distributed pixel cache server.
800 server_socket=socket(AF_INET,SOCK_STREAM,0);
801 address.sin_family=AF_INET;
802 address.sin_port=htons(port);
803 address.sin_addr.s_addr=htonl(INADDR_ANY);
804 status=bind(server_socket,(struct sockaddr *) &address,(socklen_t)
807 ThrowFatalException(CacheFatalError,"UnableToBind");
808 status=listen(server_socket,32);
810 ThrowFatalException(CacheFatalError,"UnableToListen");
811 pthread_attr_init(&attributes);
820 length=(socklen_t) sizeof(address);
821 client_socket=accept(server_socket,(struct sockaddr *) &address,&length);
822 if (client_socket == -1)
823 ThrowFatalException(CacheFatalError,"UnableToEstablishConnection");
824 status=pthread_create(&threads,&attributes,DistributePixelCacheClient,
825 (void *) &client_socket);
827 ThrowFatalException(CacheFatalError,"UnableToCreateClientThread");
829 (void) close(server_socket);
831 ThrowFatalException(MissingDelegateError,"distributed pixel cache");
836 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
840 + G e t D i s t r i b u t e C a c h e F i l e %
844 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
846 % GetDistributeCacheFile() returns the file associated with this
847 % DistributeCacheInfo structure.
849 % The format of the GetDistributeCacheFile method is:
851 % int GetDistributeCacheFile(
852 % const DistributeCacheInfo *distribute_cache_info)
854 % A description of each parameter follows:
856 % o distribute_cache_info: the distributed cache info.
859 MagickPrivate int GetDistributeCacheFile(
860 const DistributeCacheInfo *distribute_cache_info)
862 assert(distribute_cache_info != (DistributeCacheInfo *) NULL);
863 assert(distribute_cache_info->signature == MagickSignature);
864 return(distribute_cache_info->file);
868 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
872 + G e t D i s t r i b u t e C a c h e H o s t n a m e %
876 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
878 % GetDistributeCacheHostname() returns the hostname associated with this
879 % DistributeCacheInfo structure.
881 % The format of the GetDistributeCacheHostname method is:
883 % const char *GetDistributeCacheHostname(
884 % const DistributeCacheInfo *distribute_cache_info)
886 % A description of each parameter follows:
888 % o distribute_cache_info: the distributed cache info.
891 MagickPrivate const char *GetDistributeCacheHostname(
892 const DistributeCacheInfo *distribute_cache_info)
894 assert(distribute_cache_info != (DistributeCacheInfo *) NULL);
895 assert(distribute_cache_info->signature == MagickSignature);
896 return(distribute_cache_info->hostname);
900 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
904 + G e t D i s t r i b u t e C a c h e P o r t %
908 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
910 % GetDistributeCachePort() returns the port associated with this
911 % DistributeCacheInfo structure.
913 % The format of the GetDistributeCachePort method is:
915 % int GetDistributeCachePort(
916 % const DistributeCacheInfo *distribute_cache_info)
918 % A description of each parameter follows:
920 % o distribute_cache_info: the distributed cache info.
923 MagickPrivate int GetDistributeCachePort(
924 const DistributeCacheInfo *distribute_cache_info)
926 assert(distribute_cache_info != (DistributeCacheInfo *) NULL);
927 assert(distribute_cache_info->signature == MagickSignature);
928 return(distribute_cache_info->port);
932 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
936 + O p e n D i s t r i b u t e P i x e l C a c h e %
940 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
942 % OpenDistributePixelCache() opens a pixel cache on a remote server.
944 % The format of the OpenDistributePixelCache method is:
946 % MagickBooleanType *OpenDistributePixelCache(
947 % DistributeCacheInfo *distribute_cache_info,Image *image)
949 % A description of each parameter follows:
951 % o distribute_cache_info: the distributed cache info.
953 % o image: the image.
956 MagickPrivate MagickBooleanType OpenDistributePixelCache(
957 DistributeCacheInfo *distribute_cache_info,Image *image)
962 register unsigned char
969 buffer[MaxTextExtent];
971 assert(distribute_cache_info != (DistributeCacheInfo *) NULL);
972 assert(distribute_cache_info->signature == MagickSignature);
973 assert(image != (Image *) NULL);
974 assert(image->signature == MagickSignature);
976 *p++='c'; /* create */
977 (void) memcpy(p,&distribute_cache_info->session_key,
978 sizeof(distribute_cache_info->session_key));
979 p+=sizeof(distribute_cache_info->session_key);
980 (void) memcpy(p,&image->columns,sizeof(image->columns));
981 p+=sizeof(image->columns);
982 (void) memcpy(p,&image->rows,sizeof(image->rows));
983 p+=sizeof(image->rows);
984 (void) memcpy(p,&image->number_channels,sizeof(image->number_channels));
985 p+=sizeof(image->number_channels);
986 count=write(distribute_cache_info->file,buffer,p-buffer);
987 if (count != (ssize_t) (p-buffer))
989 count=read(distribute_cache_info->file,&status,sizeof(status));
990 if (count != (ssize_t) sizeof(status))
996 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
1000 + R e a d D i s t r i b u t e P i x e l C a c h e M e t a c o n t e n t %
1004 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
1006 % ReadDistributePixelCacheMetacontents() reads metacontent from the specified
1007 % region of the distributed pixel cache.
1009 % The format of the ReadDistributePixelCacheMetacontents method is:
1011 % MagickBooleanType *ReadDistributePixelCacheMetacontents(
1012 % DistributeCacheInfo *distribute_cache_info,const RectangleInfo *region,
1013 % const MagickSizeType length,unsigned char *metacontent)
1015 % A description of each parameter follows:
1017 % o distribute_cache_info: the distributed cache info.
1019 % o image: the image.
1021 % o region: read the metacontent from this region of the image.
1023 % o length: write the metacontent to this region of the image.
1025 % o metacontent: read these metacontent from the pixel cache.
1028 MagickPrivate MagickBooleanType ReadDistributePixelCacheMetacontent(
1029 DistributeCacheInfo *distribute_cache_info,const RectangleInfo *region,
1030 const MagickSizeType length,unsigned char *metacontent)
1035 register unsigned char
1042 buffer[MaxTextExtent];
1044 assert(distribute_cache_info != (DistributeCacheInfo *) NULL);
1045 assert(distribute_cache_info->signature == MagickSignature);
1046 assert(region != (RectangleInfo *) NULL);
1047 assert(metacontent != (unsigned char *) NULL);
1048 assert(length == ((size_t) length));
1050 *p++='m'; /* read */
1051 (void) memcpy(p,&distribute_cache_info->session_key,
1052 sizeof(distribute_cache_info->session_key));
1053 p+=sizeof(distribute_cache_info->session_key);
1054 (void) memcpy(p,®ion->width,sizeof(region->width));
1055 p+=sizeof(region->width);
1056 (void) memcpy(p,®ion->height,sizeof(region->height));
1057 p+=sizeof(region->height);
1058 (void) memcpy(p,®ion->x,sizeof(region->x));
1059 p+=sizeof(region->x);
1060 (void) memcpy(p,®ion->y,sizeof(region->y));
1061 p+=sizeof(region->y);
1062 (void) memcpy(p,&length,sizeof(length));
1064 count=write(distribute_cache_info->file,buffer,p-buffer);
1065 if (count != (ssize_t) (p-buffer))
1066 return(MagickFalse);
1067 count=read(distribute_cache_info->file,(unsigned char *) metacontent,(size_t)
1069 if (count != (ssize_t) length)
1070 return(MagickFalse);
1071 count=read(distribute_cache_info->file,&status,sizeof(status));
1072 if (count != (ssize_t) sizeof(status))
1073 return(MagickFalse);
1074 return(status != 0 ? MagickTrue : MagickFalse);
1078 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
1082 + R e a d D i s t r i b u t e P i x e l C a c h e P i x e l s %
1086 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
1088 % ReadDistributePixelCachePixels() reads pixels from the specified region of
1089 % the distributed pixel cache.
1091 % The format of the ReadDistributePixelCachePixels method is:
1093 % MagickBooleanType *ReadDistributePixelCachePixels(
1094 % DistributeCacheInfo *distribute_cache_info,const RectangleInfo *region,
1095 % const MagickSizeType length,unsigned char *pixels)
1097 % A description of each parameter follows:
1099 % o distribute_cache_info: the distributed cache info.
1101 % o image: the image.
1103 % o region: read the pixels from this region of the image.
1105 % o length: write the pixels to this region of the image.
1107 % o pixels: read these pixels from the pixel cache.
1110 MagickPrivate MagickBooleanType ReadDistributePixelCachePixels(
1111 DistributeCacheInfo *distribute_cache_info,const RectangleInfo *region,
1112 const MagickSizeType length,unsigned char *pixels)
1117 register unsigned char
1124 buffer[MaxTextExtent];
1126 assert(distribute_cache_info != (DistributeCacheInfo *) NULL);
1127 assert(distribute_cache_info->signature == MagickSignature);
1128 assert(region != (RectangleInfo *) NULL);
1129 assert(pixels != (unsigned char *) NULL);
1130 assert(length == ((size_t) length));
1132 *p++='r'; /* read */
1133 (void) memcpy(p,&distribute_cache_info->session_key,
1134 sizeof(distribute_cache_info->session_key));
1135 p+=sizeof(distribute_cache_info->session_key);
1136 (void) memcpy(p,®ion->width,sizeof(region->width));
1137 p+=sizeof(region->width);
1138 (void) memcpy(p,®ion->height,sizeof(region->height));
1139 p+=sizeof(region->height);
1140 (void) memcpy(p,®ion->x,sizeof(region->x));
1141 p+=sizeof(region->x);
1142 (void) memcpy(p,®ion->y,sizeof(region->y));
1143 p+=sizeof(region->y);
1144 (void) memcpy(p,&length,sizeof(length));
1146 count=write(distribute_cache_info->file,buffer,p-buffer);
1147 if (count != (ssize_t) (p-buffer))
1148 return(MagickFalse);
1149 count=read(distribute_cache_info->file,(unsigned char *) pixels,(size_t)
1151 if (count != (ssize_t) length)
1152 return(MagickFalse);
1153 count=read(distribute_cache_info->file,&status,sizeof(status));
1154 if (count != (ssize_t) sizeof(status))
1155 return(MagickFalse);
1156 return(status != 0 ? MagickTrue : MagickFalse);
1160 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
1164 + R e l i n q u i s h D i s t r i b u t e P i x e l C a c h e %
1168 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
1170 % RelinquishDistributePixelCache() frees resources acquired with
1171 % OpenDistributePixelCache().
1173 % The format of the RelinquishDistributePixelCache method is:
1175 % MagickBooleanType RelinquishDistributePixelCache(
1176 % DistributeCacheInfo *distribute_cache_info)
1178 % A description of each parameter follows:
1180 % o distribute_cache_info: the distributed cache info.
1183 MagickPrivate MagickBooleanType RelinquishDistributePixelCache(
1184 DistributeCacheInfo *distribute_cache_info)
1186 register unsigned char
1193 buffer[MaxTextExtent];
1195 assert(distribute_cache_info != (DistributeCacheInfo *) NULL);
1196 assert(distribute_cache_info->signature == MagickSignature);
1198 *p++='d'; /* delete */
1199 (void) memcpy(p,&distribute_cache_info->session_key,
1200 sizeof(distribute_cache_info->session_key));
1201 p+=sizeof(distribute_cache_info->session_key);
1202 count=write(distribute_cache_info->file,buffer,p-buffer);
1203 if (count != (ssize_t) (p-buffer))
1204 return(MagickFalse);
1209 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
1213 + W r i t e D i s t r i b u t e P i x e l C a c h e M e t a c o n t e n t %
1217 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
1219 % WriteDistributePixelCacheMetacontents() writes image metacontent to the
1220 % specified region of the distributed pixel cache.
1223 % The format of the WriteDistributePixelCacheMetacontents method is:
1225 % MagickBooleanType *WriteDistributePixelCacheMetacontents(
1226 % DistributeCacheInfo *distribute_cache_info,const RectangleInfo *region,
1227 % const MagickSizeType length,const unsigned char *metacontent)
1229 % A description of each parameter follows:
1231 % o distribute_cache_info: the distributed cache info.
1233 % o image: the image.
1235 % o region: write the metacontent to this region of the image.
1237 % o length: write the metacontent to this region of the image.
1239 % o metacontent: write these metacontent to the pixel cache.
1242 MagickPrivate MagickBooleanType WriteDistributePixelCacheMetacontent(
1243 DistributeCacheInfo *distribute_cache_info,const RectangleInfo *region,
1244 const MagickSizeType length,const unsigned char *metacontent)
1249 register unsigned char
1256 buffer[MaxTextExtent];
1258 assert(distribute_cache_info != (DistributeCacheInfo *) NULL);
1259 assert(distribute_cache_info->signature == MagickSignature);
1260 assert(region != (RectangleInfo *) NULL);
1261 assert(metacontent != (unsigned char *) NULL);
1262 assert(length == ((size_t) length));
1264 *p++='M'; /* update */
1265 (void) memcpy(p,&distribute_cache_info->session_key,
1266 sizeof(distribute_cache_info->session_key));
1267 p+=sizeof(distribute_cache_info->session_key);
1268 (void) memcpy(p,®ion->width,sizeof(region->width));
1269 p+=sizeof(region->width);
1270 (void) memcpy(p,®ion->height,sizeof(region->height));
1271 p+=sizeof(region->height);
1272 (void) memcpy(p,®ion->x,sizeof(region->x));
1273 p+=sizeof(region->x);
1274 (void) memcpy(p,®ion->y,sizeof(region->y));
1275 p+=sizeof(region->y);
1276 (void) memcpy(p,&length,sizeof(length));
1278 count=write(distribute_cache_info->file,buffer,p-buffer);
1279 if (count != (ssize_t) (p-buffer))
1280 return(MagickFalse);
1281 count=write(distribute_cache_info->file,(unsigned char *) metacontent,(size_t)
1283 if (count != (ssize_t) length)
1284 return(MagickFalse);
1285 count=read(distribute_cache_info->file,&status,sizeof(status));
1286 if (count != (ssize_t) sizeof(status))
1287 return(MagickFalse);
1288 return(status != 0 ? MagickTrue : MagickFalse);
1292 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
1296 + W r i t e D i s t r i b u t e P i x e l C a c h e P i x e l s %
1300 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
1302 % WriteDistributePixelCachePixels() writes image pixels to the specified
1303 % region of the distributed pixel cache.
1306 % The format of the WriteDistributePixelCachePixels method is:
1308 % MagickBooleanType *WriteDistributePixelCachePixels(
1309 % DistributeCacheInfo *distribute_cache_info,const RectangleInfo *region,
1310 % const MagickSizeType length,const unsigned char *pixels)
1312 % A description of each parameter follows:
1314 % o distribute_cache_info: the distributed cache info.
1316 % o image: the image.
1318 % o region: write the pixels to this region of the image.
1320 % o length: write the pixels to this region of the image.
1322 % o pixels: write these pixels to the pixel cache.
1325 MagickPrivate MagickBooleanType WriteDistributePixelCachePixels(
1326 DistributeCacheInfo *distribute_cache_info,const RectangleInfo *region,
1327 const MagickSizeType length,const unsigned char *pixels)
1332 register unsigned char
1339 buffer[MaxTextExtent];
1341 assert(distribute_cache_info != (DistributeCacheInfo *) NULL);
1342 assert(distribute_cache_info->signature == MagickSignature);
1343 assert(region != (RectangleInfo *) NULL);
1344 assert(pixels != (const unsigned char *) NULL);
1345 assert(length == ((size_t) length));
1347 *p++='u'; /* update */
1348 (void) memcpy(p,&distribute_cache_info->session_key,
1349 sizeof(distribute_cache_info->session_key));
1350 p+=sizeof(distribute_cache_info->session_key);
1351 (void) memcpy(p,®ion->width,sizeof(region->width));
1352 p+=sizeof(region->width);
1353 (void) memcpy(p,®ion->height,sizeof(region->height));
1354 p+=sizeof(region->height);
1355 (void) memcpy(p,®ion->x,sizeof(region->x));
1356 p+=sizeof(region->x);
1357 (void) memcpy(p,®ion->y,sizeof(region->y));
1358 p+=sizeof(region->y);
1359 (void) memcpy(p,&length,sizeof(length));
1361 count=write(distribute_cache_info->file,buffer,p-buffer);
1362 if (count != (ssize_t) (p-buffer))
1363 return(MagickFalse);
1364 count=write(distribute_cache_info->file,(unsigned char *) pixels,(size_t)
1366 if (count != (ssize_t) length)
1367 return(MagickFalse);
1368 count=read(distribute_cache_info->file,&status,sizeof(status));
1369 if (count != (ssize_t) sizeof(status))
1370 return(MagickFalse);
1371 return(status != 0 ? MagickTrue : MagickFalse);